-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpus.cpp
265 lines (229 loc) · 6.9 KB
/
cpus.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "cpus.h"
#include <algorithm>
CPUS::CPUS(std::string const & name, size_t maxTiles):Player(name, maxTiles){
isCPUS = true;
isCPUL = false;
}
/*
Gets valid words from given characters in letters vector
*/
void CPUS::getWords(vector<char> letters,
std::set<std::string>& words, Dictionary& dict, char onBoard){
std::vector<std::string> permutations; //holds all permutations of letters
while(next_permutation(letters.begin(), letters.end())){ //gets perms
std::string str;
for(size_t x = 0; x < letters.size(); x++){
str += letters[x];
}
permutations.push_back(str);
}
//if the permutation of letters is a valid word, add it to the set
for(size_t x = 0; x < permutations.size(); x++){
for(size_t y = 0; y < letters.size()-1; y++){
if(dict.isLegalWord(permutations[x].substr(y,letters.size()))
&& (onBoard == '.' ||
permutations[x].find(onBoard) != std::string::npos)){
words.insert(permutations[x].substr(y,letters.size()));
}
}
}
}
/*
Gets the list of valid words from the hand
If there are blanks, it runs all possible additional letters
and checks if there are possible words formed
*/
std::vector<std::string> CPUS::playWord(std::set<Tile*> hand,
Square* square, int x, int y, Dictionary& dict){
std::vector<char> letters;
std::set<std::string> words;
char onBoard = '.';
if(square != nullptr){ //not starting move
onBoard = square->getLetter();
letters.push_back(onBoard);
}
int blanks = 0;
//pushes letters in hand into vector
for(std::set<Tile*>::iterator it = hand.begin(); it != hand.end(); ++it){
if((*it)->getLetter() < 123 && (*it)->getLetter() > 96){
letters.push_back((*it)->getLetter());
}
else{
blanks++;
}
}
getWords(letters, words, dict, onBoard); //gets legal words
if(blanks == 1){ //adds new possible words
for(int y = 0; y < 26; y++){
letters.push_back(97+y);
getWords(letters,words,dict,onBoard);
letters.pop_back();
}
}
if(blanks == 2){ //adds new possible words
for(int x = 0; x < 26; x++){
for(int y = 0; y < 26; y++){
letters.push_back(97+x);
letters.push_back(97+y);
getWords(letters,words,dict,onBoard);
letters.pop_back();
letters.pop_back();
}
}
}
std::vector<std::string> vec;
//creates the vector to be returned
for(std::set<std::string>::iterator it = words.begin(); it != words.end();
++it){
vec.push_back(*it);
}
return vec;
}
/*
Based on what valid words can be played, gets the best option and plays it
*/
void CPUS::findBestMove(Bag* bag, Board* board, Dictionary& dict){
std::pair<int, std::string> move;
//MAKE THIS MAP INT OF SCORE TO VECTOR OF MOVES
std::map<int, std::vector<std::string>> words;
std::set<int> points;
std::vector<int> sortedPoints;
int maxPoints = 0;
PlaceMove p = PlaceMove(0, 0, true, "zxz", this);
int blanks = 0;
//accounts for blanks
for(std::set<Tile*>::iterator it = hand.begin(); it != hand.end(); ++it){
if((*it)->getLetter() == '?'){
blanks++;
}
}
//contains the letters in the hand - to be used for accounting for blanks
std::set<char> handLetters;
for(std::set<Tile*>::iterator it = hand.begin(); it != hand.end(); ++it){
handLetters.insert((*it)->getLetter());
}
bool gotWords = false; //used to account for if it's the first move
for(size_t x = 1; x <= board->getRows(); x++){
for(size_t y = 1; y <= board->getColumns(); y++){
//creates the strings to actually be played
//so moves don't include letter already on the board
if(getFirstMove() || board->getSquare(x,y)->isOccupied()){
std::vector<std::string> holder;
holder = playWord(hand, board->getSquare(x,y), x, y, dict);
//adds the string to holder, puts into map based on length
for(size_t z = 0; z < holder.size(); z++){
if(blanks == 1){ //accounts for one blank
int index = -1;
for(size_t i = 0; i < holder[z].length(); i++){
if(handLetters.find(holder[z][i]) ==
handLetters.end()){
index = i;
}
}
if(index != -1 && index < (int)(holder[z].length())){
holder[z] = holder[z].substr(0,index) + '?' +
holder[z].substr(index);
}
}
if(blanks == 2){ //accounts for two blanks
int index1 = -1, index2 = -1;
for(size_t i = 0; i < holder[z].length(); i++){
if(handLetters.find(holder[z][i]) ==
handLetters.end() && index1 != -1){
index1 = i;
}
else if(handLetters.find(holder[z][i]) ==
handLetters.end()){
index2 = i;
}
}
if(index1 != -1 && index1 < (int)holder[z].length()
&& index2 != -1 &&
index2 < (int)holder[z].length()){
holder[z] = holder[z].substr(0,index1) + '?' +
holder[z].substr(index1, index2-index1) + '?' +
holder[z].substr(index2);
}
}
int point = 0;
size_t index = 0;
std::string str;
if(board->getSquare(x,y)->isOccupied()){
index = holder[z].find(board->getSquare(x,y)->getLetter());
str = holder[z].substr(0,index) +
holder[z].substr(index+1);
PlaceMove m1 = PlaceMove(x-index, y, true, str, this);
PlaceMove m2 = PlaceMove(x, y-index, false, str, this);
point = 0;
if(m1.tryPlace(*board, *bag, dict)){
int x = m1.getPoints(*board, *bag, dict);
point = x;
if(x > maxPoints){
maxPoints = x;
p = m1;
}
}
if(m2.tryPlace(*board, *bag, dict)){
int x = m2.getPoints(*board, *bag, dict);
if(x > point){
point = x;
}
if(x > maxPoints){
maxPoints = x;
p = m2;
}
}
}
else if(getFirstMove()){
str = holder[z];
words[point].push_back(str);
points.insert(point);
}
}
}
if(getFirstMove()){ //if the first move
gotWords = true;
break;
}
}
if(gotWords){ //if the first move
break;
}
}
if(p.tryPlace(*board, *bag, dict)){
p.execute(*board, *bag, dict);
return;
}
for(std::set<int>::iterator it = points.begin(); it != points.end();
++it){
sortedPoints.push_back(*it);
}
sort(sortedPoints.begin(), sortedPoints.end());
//used to account for issues that arose with the vector
size_t start = 0;
for(size_t x = 0; x < sortedPoints.size(); x++){
if(sortedPoints[x] < 0){
start++;
}
}
//if AI ends up needing to exchange tiles
std::string handForExchange = "";
for(std::set<Tile*>::iterator it = hand.begin(); it != hand.end(); ++it){
handForExchange += (*it)->getLetter();
}
if(getFirstMove()){ //accounts for if it's the first move of game
for(size_t i = sortedPoints.size()-1 - start; i >= 0; i--){
for(size_t z = 0; z < words[sortedPoints[i]].size(); z++){
PlaceMove m1 = PlaceMove(board->getStartX(), board->getStartY(),
true, words[sortedPoints[i]][z], this);
if(m1.tryPlace(*board, *bag, dict)){
m1.execute(*board, *bag, dict);
setFirstMove(false);
return;
}
}
}
}
ExchangeMove m = ExchangeMove(handForExchange, this);
m.execute(*board, *bag, dict);
}