summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-11-29 03:52:46 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-11-29 03:52:46 +0100
commit4fcb511e0f4f81453b1c97df99c56ebe1b38aade (patch)
tree342aa9261a573796f021eefa0d579858fcf597bc
parent20b90c175988bf01b4c0933d956879f5848c6195 (diff)
downloadtddd86-4fcb511e0f4f81453b1c97df99c56ebe1b38aade.tar.gz
function comments, reorder includes
-rwxr-xr-xlabb5/src/Boggle.cpp33
-rwxr-xr-xlabb5/src/Boggle.h61
-rwxr-xr-xlabb5/src/boggleplay.cpp5
3 files changed, 81 insertions, 18 deletions
diff --git a/labb5/src/Boggle.cpp b/labb5/src/Boggle.cpp
index 5bec889..7eb37e8 100755
--- a/labb5/src/Boggle.cpp
+++ b/labb5/src/Boggle.cpp
@@ -4,14 +4,15 @@
// Also remove these comments here and add your own.
// TODO: remove this comment header and replace it with your own
-#include <sstream>
-#include <iostream>
#include "Boggle.h"
+
+#include <algorithm>
+#include <chrono>
+#include <iostream>
+#include <sstream>
#include "random.h"
#include "shuffle.h"
#include "strlib.h"
-#include <chrono>
-#include <algorithm>
static const int NUM_CUBES = 16; // the number of cubes in the game
static const int CUBE_SIDES = 6; // the number of sides on each cube
@@ -22,6 +23,12 @@ static string CUBES[NUM_CUBES] = { // the letters on all 6 sides of every cube
"EIOSST", "ELRTTY", "HIMNQU", "HLNNRZ"
};
+/*
+ * Find all immediate neighbours (including diagonally).
+ *
+ * Only returns points where x is in [0, w) and y is in [0, h).
+ * Additionally, points in visited are ignored.
+ */
vector<point> neighbours_in_range_filt(const point& p, int width, int height, const set<point>& visited) {
int x, y;
tie(x, y) = p;
@@ -39,6 +46,17 @@ vector<point> neighbours_in_range_filt(const point& p, int width, int height, co
return res;
}
+/*
+ * Return wether word starts with prefix.
+ */
+// There is a string::starts_with in C++20.
+bool prefix_matches(const string& prefix, const string& word) {
+ for (int i = 0; i < prefix.length(); i++) {
+ if (prefix[i] != word[i]) return false;
+ }
+ return true;
+}
+
Boggle::Boggle() {
board = Grid<char>(4, 4);
dictionary = Lexicon(DICTIONARY_FILE);
@@ -108,13 +126,6 @@ bool Boggle::find_single_word(const string& word) const {
return found;
}
-bool prefix_matches(const string& prefix, const string& word) {
- for (int i = 0; i < prefix.length(); i++) {
- if (prefix[i] != word[i]) return false;
- }
- return true;
-}
-
bool Boggle::find_single_word_helper(const string& word, point cur_point, string cur_word, set<point> visited) const {
if (cur_word == word && dictionary.contains(cur_word)) {
return true;
diff --git a/labb5/src/Boggle.h b/labb5/src/Boggle.h
index 1c73f3d..8e923a9 100755
--- a/labb5/src/Boggle.h
+++ b/labb5/src/Boggle.h
@@ -8,11 +8,11 @@
#define _boggle_h
#include <iostream>
-#include <string>
#include <set>
-#include "lexicon.h"
-#include "grid.h"
+#include <string>
#include <utility>
+#include "grid.h"
+#include "lexicon.h"
using namespace std;
using point = pair<int, int>;
@@ -25,20 +25,67 @@ public:
bool debug_mode = false;
Boggle();
+
+ /*
+ * Try to load a string of letters as the board.
+ *
+ * Returns wether it succeeds or not.
+ */
bool letters_from_string(const string& letters);
void read_dictionary();
+
+ /*
+ * Clear state between games.
+ */
void clear();
+
+ /*
+ * Shuffle the board, both the dices and their locations.
+ */
void shuffle();
- set<string> find_all_words() const;
+ /*
+ * Return wether a word can be constructed on the current board or not.
+ *
+ * Times itself if debug_mode is enabled.
+ */
bool find_single_word(const string& word) const;
+ /*
+ * Find and return all valid words that can be constructed from the current board.
+ *
+ * Times itself if debug_mode is enabled.
+ */
+ set<string> find_all_words() const;
+
+ /*
+ * Convert a board to its string representation.
+ *
+ * Does not contain a trailing newline.
+ */
string board_to_string() const;
+
+ /*
+ * Return a string represntation of all user words.
+ */
string user_words_to_string(int words_per_line = 8) const;
+
+ /*
+ * Return a string represntation of all computer words.
+ */
string computer_words_to_string(int words_per_line = 8) const;
+
+ /*
+ * Return a string representation of a set of words.
+ */
string words_to_string(const set<string>& words, int words_per_line = 8) const;
+ /*
+ * Run calculations for when it's the computers turn.
+ *
+ * Finds which words the computer found and how many points they're worths.
+ */
void do_computer_turn();
int get_computer_words_size() const;
int get_computer_score() const;
@@ -48,6 +95,12 @@ public:
bool word_is_valid(const string& word) const;
bool word_is_unplayed(const string& word) const;
+
+ /*
+ * Add a word the user has found and awards points.
+ *
+ * Does not do any checks.
+ */
void add_user_word(const string& word);
private:
diff --git a/labb5/src/boggleplay.cpp b/labb5/src/boggleplay.cpp
index 23daf99..f9bf79d 100755
--- a/labb5/src/boggleplay.cpp
+++ b/labb5/src/boggleplay.cpp
@@ -3,15 +3,14 @@
// TODO remove this comment header and replace with your own
#include <cstdlib>
-#include <iostream>
#include <iomanip>
+#include <iostream>
#include <sstream>
+#include <string>
#include "Boggle.h"
#include "bogglemain.h"
#include "strlib.h"
-#include <string>
-
void setup_board(Boggle& boggle) {
bool input_custom_board = yesOrNo("Input custom board? ");
if (input_custom_board) {