Compare commits

..

No commits in common. "136451c53bcad8cf2d5cffa98f772a0ef211aca7" and "6e5fa6778fb418df21ed8993c1a94ce15aef5636" have entirely different histories.

4 changed files with 16 additions and 123 deletions

View File

@ -10,103 +10,21 @@
#include "random.h"
#include "shuffle.h"
#include "strlib.h"
#include <fstream>
#include <sstream>
#include <unordered_set>
// The res folder isn't copied the same way on macOS
#ifdef __APPLE__
const std::string Boggle::s_DICTIONARY_FILE = "../../../EnglishWords.dat";
const std::string Boggle::DICTIONARY_FILE = "../../../EnglishWords.dat";
#else
const std::string Boggle::DICTIONARY_FILE = "EnglishWords.dat";
#endif
static const int g_NUM_CUBES = 16; // the number of cubes in the game
static const int g_CUBE_SIDES = 6; // the number of sides on each cube
static std::string g_CUBES[g_NUM_CUBES] =
{ // the letters on all 6 sides of every cube
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 std::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"};
std::array<std::string, g_NUM_ROLLED_CUBES> rollSides() {
// Intermediate array to shuffle
std::array<std::string, g_NUM_CUBES> cubes;
std::copy(std::begin(g_CUBES), std::end(g_CUBES), cubes.begin());
shuffle(cubes.data(), cubes.size());
// Just return the first g_NUM_ROLLED_CUBES (4pcs) entries
std::array<std::string, g_NUM_ROLLED_CUBES> ret;
std::copy(cubes.begin(), cubes.begin() + g_NUM_ROLLED_CUBES, ret.begin());
return ret;
}
Boggle::Boggle()
: m_showingSides(rollSides()), m_englishWords(Boggle::s_DICTIONARY_FILE) {}
bool Boggle::isWordEnglish(const std::string &word) const {
return m_englishWords.contains(word);
}
bool Boggle::isWordLongEnough(const std::string &word) const {
return word.length() >= s_MIN_WORD_LENGTH;
}
bool Boggle::isWordPlayed(const std::string &word) const {
return std::find(m_playedWords.begin(), m_playedWords.end(), word) !=
m_playedWords.end();
}
unsigned int Boggle::getPointsForWord(const std::string &word) {
// This is kept as a variable to avoid static_cast<int>
const int pointsForValidWord = word.length() - s_MIN_WORD_LENGTH;
return std::min(pointsForValidWord, 0);
}
bool Boggle::userInsert(std::string word){
bool existsAlready = std::find(m_playedWords.begin(), m_playedWords.end(), word) != m_playedWords.end();
if (existsAlready)
return false;
m_playedWords.push_back(std::move(word));
return true;
}
std::vector<std::string> Boggle::computerPlay() const {
std::unordered_set<std::string> visited;
std::vector<std::string> valiWords;
for (const std::string &cube : m_showingSides) {
visited.insert(cube);
std::string word;
for (const char c : word) {
word.push_back(c);
backtrack(valiWords, visited, word);
}
}
return valiWords;
}
void Boggle::backtrack(std::vector<std::string> &validWords,
std::unordered_set<string> &visited,
std::string &word) const {
if (!m_englishWords.containsPrefix(word))
return;
if (word.size() >= s_MIN_WORD_LENGTH && m_englishWords.contains(word))
validWords.push_back(word);
for (const std::string &neighbor : m_showingSides) {
bool neighborAlreadyVisited = visited.find(neighbor) != visited.end();
if (neighborAlreadyVisited)
continue;
visited.insert(word);
for (const char neighborChar : neighbor) {
word.push_back(neighborChar);
backtrack(validWords, visited, word);
word.pop_back();
}
}
}
// TODO: implement the members you declared in Boggle.h

View File

@ -9,48 +9,21 @@
#ifndef _boggle_h
#define _boggle_h
#include "lexicon.h"
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
static const int g_NUM_ROLLED_CUBES = 4;
class Boggle {
public:
static const std::string s_DICTIONARY_FILE;
static const int s_MIN_WORD_LENGTH = 4;
static const int s_BOARD_SIZE = 4;
static const std::string DICTIONARY_FILE;
const int MIN_WORD_LENGTH = 4;
const int BOARD_SIZE = 4;
Boggle();
bool isWordEnglish(const std::string &word) const;
bool isWordLongEnough(const std::string &word) const;
bool isWordPlayed(const std::string &word) const;
static unsigned int getPointsForWord(const std::string &word);
/**
* @brief userInsert saves the users word
* @return false if the word is already inserted
*/
bool userInsert(std::string word);
/**
* @brief computerPlay calculates all possible english words which can be
* formed from the Lexicon recursivly
* @return all possible words of the current board
*/
std::vector<std::string> computerPlay() const;
// TODO: decide the public member functions and declare them
private:
void backtrack(std::vector<std::string> &validWords,
std::unordered_set<string> &visited, std::string &word) const;
private:
// This is kept as a vector since there's max 16 words
std::vector<std::string> m_playedWords;
std::array<std::string, g_NUM_ROLLED_CUBES> m_showingSides;
Lexicon m_englishWords;
// TODO: decide the private member variables/functions and declare them
};
#endif

View File

@ -11,10 +11,11 @@
#include "Boggle.h"
#include <string>
using namespace std;
void intro();
void playOneGame(Boggle& boggle);
bool yesOrNo(std::string prompt);
bool yesOrNo(string prompt);
void clearConsole();
#endif

View File

@ -15,6 +15,7 @@
* 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)
}