Computer algorithm
This commit is contained in:
parent
50a34e88e4
commit
5c1e5fabd1
@ -12,6 +12,7 @@
|
|||||||
#include "strlib.h"
|
#include "strlib.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
// 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__
|
||||||
@ -61,3 +62,42 @@ unsigned int Boggle::getPointsForWord(const std::string &word) {
|
|||||||
const int pointsForValidWord = word.length() - s_MIN_WORD_LENGTH;
|
const int pointsForValidWord = word.length() - s_MIN_WORD_LENGTH;
|
||||||
return std::min(pointsForValidWord, 0);
|
return std::min(pointsForValidWord, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
11
src/Boggle.h
11
src/Boggle.h
@ -12,6 +12,7 @@
|
|||||||
#include "lexicon.h"
|
#include "lexicon.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
static const int g_NUM_ROLLED_CUBES = 4;
|
static const int g_NUM_ROLLED_CUBES = 4;
|
||||||
@ -28,6 +29,16 @@ public:
|
|||||||
bool isWordLongEnough(const std::string &word) const;
|
bool isWordLongEnough(const std::string &word) const;
|
||||||
bool isWordPlayed(const std::string &word) const;
|
bool isWordPlayed(const std::string &word) const;
|
||||||
static unsigned int getPointsForWord(const std::string &word);
|
static unsigned int getPointsForWord(const 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;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void backtrack(std::vector<std::string> &validWords,
|
||||||
|
std::unordered_set<string> &visited, std::string &word) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> m_playedWords;
|
std::vector<std::string> m_playedWords;
|
||||||
|
Loading…
Reference in New Issue
Block a user