-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
247 lines (220 loc) · 4.9 KB
/
Game.cpp
File metadata and controls
247 lines (220 loc) · 4.9 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
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
#include "Game.h"
#include <iostream>
using namespace std;
Game::Game(OSHandler* oshandler, int bx, int by, int off)
{
os = oshandler;
base_x = bx;
base_y = by;
offset = off;
for (int i = 0; i != 8; ++i)
{
for (int j = 0; j != 8; ++j)
{
board[i][j] = 0;
}
}
//Delicious magic numbers! (Fuck you, don't judge me)
int playbutton = os->getcolour(bx + 55, by + 280);
if (abs(playbutton - 11438276) < 100)
{
//Ready state
state = 0;
} else {
//WTF!?
os->msgbox("Get to the 'Play Now' Screen or change some values");
state = -1;
}
time = 0;
//os->sleep(100);
}
void Game::play()
{
//Not in ready state - can't play
if (state != 0) return;
//Click play button and wait a bit
os->wait(500);
os->click(base_x + 55, base_y + 280);
os->wait(3500);
int time = os->gettime();
while (time - os->gettime() < 75)
{
//Get the cursor out the way (not sure if needed)
os->movecursor(0, 0);
//Get the current board
readboard();
printboard();
cout << endl;
//Find a move
int movex, movey;
bool moveup;
int maxscore = 0;
for (int x = 0; x != 8; ++x)
{
for (int y = 0; y != 8; ++ y)
{
int row1[6], row1len;
int row2[6], row2len;
int col1[6], col1len;
int col2[6], col2len;
int scoreup = 0, scoredown = 0;
//Swap vertical (always up, no need to check down swaps)
row1len = getrow(x, y-1, 1, row1);
row2len = getrow(x, y, -1, row2);
col1len = getcol(x, y, 0, col1);
scoreup += checkstrip(row1, row1len);
scoreup += checkstrip(row2, row2len);
scoreup += checkstrip(col1, col1len);
//Swap horizontal (always left)
row1len = getrow(x, y, 0, row1);
col1len = getcol(x-1, y, 1, col1);
col2len = getcol(x, y, -1, col2);
/*for (int i = 0; i != row1len; ++i)
{
cout << row1[i] << " ";
}
cout << endl;*/
scoredown += checkstrip(row1, row1len);
scoredown += checkstrip(col1, col1len);
scoredown += checkstrip(col2, col2len);
//Update max score
if (scoreup > maxscore || scoredown > maxscore)
{
movex = x;
movey = y;
moveup = (scoreup > scoredown);
maxscore = (moveup) ? scoreup : scoredown;
}
}
}
//Print it out for shits n giggles
if (moveup)
{
cout << "Best move: (" << movex << ", " << movey << ", ^) = " << maxscore;
} else {
cout << "Best move: (" << movex << ", " << movey << ", <) = " << maxscore;
}
cout << endl;
//Make the move
if (maxscore > 0)
{
os->click(base_x + (movex * offset), base_y + (movey * offset));
if (moveup)
{
os->click(base_x + (movex * offset), base_y + ((movey-1) * offset));
} else {
os->click(base_x + ((movex-1) * offset), base_y + (movey * offset));
}
}
//Wait for swap to finish
os->wait(500);
}
}
//void Game::makemove(int x, int y)
//{
//
//}
void Game::readboard()
{
for (int x = 0; x != 8; ++x)
{
for (int y = 0; y != 8; ++y)
{
board[x][y] = os->getgem(base_x + (y * offset), base_y + (x * offset));
}
}
}
void Game::printboard()
{
for (int x = 0; x != 8; ++x)
{
for (int y = 0; y != 8; ++y)
{
cout << board[x][y] << " ";
}
cout << endl;
}
}
//Check a strip of the board for any matching sequences
int Game::checkstrip(int strip[], int length)
{
int count = 1;
int max = 0;
int prev = -1;
for (int i = 0; i != length; ++i)
{
if (strip[i] == -1)
{
count = 1;
prev = -1;
continue;
} else if (strip[i] == prev) {
count++;
} else {
if (count > max) max = count;
count = 1;
}
prev = strip[i];
}
if (count > max) max = count;
if (max < 3) max = 0;
return max;
}
//Get a row of the board - without overflowing off edges
//dir = -1 for up, dir = 1 for down, dir = 0 for left
//Yes I realise that doesn't make any sense - fuck you
//Return value is length, and modifies row
int Game::getrow(int x, int y, int dir, int row[])
{
if (y+dir <= 0) return 0;
if (dir == 0 && x == 0) return 0;
int start = (dir==0) ? x-3 : x-2;
int end = x+3;
if (start < 0) start = 0;
if (end > 8) end = 8;
for (int i = start; i != end; ++i)
{
if (i == x)
{
row[i-start] = board[y+dir][i];
} else {
row[i-start] = board[y][i];
}
}
if (dir == 0)
{
int temp = row[x-start];
row[x-start] = row[x-start-1];
row[x-start-1] = temp;
}
return end-start;
}
//Get a column of the board - without overflowing off edges
//dir = -1 for left, dir = 1 for right, dir = 0 for up
//Yes I realise that doesn't make any sense - fuck you
//Return value is length, and modifies col
int Game::getcol(int x, int y, int dir, int col[])
{
if (x+dir <= 0) return 0;
if (dir == 0 && y == 0) return 0;
int start = (dir==0) ? y-3 : y-2;
int end = y+3;
if (start < 0) start = 0;
if (end > 8) end = 8;
for (int i = start; i != end; ++i)
{
if (i == y)
{
col[i-start] = board[i][x+dir];
} else {
col[i-start] = board[i][x];
}
}
if (dir == 0)
{
int temp = col[y-start];
col[y-start] = col[y-start-1];
col[y-start-1] = temp;
}
return end-start;
}