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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Game.h"
#include <AK/Array.h>
#include <AK/NumericLimits.h>
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <stdlib.h>
Game::Game(size_t grid_size, size_t target_tile, bool evil_ai)
: m_grid_size(grid_size)
, m_evil_ai(evil_ai)
{
if (target_tile == 0)
m_target_tile = 2048;
else if ((target_tile & (target_tile - 1)) != 0)
m_target_tile = 1 << max_power_for_board(grid_size);
else
m_target_tile = target_tile;
m_board.m_tiles.resize(grid_size);
for (auto& row : m_board.m_tiles) {
row.ensure_capacity(grid_size);
for (size_t i = 0; i < grid_size; i++)
row.append(0);
}
add_tile();
add_tile();
}
void Game::add_random_tile()
{
int row;
int column;
do {
row = rand() % m_grid_size;
column = rand() % m_grid_size;
} while (m_board.m_tiles[row][column] != 0);
size_t value = rand() < RAND_MAX * 0.9 ? 2 : 4;
m_board.add_tile(row, column, value);
}
void Game::Board::transpose()
{
for (size_t i = 1; i < m_tiles.size(); ++i) {
for (size_t j = 0; j < i; j++)
swap(m_tiles[i][j], m_tiles[j][i]);
}
for (auto& t : m_sliding_tiles) {
swap(t.row_from, t.column_from);
swap(t.row_to, t.column_to);
}
}
void Game::Board::reverse()
{
for (auto& row : m_tiles) {
for (size_t i = 0; i < row.size() / 2; ++i)
swap(row[i], row[row.size() - i - 1]);
}
auto const row_size = m_tiles[0].size();
for (auto& t : m_sliding_tiles) {
t.column_from = row_size - t.column_from - 1;
t.column_to = row_size - t.column_to - 1;
}
}
size_t Game::Board::slide_row(size_t row_index)
{
Game::Board::Row& row = m_tiles[row_index];
size_t successful_merge_score = 0;
auto next_nonempty = [&](size_t start_index) {
size_t next = start_index;
for (; next < row.size(); next++) {
if (row[next] != 0)
break;
}
return next;
};
size_t current_index = 0;
size_t first = next_nonempty(0);
if (first == row.size()) // empty row
return 0;
while (first < row.size()) {
auto second = next_nonempty(first + 1);
if (second == row.size() || row[first] != row[second]) {
m_sliding_tiles.append({ row_index, first, row[first], row_index, current_index, row[first] });
row[current_index] = row[first];
current_index++;
first = second;
} else {
VERIFY(row[first] == row[second]);
m_sliding_tiles.append({ row_index, first, row[first], row_index, current_index, 2 * row[first] });
m_sliding_tiles.append({ row_index, second, row[second], row_index, current_index, 2 * row[first] });
row[current_index] = 2 * row[first];
current_index++;
successful_merge_score += 2 * row[first];
first = next_nonempty(second + 1);
}
}
for (; current_index < row.size(); current_index++)
row[current_index] = 0;
return successful_merge_score;
}
size_t Game::Board::slide_left()
{
m_sliding_tiles.clear();
size_t successful_merge_score = 0;
for (size_t row_index = 0; row_index < m_tiles.size(); row_index++)
successful_merge_score += slide_row(row_index);
return successful_merge_score;
}
static bool is_complete(Game::Board const& board, size_t target)
{
for (auto& row : board.tiles()) {
if (row.contains_slow(target))
return true;
}
return false;
}
static bool has_no_neighbors(Span<u32 const> const& row)
{
if (row.size() < 2)
return true;
auto x = row[0];
auto y = row[1];
if (x == y)
return false;
return has_no_neighbors(row.slice(1, row.size() - 1));
};
bool Game::Board::is_stalled()
{
static auto stalled = [](auto& row) {
return !row.contains_slow(0) && has_no_neighbors(row.span());
};
for (auto& row : m_tiles)
if (!stalled(row))
return false;
transpose();
auto scope_guard = ScopeGuard([&]() { transpose(); });
for (auto& row : m_tiles)
if (!stalled(row))
return false;
return true;
}
static size_t get_number_of_free_cells(Game::Board const& board)
{
size_t accumulator = 0;
for (auto& row : board.tiles()) {
for (auto& cell : row)
accumulator += cell == 0;
}
return accumulator;
}
Game::Board::SlideResult Game::Board::slide_tiles(Direction direction)
{
size_t successful_merge_score = 0;
switch (direction) {
case Direction::Left:
successful_merge_score = slide_left();
break;
case Direction::Right:
reverse();
successful_merge_score = slide_left();
reverse();
break;
case Direction::Up:
transpose();
successful_merge_score = slide_left();
transpose();
break;
case Direction::Down:
transpose();
reverse();
successful_merge_score = slide_left();
reverse();
transpose();
break;
}
bool moved = false;
for (auto& t : m_sliding_tiles) {
if (t.row_from != t.row_to || t.column_from != t.column_to)
moved = true;
}
return { moved, successful_merge_score };
}
Game::MoveOutcome Game::attempt_move(Direction direction)
{
auto [moved, successful_merge_score] = m_board.slide_tiles(direction);
if (moved) {
m_turns++;
m_score += successful_merge_score;
add_tile();
}
if (is_complete(m_board, m_target_tile) && !m_want_to_continue)
return MoveOutcome::Won;
if (m_board.is_stalled())
return MoveOutcome::GameOver;
if (moved)
return MoveOutcome::OK;
return MoveOutcome::InvalidMove;
}
void Game::add_evil_tile()
{
size_t worst_row = 0;
size_t worst_column = 0;
u32 worst_value = 2;
size_t most_free_cells = NumericLimits<size_t>::max();
size_t worst_score = NumericLimits<size_t>::max();
for (size_t row = 0; row < m_grid_size; row++) {
for (size_t column = 0; column < m_grid_size; column++) {
if (m_board.m_tiles[row][column] != 0)
continue;
for (u32 value : Array { 2, 4 }) {
Game saved_state = *this;
saved_state.m_board.m_tiles[row][column] = value;
if (saved_state.m_board.is_stalled()) {
// We can stall the board now, instant game over.
worst_row = row;
worst_column = column;
worst_value = value;
goto found_worst_tile;
}
// These are the best outcome and score the player can achieve in one move.
// We want this to be as low as possible.
size_t best_outcome = 0;
size_t best_score = 0;
for (auto direction : Array { Direction::Down, Direction::Left, Direction::Right, Direction::Up }) {
Game moved_state = saved_state;
auto [moved, score_delta] = moved_state.m_board.slide_tiles(direction);
if (!moved) // invalid move
continue;
best_outcome = max(best_outcome, get_number_of_free_cells(moved_state.board()));
best_score = max(best_score, score_delta);
}
// We already know a worse cell placement; discard.
if (best_outcome > most_free_cells)
continue;
// This tile is the same as the worst we know in terms of board population,
// but the player can achieve the same or better score; discard.
if (best_outcome == most_free_cells && best_score >= worst_score)
continue;
worst_row = row;
worst_column = column;
worst_value = value;
most_free_cells = best_outcome;
worst_score = best_score;
}
}
}
found_worst_tile:
m_board.add_tile(worst_row, worst_column, worst_value);
}
u32 Game::largest_tile() const
{
u32 tile = 0;
for (auto& row : m_board.m_tiles) {
for (auto& cell : row)
tile = max(tile, cell);
}
return tile;
}
|