From 5eb7c59019fdf573a8188ae688065541875a6f6b Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Thu, 12 Sep 2024 14:33:29 +0200 Subject: [PATCH] simple utils --- src/Boggle.cpp | 22 ++++++++++++++-------- src/Boggle.h | 12 +++++------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Boggle.cpp b/src/Boggle.cpp index dffb816..e21ecd2 100755 --- a/src/Boggle.cpp +++ b/src/Boggle.cpp @@ -10,8 +10,8 @@ #include "random.h" #include "shuffle.h" #include "strlib.h" -#include #include +#include // The res folder isn't copied the same way on macOS #ifdef __APPLE__ @@ -20,15 +20,16 @@ const std::string Boggle::DICTIONARY_FILE = "../../../EnglishWords.dat"; const std::string Boggle::DICTIONARY_FILE = "EnglishWords.dat"; #endif -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 +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"}; static const int NUM_ROLLED_CUBES = 4; -std::array rollSides() { +std::array rollSides() { // Intermediate array to shuffle std::array cubes; std::copy(std::begin(CUBES), std::end(CUBES), cubes.begin()); @@ -41,8 +42,13 @@ std::array rollSides() { } Boggle::Boggle() - : showingSides(rollSides()), englishWords(Boggle::DICTIONARY_FILE) {} - -bool Boggle::insertWordIfValid() { + : m_showingSides(rollSides()), m_englishWords(Boggle::DICTIONARY_FILE) {} +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(); } diff --git a/src/Boggle.h b/src/Boggle.h index 7909608..2f363f0 100755 --- a/src/Boggle.h +++ b/src/Boggle.h @@ -22,14 +22,12 @@ public: Boggle(); - /** - * @return true if the word read was valid, false if not - */ - bool insertWordIfValid(); + bool isWordValid(const std::string &word) const; + bool isWordPlayed(const std::string &word) const; private: - std::vector userWords; - std::array showingSides; - Lexicon englishWords; + std::vector m_playedWords; + std::array m_showingSides; + Lexicon m_englishWords; };