-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cpp
More file actions
281 lines (233 loc) · 8.06 KB
/
Bot.cpp
File metadata and controls
281 lines (233 loc) · 8.06 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "Bot.h"
#include <algorithm>
#include <cstdlib>
#include <cmath>
Bot::Bot() {
}
Move Bot::findBestMove(const Board& board, const Tetromino& piece) {
Move bestMove;
// Try all possible rotations (0-3)
for (int rot = 0; rot < 4; ++rot) {
// Try all possible x positions
for (int x = -2; x < Board::WIDTH + 2; ++x) {
int score = evaluateBoard(board, piece, x, rot);
if (score > bestMove.score) {
bestMove = Move(x, rot, score);
}
}
}
return bestMove;
}
Move Bot::findBestMoveWithLookahead(const Board& board, const Tetromino& current, const Tetromino& next) {
Move bestMove;
// Try all possible rotations (0-3)
for (int rot = 0; rot < 4; ++rot) {
// Try all possible x positions
for (int x = -2; x < Board::WIDTH + 2; ++x) {
int score = evaluateBoardWithLookahead(board, current, x, rot, next);
if (score > bestMove.score) {
bestMove = Move(x, rot, score);
}
}
}
return bestMove;
}
int Bot::evaluateBoard(const Board& board, const Tetromino& piece, int x, int rotations) {
// Create a test piece and apply rotations
Tetromino testPiece(piece.getType());
for (int r = 0; r < rotations; ++r) {
testPiece.rotate();
}
testPiece.setX(x);
testPiece.setY(0);
// Check if this initial position is valid
if (!board.isValidRotation(testPiece)) {
return -999999;
}
// Find drop position
while (board.isValidMove(testPiece, 0, 1)) {
testPiece.move(0, 1);
}
// Check if final position is valid
if (!board.isValidRotation(testPiece)) {
return -999999;
}
// Simulate placement and evaluate
Board simulatedBoard = simulatePlacement(board, testPiece, x, rotations);
if (simulatedBoard.isGameOver()) {
return -999999;
}
// Heuristic weights - tuned for survival (based on Dellacherie algorithm)
const double LINE_CLEAR_WEIGHT = 760.0;
const double HOLE_WEIGHT = -356.0;
const double HEIGHT_WEIGHT = -51.0;
const double BUMPINESS_WEIGHT = -184.0;
const double MAX_HEIGHT_WEIGHT = -100.0;
const double ROW_TRANSITION_WEIGHT = -80.0;
const double COL_TRANSITION_WEIGHT = -50.0;
const double WELL_DEPTH_WEIGHT = -30.0;
int linesCleared = simulatedBoard.clearLines();
int holes = countHoles(simulatedBoard);
int aggregateHeight = getAggregateHeight(simulatedBoard);
int bumpiness = getBumpiness(simulatedBoard);
int maxHeight = getMaxHeight(simulatedBoard);
int rowTransitions = getRowTransitions(simulatedBoard);
int colTransitions = getColTransitions(simulatedBoard);
int wellDepth = getWellDepth(simulatedBoard);
// Heavy penalty for very high stacks
double heightPenalty = 0;
if (maxHeight > 15) {
heightPenalty = (maxHeight - 15) * 200;
}
double score = (linesCleared * LINE_CLEAR_WEIGHT) +
(holes * HOLE_WEIGHT) +
(aggregateHeight * HEIGHT_WEIGHT) +
(bumpiness * BUMPINESS_WEIGHT) +
(maxHeight * MAX_HEIGHT_WEIGHT) +
(rowTransitions * ROW_TRANSITION_WEIGHT) +
(colTransitions * COL_TRANSITION_WEIGHT) +
(wellDepth * WELL_DEPTH_WEIGHT) -
heightPenalty;
return static_cast<int>(score);
}
int Bot::evaluateBoardWithLookahead(const Board& board, const Tetromino& current, int x, int rot, const Tetromino& next) {
// First, evaluate the current move
int currentScore = evaluateBoard(board, current, x, rot);
if (currentScore < -900000) {
return currentScore; // Invalid move
}
// Simulate current piece placement
Tetromino currentPiece(current.getType());
for (int r = 0; r < rot; ++r) {
currentPiece.rotate();
}
currentPiece.setX(x);
currentPiece.setY(0);
while (board.isValidMove(currentPiece, 0, 1)) {
currentPiece.move(0, 1);
}
Board afterCurrent = simulatePlacement(board, currentPiece, x, rot);
if (afterCurrent.isGameOver()) {
return -999999;
}
// Now evaluate best move for next piece
Move bestNextMove;
for (int nextRot = 0; nextRot < 4; ++nextRot) {
for (int nextX = -2; nextX < Board::WIDTH + 2; ++nextX) {
int nextScore = evaluateBoard(afterCurrent, next, nextX, nextRot);
if (nextScore > bestNextMove.score) {
bestNextMove = Move(nextX, nextRot, nextScore);
}
}
}
// Combine scores: current move is primary, but consider next piece outcome
// Weight current move 70%, next piece potential 30%
return static_cast<int>(currentScore * 0.7 + bestNextMove.score * 0.3);
}
Board Bot::simulatePlacement(Board board, const Tetromino& piece, int x, int rotations) {
// Create a test piece at the starting position
Tetromino testPiece(piece.getType());
for (int r = 0; r < rotations; ++r) {
testPiece.rotate();
}
testPiece.setX(x);
testPiece.setY(0);
// Drop to bottom
while (board.isValidMove(testPiece, 0, 1)) {
testPiece.move(0, 1);
}
// Place the piece
board.placeTetromino(testPiece);
return board;
}
int Bot::countHoles(const Board& board) const {
const auto& grid = board.getGrid();
int holes = 0;
for (int x = 0; x < Board::WIDTH; ++x) {
bool foundBlock = false;
for (int y = 0; y < Board::HEIGHT; ++y) {
if (grid[y][x]) {
foundBlock = true;
} else if (foundBlock && !grid[y][x]) {
holes++;
}
}
}
return holes;
}
int Bot::getAggregateHeight(const Board& board) const {
int totalHeight = 0;
for (int x = 0; x < Board::WIDTH; ++x) {
totalHeight += getColumnHeight(board, x);
}
return totalHeight;
}
int Bot::getMaxHeight(const Board& board) const {
int maxHeight = 0;
for (int x = 0; x < Board::WIDTH; ++x) {
int h = getColumnHeight(board, x);
if (h > maxHeight) {
maxHeight = h;
}
}
return maxHeight;
}
int Bot::getBumpiness(const Board& board) const {
int bumpiness = 0;
int prevHeight = getColumnHeight(board, 0);
for (int x = 1; x < Board::WIDTH; ++x) {
int height = getColumnHeight(board, x);
bumpiness += abs(height - prevHeight);
prevHeight = height;
}
return bumpiness;
}
int Bot::getRowTransitions(const Board& board) const {
const auto& grid = board.getGrid();
int transitions = 0;
for (int y = 0; y < Board::HEIGHT; ++y) {
for (int x = 0; x < Board::WIDTH - 1; ++x) {
// Count transitions between filled and empty cells
if (grid[y][x] != grid[y][x+1]) {
transitions++;
}
}
}
return transitions;
}
int Bot::getColTransitions(const Board& board) const {
const auto& grid = board.getGrid();
int transitions = 0;
for (int x = 0; x < Board::WIDTH; ++x) {
for (int y = 0; y < Board::HEIGHT - 1; ++y) {
// Count transitions between filled and empty cells
if (grid[y][x] != grid[y+1][x]) {
transitions++;
}
}
}
return transitions;
}
int Bot::getWellDepth(const Board& board) const {
int wellDepth = 0;
for (int x = 0; x < Board::WIDTH; ++x) {
int colHeight = getColumnHeight(board, x);
int leftHeight = (x > 0) ? getColumnHeight(board, x - 1) : Board::HEIGHT;
int rightHeight = (x < Board::WIDTH - 1) ? getColumnHeight(board, x + 1) : Board::HEIGHT;
// A "well" is a column lower than both neighbors
int minNeighborHeight = std::min(leftHeight, rightHeight);
if (colHeight < minNeighborHeight) {
wellDepth += (minNeighborHeight - colHeight);
}
}
return wellDepth;
}
int Bot::getColumnHeight(const Board& board, int x) const {
const auto& grid = board.getGrid();
for (int y = 0; y < Board::HEIGHT; ++y) {
if (grid[y][x]) {
return Board::HEIGHT - y;
}
}
return 0;
}