-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoggle.cpp
More file actions
219 lines (199 loc) · 6.14 KB
/
Boggle.cpp
File metadata and controls
219 lines (199 loc) · 6.14 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <windows.h>
#include <cwchar>
#include <time.h>
#include <unordered_map>
#include <strings.h>
#include <vector>
#include <algorithm>
#include "EnglishWord.cpp"
#include "Board.cpp"
#include "Score.cpp"
using namespace std;
char** create_board(string shuffled){
char** board = new char*[5];
for(int i=0; i<5; i++)
{
board[i] = new char[5];
}
for(int j=0; j<5; j++)
{
for(int k=0; k<5; k++)
{
board[j][k] = shuffled[j*5+k];
}
}
return board;
}
bool** visited(){
bool** vis = new bool*[5];
for(int i=0; i<5; i++)
{
vis[i] = new bool[5];
}
for(int j=0; j<5; j++)
{
for(int k=0; k<5; k++)
{
vis[j][k] = false;
}
}
return vis;
}
bool isedge(int a, int b, int i, int j){ // a,b curr node and i,j is to be checked
if( max( abs(a-i), abs(b-j) )==1 )
{
return true;
}
return false;
}
unordered_map<char, vector<pair<int,int>>> create_map(string shuffled){
unordered_map<char, vector<pair<int,int>>> letter_map;
int character = 0;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
pair<int,int> coord;
coord.first = i;
coord.second = j;
if ( letter_map.count( shuffled[character] ) == 0 )
{
pair<char, vector<pair<int,int>>> p;
vector<pair<int,int>> v;
v.push_back(coord);
p.first = shuffled[character];
p.second = v;
letter_map.insert(p);
}else
{
letter_map[shuffled[character]].push_back(coord);
}
character++;
}
}
return letter_map;
}
bool in_board(char** board, unordered_map<char, vector<pair<int,int>>> wordmap, bool** vis, string word, int xx, int yy){
if( word.length() ==0 )
{
return true;
}
if( wordmap.count(word[0]) != 0 )
{
bool ans = false;
vector<pair<int,int>> v = wordmap[word[0]];
int n = v.size();
for(int j=0; j<n; j++)
{
int x = v[j].first, y = v[j].second;
if(!vis[x][y] && isedge(xx,yy,x,y))
{
vis[x][y] = true;
ans = in_board( board, wordmap, vis, word.substr(1), x, y );
if(ans)
{
return ans;
}
}
}
}
return false;
}
bool Boggle(char** board, unordered_map<char, vector<pair<int,int>>> wordmap, string word){
bool ans = false;
bool** vis = visited();
if(wordmap.count(word[0])!=0)
{
vector<pair<int,int>> v = wordmap[word[0]];
int n = v.size();
for(int j=0; j<n; j++)
{
int x = v[j].first, y = v[j].second;
if( !vis[x][y] )
{
vis[x][y] = true;
if( in_board( board, wordmap, vis, word.substr(1), x, y) )
{
return true;
}
}
}
}
return ans;
}
vector<string> play(string shuffled, vector<string> wordlist){
vector<string> valid;
char** board = create_board( shuffled );
unordered_map<char, vector<pair<int,int>>> char_map = create_map(shuffled);
int numwords = wordlist.size();
for(int i=0; i<numwords; i++)
{
if( isWord( wordlist[i] ) )
{
if( Boggle( board, char_map, wordlist[i] ) )
{
valid.push_back( wordlist[i] );
}
}
}
return valid;
}
int main()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 207);
instructions();
SetConsoleTextAttribute(hConsole, 224);
string generate = shuffle_string(); // string of shuffled letters generated
show_board(generate); // board is displayed
SetConsoleTextAttribute(hConsole, 240);
cout<<"\nTime remaining | Your words: "<<endl;
cout<<"(in seconds)\n"<<endl;
transform(generate.begin(), generate.end(), generate.begin(), ::tolower); // shuffled string is converted to lowercase
char** grid = create_board(generate); // 2D array of board is created
vector<string> input; // user input of words is taken from here
unordered_map<string, int> duplicate;
string s;
time_t start = time(NULL); // start time
time_t end = time(NULL) + 120; // end time
while(s != "0") // when user enters "0", input terminates
{
if(time(NULL) > end)
{
SetConsoleTextAttribute(hConsole, 116);
cout<<endl<<endl<<"YOUR TIME IS UP!!!"<<endl;
break;
}
int now = (int)(time(NULL) - start); // current time
cout<< (120 - now) <<"\t\t| ";
cin>>s;
if(time(NULL) > end)
{
SetConsoleTextAttribute(hConsole, 116);
cout<<endl<<endl<<"YOUR TIME IS UP!!!"<<endl;
break;
}
transform(s.begin(), s.end(), s.begin(), ::tolower);
if(duplicate.count(s)==0){ // removes duplicates entered by user
pair<string,int> p;
p.first = s;
p.second = 1;
duplicate.insert(p);
input.push_back(s);
}
}
vector<string> output = play(generate, input); // valid strings entered by user
SetConsoleTextAttribute(hConsole, 6);
cout<<"\nValid words entered by you are: ";
int n = output.size(); // valid user input words are displayed
for(int i=0; i<n; i++)
{
cout<<output[i]<<", ";
}
int score = calculatePoints(output); // points calculated
SetConsoleTextAttribute(hConsole, 2);
cout<<endl<<"\nYou scored "<<score<<" points!\n"<<endl; // points displayed
return 0;
}