-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuffle.cc
96 lines (91 loc) · 2.75 KB
/
tuffle.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "tuffle.h"
std::string DoCheck(std::string guess, std::string answer) {
std::string wordcolors = "BBBBB";
for (int i = 0; i < answer.size(); i++) {
if (guess[i] == answer[i]) {
wordcolors[i] = 'G';
answer[i] = '-';
}
}
for (int i = 0; i < answer.size(); i++) {
if (answer.find(guess[i]) != -1) {
int index = answer.find(guess[i]);
if (wordcolors[i] != 'G') {
wordcolors[i] = 'Y';
answer[index] = '-';
}
}
}
return wordcolors;
}
void TuffleGame::NewGame() {
game_state_ = GameState(dictionary_.GetRandomTuffle());
game_state_.SetGameStatus("active");
}
void TuffleGame::LetterKeyPressed(char key) {
if (game_state_.GetGameStatus() != "active") {
return;
}
game_state_.SetError("");
std::vector<std::string> guesses = game_state_.GetGuessedWords();
if (guesses.size() == 0) {
guesses.push_back("");
}
int index = guesses.size() - 1;
std::string guess = guesses.at(index);
if (guess.size() != 5) {
guesses.at(index) = guess + key;
game_state_.SetGuessedWords(guesses);
}
}
void TuffleGame::EnterKeyPressed() {
if (game_state_.GetGameStatus() != "active") {
return;
}
std::vector<std::string> guesses = game_state_.GetGuessedWords();
int index = guesses.size() - 1;
std::string guess = guesses.at(index);
if (guess.size() < 5) {
game_state_.SetError("Please enter a word with 5 characters.");
return;
}
if (!dictionary_.IsValidGuess(guess)) {
game_state_.SetError("Please enter a valid word.");
return;
}
std::vector<std::string> boardcolors = game_state_.GetBoardColors();
std::string color = DoCheck(guess, game_state_.GetAnswer());
boardcolors.push_back(color);
game_state_.SetBoardColors(boardcolors);
if (color == "GGGGG") {
game_state_.SetGameStatus("win");
} else if (guesses.size() > 5) {
game_state_.SetGameStatus("lose");
} else {
guesses.push_back("");
game_state_.SetGuessedWords(guesses);
}
}
void TuffleGame::DeleteKeyPressed() {
if (game_state_.GetGameStatus() != "active") {
return;
}
std::vector<std::string> guesses = game_state_.GetGuessedWords();
int index = guesses.size() - 1;
std::string guess = guesses.at(index);
if (guess.size() == 0) {
return;
}
guess.pop_back();
guesses.at(index) = guess;
game_state_.SetGuessedWords(guesses);
}
crow::json::wvalue TuffleGame::GameStateInJson() {
crow::json::wvalue game_state_json({});
game_state_json["answer"] = game_state_.GetAnswer();
game_state_json["boardColors"] = game_state_.GetBoardColors();
game_state_json["guessedWords"] = game_state_.GetGuessedWords();
game_state_json["gameStatus"] = game_state_.GetGameStatus();
game_state_json["errorMessage"] = game_state_.GetErrorMsg();
return game_state_json;
}