simple utils

This commit is contained in:
Love 2024-09-12 14:33:29 +02:00
parent ae0512820f
commit 538a837639
2 changed files with 19 additions and 15 deletions

View File

@ -10,8 +10,8 @@
#include "random.h" #include "random.h"
#include "shuffle.h" #include "shuffle.h"
#include "strlib.h" #include "strlib.h"
#include <sstream>
#include <fstream> #include <fstream>
#include <sstream>
// The res folder isn't copied the same way on macOS // The res folder isn't copied the same way on macOS
#ifdef __APPLE__ #ifdef __APPLE__
@ -20,15 +20,16 @@ const std::string Boggle::DICTIONARY_FILE = "../../../EnglishWords.dat";
const std::string Boggle::DICTIONARY_FILE = "EnglishWords.dat"; const std::string Boggle::DICTIONARY_FILE = "EnglishWords.dat";
#endif #endif
static const int NUM_CUBES = 16; // the number of cubes in the game 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 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 static std::string CUBES[NUM_CUBES] =
{ // the letters on all 6 sides of every cube
"AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS", "AOOTTW", "CIMOTU", "AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS", "AOOTTW", "CIMOTU",
"DEILRX", "DELRVY", "DISTTY", "EEGHNW", "EEINSU", "EHRTVW", "DEILRX", "DELRVY", "DISTTY", "EEGHNW", "EEINSU", "EHRTVW",
"EIOSST", "ELRTTY", "HIMNQU", "HLNNRZ"}; "EIOSST", "ELRTTY", "HIMNQU", "HLNNRZ"};
static const int NUM_ROLLED_CUBES = 4; static const int NUM_ROLLED_CUBES = 4;
std::array<std::string, NUM_ROLLED_CUBES> rollSides() { std::array<std::string, NUM_ROLLED_CUBES> rollSides() {
// Intermediate array to shuffle // Intermediate array to shuffle
std::array<std::string, NUM_CUBES> cubes; std::array<std::string, NUM_CUBES> cubes;
std::copy(std::begin(CUBES), std::end(CUBES), cubes.begin()); std::copy(std::begin(CUBES), std::end(CUBES), cubes.begin());
@ -41,8 +42,13 @@ std::array<std::string, NUM_ROLLED_CUBES> rollSides() {
} }
Boggle::Boggle() Boggle::Boggle()
: showingSides(rollSides()), englishWords(Boggle::DICTIONARY_FILE) {} : m_showingSides(rollSides()), m_englishWords(Boggle::DICTIONARY_FILE) {}
bool Boggle::insertWordIfValid() {
bool Boggle::isWordValid(const std::string &word) const {
return m_englishWords.contains(word);
}
bool Boggle::isWordPlayed(const string &word) const {
return std::find(m_playedWords.begin(), m_playedWords.end(), word) !=
m_playedWords.end();
} }

View File

@ -22,14 +22,12 @@ public:
Boggle(); Boggle();
/** bool isWordValid(const std::string &word) const;
* @return true if the word read was valid, false if not bool isWordPlayed(const std::string &word) const;
*/
bool insertWordIfValid();
private: private:
std::vector<std::string> userWords; std::vector<std::string> m_playedWords;
std::array<std::string, 4> showingSides; std::array<std::string, 4> m_showingSides;
Lexicon englishWords; Lexicon m_englishWords;
}; };