summaryrefslogtreecommitdiffstats
path: root/labb5/src
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-11-17 15:42:27 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-11-17 15:42:27 +0100
commitfb80ac50825c7ca1fa063d3493175b7b27adbdb1 (patch)
tree4d84895d45ca2c177ddefe11164575bf6762b3ca /labb5/src
parent4065799f7080260f507a5e3ea8c2d8375e735166 (diff)
downloadtddd86-fb80ac50825c7ca1fa063d3493175b7b27adbdb1.tar.gz
add given code
Diffstat (limited to 'labb5/src')
-rwxr-xr-xlabb5/src/Boggle.cpp22
-rwxr-xr-xlabb5/src/Boggle.h29
-rwxr-xr-xlabb5/src/bogglemain.cpp79
-rwxr-xr-xlabb5/src/bogglemain.h21
-rwxr-xr-xlabb5/src/boggleplay.cpp32
-rwxr-xr-xlabb5/src/shuffle.h78
6 files changed, 261 insertions, 0 deletions
diff --git a/labb5/src/Boggle.cpp b/labb5/src/Boggle.cpp
new file mode 100755
index 0000000..9988c4e
--- /dev/null
+++ b/labb5/src/Boggle.cpp
@@ -0,0 +1,22 @@
+// This is the .cpp file you will edit and turn in.
+// We have provided a minimal skeleton for you,
+// but you must finish it as described in the spec.
+// Also remove these comments here and add your own.
+// TODO: remove this comment header and replace it with your own
+
+#include <sstream>
+#include "Boggle.h"
+#include "random.h"
+#include "shuffle.h"
+#include "strlib.h"
+
+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
+static string CUBES[NUM_CUBES] = { // the letters on all 6 sides of every cube
+ "AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS",
+ "AOOTTW", "CIMOTU", "DEILRX", "DELRVY",
+ "DISTTY", "EEGHNW", "EEINSU", "EHRTVW",
+ "EIOSST", "ELRTTY", "HIMNQU", "HLNNRZ"
+};
+
+// TODO: implement the members you declared in Boggle.h
diff --git a/labb5/src/Boggle.h b/labb5/src/Boggle.h
new file mode 100755
index 0000000..3abe9b5
--- /dev/null
+++ b/labb5/src/Boggle.h
@@ -0,0 +1,29 @@
+// This is the .h file you will edit and turn in.
+// We have provided a minimal skeleton for you,
+// but you must finish it as described in the spec.
+// Also remove these comments here and add your own, as well as on the members.
+// TODO: remove this comment header and replace it with your own
+
+#ifndef _boggle_h
+#define _boggle_h
+
+#include <iostream>
+#include <string>
+// TODO: include any other header files you need
+
+using namespace std;
+
+class Boggle {
+public:
+ const string DICTIONARY_FILE = "EnglishWords.dat";
+ const int MIN_WORD_LENGTH = 4;
+ const int BOARD_SIZE = 4;
+
+ // TODO: decide the public member functions and declare them
+
+private:
+ // TODO: decide the private member variables/functions and declare them
+
+};
+
+#endif
diff --git a/labb5/src/bogglemain.cpp b/labb5/src/bogglemain.cpp
new file mode 100755
index 0000000..067c8a0
--- /dev/null
+++ b/labb5/src/bogglemain.cpp
@@ -0,0 +1,79 @@
+/*
+ * TDDD86 Boggle
+ * This file contains the main program and user interface for running your
+ * Boggle game. We provide you a skeleton of this file that contains a shell
+ * of the overall logic, but you must complete the playOneGame function.
+ *
+ * The playOneGame function talks to the Boggle class that you will write.
+ * This file should contain all user interaction (cout / cin), while the Boggle
+ * class should contain ALL game state such as the 16 letter cubes, the set of
+ * words that have been formed, the algorithms for searching for words, etc.
+ *
+ * Please do not modify this provided file. Your turned-in files should work
+ * with an unmodified version of all provided code files.
+ */
+
+#include <fstream>
+#include <iostream>
+#include <string>
+#include "random.h"
+#include "strlib.h"
+#include "Boggle.h"
+#include "bogglemain.h"
+
+using namespace std;
+
+int main() {
+ intro();
+
+ // play games repeatedly until user decides to quit
+ Boggle boggle;
+ while (true) {
+ playOneGame(boggle);
+ cout << endl;
+ if (!yesOrNo("Play again (Y/N)? ")) {
+ break;
+ }
+ }
+
+ cout << "Have a nice day." << endl;
+ return 0;
+}
+
+/*
+ * Explains the program to the user.
+ */
+void intro() {
+ cout << "Welcome to TDDD86 Boggle!" << endl;
+ cout << "This game is a search for words on a 2-D board of letter cubes." << endl;
+ cout << "The good news is that you might improve your vocabulary a bit." << endl;
+ cout << "The bad news is that you're probably going to lose miserably to" << endl;
+ cout << "this little dictionary-toting hunk of silicon." << endl;
+ cout << "If only YOU had a gig of RAM!" << endl;
+ cout << endl;
+ cout << "Press Enter to begin the game ... ";
+ string line;
+ getline(cin, line);
+}
+
+/*
+ * Prompts the user to answer a yes/no question and returns true if the user
+ * typed 'yes' (or anything that starts with a 'y', case-insensitively),
+ * false if the user types anything that starts with 'n', or re-prompts if
+ * the user doesn't type a 'y' or 'n' word.
+ */
+bool yesOrNo(string prompt) {
+ cout << prompt;
+ while (true) {
+ string answer;
+ getline(cin, answer);
+ answer = trim(toLowerCase(answer));
+ if (startsWith(answer, 'y')) {
+ return true;
+ } else if (startsWith(answer, 'n')) {
+ return false;
+ } else {
+ cout << "Please type a word that begins with 'y' or 'n'." << endl;
+ }
+ }
+}
diff --git a/labb5/src/bogglemain.h b/labb5/src/bogglemain.h
new file mode 100755
index 0000000..78b7f25
--- /dev/null
+++ b/labb5/src/bogglemain.h
@@ -0,0 +1,21 @@
+/*
+ * TDDD86 Boggle
+ * This file declares required function prototypes that are defined in
+ * our provided bogglemain.cpp and your boggleplay.cpp that you will write.
+ * See the respective .cpp files for implementation comments for each function.
+ * Please do not modify this provided file.
+ */
+
+#ifndef _bogglemain_h
+#define _bogglemain_h
+
+#include "Boggle.h"
+#include <string>
+using namespace std;
+
+void intro();
+void playOneGame(Boggle& boggle);
+bool yesOrNo(string prompt);
+void clearConsole();
+
+#endif
diff --git a/labb5/src/boggleplay.cpp b/labb5/src/boggleplay.cpp
new file mode 100755
index 0000000..76bdcde
--- /dev/null
+++ b/labb5/src/boggleplay.cpp
@@ -0,0 +1,32 @@
+// You will edit and turn in this CPP file.
+// Also remove these comments here and add your own.
+// TODO: remove this comment header and replace with your own
+
+#include <cstdlib>
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include "Boggle.h"
+#include "bogglemain.h"
+#include "strlib.h"
+// TODO: include any other header files you need
+
+/*
+ * Plays one game of Boggle using the given boggle game state object.
+ */
+void playOneGame(Boggle& boggle) {
+ // TODO: implement this function (and add any other functions you like to help you)
+
+}
+
+/*
+ * Erases all currently visible text from the output console.
+ */
+void clearConsole() {
+#if defined(_WIN32) || defined(_WIN64)
+ std::system("CLS");
+#else
+ // assume POSIX
+ std::system("clear");
+#endif
+}
diff --git a/labb5/src/shuffle.h b/labb5/src/shuffle.h
new file mode 100755
index 0000000..ebf48c2
--- /dev/null
+++ b/labb5/src/shuffle.h
@@ -0,0 +1,78 @@
+/*
+ * TDDD86 Boggle
+ * This file contains implementation of a shuffling function that operates on
+ * a 1-D and 2-D array, vector, or Grid of any type.
+ * You can use it in your program.
+ * Please do not modify this provided file.
+ */
+
+#ifndef _shuffle_h
+#define _shuffle_h
+
+#include "grid.h"
+#include "random.h"
+#include <vector>
+
+template <typename T>
+void shuffle(T* array, int length) {
+ for (int i = 0; i < length; i++) {
+ int j = randomInteger(i, length - 1);
+ if (i != j) {
+ T temp = array[i];
+ array[i] = array[j];
+ array[j] = temp;
+ }
+ }
+}
+
+template <typename T>
+void shuffle(T** array2d, int rows, int cols) {
+ int length = rows * cols;
+ for (int i = 0; i < length; i++) {
+ int j = randomInteger(i, length - 1);
+ if (i != j) {
+ int r1 = i / cols;
+ int c1 = i % cols;
+ int r2 = j / cols;
+ int c2 = j % cols;
+
+ T temp = array2d[r1][c1];
+ array2d[r1][c1] = array2d[r2][c2];
+ array2d[r2][c2] = temp;
+ }
+ }
+}
+
+template <typename T>
+void shuffle(vector<T>& v) {
+ for (int i = 0, length = v.size(); i < length; i++) {
+ int j = randomInteger(i, length - 1);
+ if (i != j) {
+ T temp = v[i];
+ v[i] = v[j];
+ v[j] = temp;
+ }
+ }
+}
+
+template <typename T>
+void shuffle(Grid<T>& grid) {
+ int rows = grid.numRows();
+ int cols = grid.numCols();
+ int length = rows * cols;
+ for (int i = 0; i < length; i++) {
+ int j = randomInteger(i, length - 1);
+ if (i != j) {
+ int r1 = i / cols;
+ int c1 = i % cols;
+ int r2 = j / cols;
+ int c2 = j % cols;
+
+ T temp = grid[r1][c1];
+ grid[r1][c1] = grid[r2][c2];
+ grid[r2][c2] = temp;
+ }
+ }
+}
+
+#endif