-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
241 lines (208 loc) · 7.74 KB
/
main.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
/*
* File: main.cpp
* Author: Desmond
*
* Created on December 25, 2016, 5:08 PM
*/
//This somehow helps windows.h import correctly, do not remove
#define _WIN32_WINNT 0x0500
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
//Object that specifies where a word is located on the board
class wordPos{
public:
int startPos;
string word;
int position[16];
wordPos();
void operator=(const wordPos &rhs){
startPos = rhs.startPos;
word = rhs.word;
for(int i=0;i<16;i++)
position[i] = rhs.position[i];
}
};
wordPos::wordPos(void){
startPos=-1;
word=" ";
for(int i=0;i<16;i++)
position[i]=0;
}
//Given a word and a board, sees if it exists and returns wordPos
wordPos checkWord(string word, char board[16]);
//Helper function to check each letter in the word
int checkLetter(string word, char board[16], int searched[16], int letter, int position);
//Gets the (no bonus) score for a word
int getScore(string word);
int main(int argc, char** argv) {
//Variables
string line; //Generic string input
string wordArray[172820]=" "; //Stores the ENABLE list
char gameBoard[16]=" "; //Stores the board
int valid = 0; //Generic bool
wordPos found; //Stores found words
int score = 0; //Total score, used for board resets
int targetScore = 0; //Target score (0 for none)
//Store the ENABLE word list into an array
ifstream wordList ("enable1.txt");
if (wordList.is_open()){
int i=0;
while(getline(wordList,line)){
wordArray[i] = line;
i++;
}
wordList.close();
}
else
cout << "Unable to open file." << endl;
//Loop for inputting board states
while(1){
//Reset variables
valid = 0;
//Get input and initialize the board state
cout << "Target Score? (0 for none)" << endl;
cin >> targetScore;
cout << "Input Board State:" << endl;
while(!valid){
cin >> line;
if(line.size()==16 || line.size()==1)
valid=1;
if(!valid)
cout << "Incorrect Length." << endl;
}
//If user inputted a single character line, end program
if(line.size()==1)
break;
//Otherwise, initialize the board
for(int i=0;i<16;i++)
gameBoard[i] = line[i];
//Check all ENABLE words, draw them on the board
for(int i=0;i<172820;i++){
//Variables for word checking
line = wordArray[i];
found = checkWord(line,gameBoard);
//Variables for automated word entering
int xPos[16];
int yPos[16];
int wordPos, row, col;
INPUT input;
if(found.startPos!=-1){
cout << found.word << endl;
//Convert the wordPos to a series of mouse coordinates
for(int j=0;j<16;j++){
wordPos = found.position[j];
row = j/4;
col = j%4;
if(wordPos!=0){
xPos[found.position[j]-1] = 11050+3600*col;
yPos[found.position[j]-1] = 30500+6450*row;
}
}
//Draw the word on the board
for(int k=0;k<found.word.size();k++){
input.type=INPUT_MOUSE;
input.mi.dx=xPos[k];
input.mi.dy=yPos[k];
if(k==0)
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE|MOUSEEVENTF_LEFTDOWN);
else if(k==found.word.size()-1)
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE|MOUSEEVENTF_LEFTUP);
else
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE);
input.mi.mouseData=0;
input.mi.dwExtraInfo=NULL;
input.mi.time=0;
SendInput(1,&input,sizeof(INPUT));
Sleep(25);
}
//Add the word's score to the tally
score = score+getScore(found.word);
}
//If we have reached our target score, stop
if(targetScore!=0)
if(score>=targetScore)
break;
//If user presses "q", stop
if(GetAsyncKeyState(113) & 0x8000 )
break;
}
}
return 0;
}
wordPos checkWord(string word, char board[16]){
//Variables
int searched[16] = {0}; //Tracks location of the word
int startPos = 0; //Starting position of the word
wordPos found;
int check = 0;
while(startPos<16 && !check){
if(board[startPos]==word[0]){
check = checkLetter(word, board, searched, 0, startPos);
if(check){
found.startPos = startPos;
for(int i=0;i<16;i++)
found.position[i] = searched[i];
found.word = word;
}
}
startPos++;
}
return found;
}
int checkLetter(string word, char board[16], int searched[16], int letter, int position){
//Indicates whether we have found the word
int found = 0;
//Position in row and column of board
int row = position/4;
int col = position%4;
//This position is now searched (17 used as placeholder)
searched[position]=17;
if(letter==word.size()-1)
found=1;
else{
if(row!=0&&col!=0&&found==0)
if(board[position-5]==word[letter+1]&&searched[position-5]==0)
found = checkLetter(word,board,searched,letter+1,position-5);
if(row!=0&&col!=3&&found==0)
if(board[position-3]==word[letter+1]&&searched[position-3]==0)
found = checkLetter(word,board,searched,letter+1,position-3);
if(row!=3&&col!=0&&found==0)
if(board[position+3]==word[letter+1]&&searched[position+3]==0)
found = checkLetter(word,board,searched,letter+1,position+3);
if(row!=3&&col!=3&&found==0)
if(board[position+5]==word[letter+1]&&searched[position+5]==0)
found = checkLetter(word,board,searched,letter+1,position+5);
if(row!=0&&found==0)
if(board[position-4]==word[letter+1]&&searched[position-4]==0)
found = checkLetter(word,board,searched,letter+1,position-4);
if(row!=3&&found==0)
if(board[position+4]==word[letter+1]&&searched[position+4]==0)
found = checkLetter(word,board,searched,letter+1,position+4);
if(col!=0&&found==0)
if(board[position-1]==word[letter+1]&&searched[position-1]==0)
found = checkLetter(word,board,searched,letter+1,position-1);
if(col!=3&&found==0)
if(board[position+1]==word[letter+1]&&searched[position+1]==0)
found = checkLetter(word,board,searched,letter+1,position+1);
}
//If we found the word, add this position to it
if(found)
searched[position]=letter+1;
return found;
}
int getScore(string word){
int score = 0;
int letter = 0;
int letterScores[26] =
{1,4,4,2,1,4,3,3,1,10,5,2,4,2,1,4,10,1,1,1,2,5,4,8,3,10};
for(int i=0;i<word.size();i++){
letter = (int)word[i];
letter = letter-97; //a=0, b=1, c=2, etc.
score += letterScores[letter];
}
return score;
}