-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cc
More file actions
82 lines (70 loc) · 2.66 KB
/
game.cc
File metadata and controls
82 lines (70 loc) · 2.66 KB
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
#include <dpp/dpp.h>
#include <iostream>
#include <random>
#include <utility>
#include <unordered_set>
#include <sstream>
#include <vector>
#include "game.h"
#include <cctype>
#include <string>
using namespace std;
unique_ptr<Dictionary> ReadDictionary(){
unordered_set <string> stringSet;
ifstream input_file("5LetterWords.txt");
for (string line; getline(input_file, line);) {
string x;
stringstream ss(line);
ss >> x;
stringSet.insert(x);
}
return make_unique <Dictionary> (stringSet);
}
void InitGame(std::unordered_map<dpp::snowflake, GameState> &games,const dpp::snowflake &user,const std::unique_ptr<Dictionary> &dictionary){
GameState g;
// pick a random word from the dictionary
std::mt19937 gen(std::random_device{}());
std::string element;
std::sample(dictionary->begin(), dictionary->end(), &element, 1, gen);
// update the word field of GameState with random word
g.word = element;
games[user] = g;
cout << element << " is the random word" << endl;
}
string Guess(dpp::snowflake user, string guess, std::unordered_map<dpp::snowflake, GameState> &games, const unique_ptr<Dictionary> &dictionary){
GameState g;
string winner;
if (games.find(user) != games.end()){
g = games.at(user);
}
string word = g.word;
if (guess.size() == 5){
if(dictionary->find(guess) != dictionary->end()){
for (int j = 0; j < 5; j++){
if(islower(guess[j])){ // check to see if lowercase letter
if(guess[j] == word[j]){
winner.append(":green_square: ");
} else if (word.find(guess[j]) != std::string::npos){ // find if letter in word
winner.append(":yellow_square: ");
} else{
winner.append(":red_square: ");
}
} else { // change uppercase letter to lowercase letter
char c = tolower(guess[j]);
if(c == word[j]){
winner.append(":green_square: ");
} else if (word.find(guess[j]) != std::string::npos){ // find if letter in word
winner.append(":yellow_square: ");
} else{
winner.append(":red_square: ");
}
}
}
} else {
winner = "The word was not in the dictionary :disappointed: Please enter a new word: ";
}
} else {
winner = "Sorry, your word was not the right length :disappointed: Please enter a new word: ";
}
return winner;
}