-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.php
More file actions
282 lines (233 loc) · 5.95 KB
/
driver.php
File metadata and controls
282 lines (233 loc) · 5.95 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
282
<?php
function helpPage() {
printf("Enjoy to play 2048!\n\n");
printf("Click Enter to start . . .\n\n");
$input = trim(fgets(STDIN));
}
// Function to initialize the game board
function initializeBoard($size)
{
$board = array();
for ($i = 0; $i < $size; $i++) {
$board[$i] = array_fill(0, $size, 0);
}
addRandomTile($board);
addRandomTile($board);
return $board;
}
// Function to add a random tile (either 2 or 4) to the board
function addRandomTile(&$board)
{
$emptyCells = getEmptyCells($board);
if (count($emptyCells) > 0) {
$randomIndex = array_rand($emptyCells);
$randomCell = $emptyCells[$randomIndex];
$value = (rand(0, 1) == 0) ? 2 : 4;
$board[$randomCell['row']][$randomCell['col']] = $value;
}
}
// Function to get all empty cells on the board
function getEmptyCells($board)
{
$emptyCells = array();
foreach ($board as $row => $rowData) {
foreach ($rowData as $col => $value) {
if ($value == 0) {
$emptyCells[] = array('row' => $row, 'col' => $col);
}
}
}
return $emptyCells;
}
// Function to display the game board
function displayBoard($board)
{
echo "----------------------\n";
foreach ($board as $row) {
echo "|";
foreach ($row as $cell) {
if ($cell == 0) {
echo " |";
} else {
printf("%4d|", $cell);
}
}
echo "\n----------------------\n";
}
}
// Function to move tiles in a specified direction
function moveTiles(&$board, $direction)
{
echo "switch menu:\n\n";
echo $direction . "\n\n";
switch ($direction) {
case 'left':
case 'a':
moveLeft($board);
break;
case 'right':
case 'd':
moveRight($board);
break;
case 'up':
case 'w':
echo "up!\n\n";
moveUp($board);
break;
case 'down':
case 's' :
moveDown($board);
break;
default:
echo "Invalid direction\n";
}
addRandomTile($board);
}
// Function to move tiles to the left
function moveLeft(&$board)
{
foreach ($board as &$row) {
$row = slideLeft($row);
$row = mergeTiles($row);
$row = slideLeft($row);
}
}
// Function to slide tiles to the left
function slideLeft($row)
{
//echo "im here!\n\n";
$row = array_filter($row, function ($value) {
return $value != 0;
});
$numEmptyCells = 4 - count($row);
$row = array_merge($row, array_fill(0, $numEmptyCells, 0));
return $row;
}
// Function to move tiles to the right
function moveRight(&$board)
{
foreach ($board as &$row) {
//$row = array_reverse($row);
$row = slideRight($row);
$row = mergeTiles($row);
$row = slideRight($row);
//$row = array_reverse($row);
}
}
// Function to slide tiles to the right
function slideRight($row)
{
$row = array_reverse($row);
$row = slideLeft($row);
$row = array_reverse($row);
return $row;
}
// Function to move tiles up
function moveUp(&$board)
{
$transposedBoard = transposeBoard($board);
moveLeft($transposedBoard);
$board = transposeBoard($transposedBoard);
}
// Function to move tiles down
function moveDown(&$board)
{
$transposedBoard = transposeBoard($board);
moveRight($transposedBoard);
$board = transposeBoard($transposedBoard);
}
// Function to merge adjacent tiles with the same value
function mergeTiles($row)
{
for ($i = 0; $i < count($row) - 1; $i++) {
if ($row[$i] == $row[$i + 1] && $row[$i] != 0) {
$row[$i] *= 2;
$row[$i + 1] = 0;
}
}
return $row;
}
// Function to transpose the game board
//map
function transposeBoard($board)
{
$transposedBoard = array();
for ($i = 0; $i < count($board); $i++) {
for ($j = 0; $j < count($board[$i]); $j++) {
$transposedBoard[$j][$i] = $board[$i][$j];
}
}
return $transposedBoard;
}
// Function to get user input for the next move
function getUserMove()
{
echo "Enter your move (left - a, right - d, up - w, down - s): ";
$input = trim(fgets(STDIN));
return strtolower($input);
}
// Function to check if the game is over
function isGameOver($board)
{
// Check if there are any empty cells
if (count(getEmptyCells($board)) > 0) {
return false;
}
// Check if there are any adjacent tiles with the same value
for ($i = 0; $i < count($board); $i++) {
for ($j = 0; $j < count($board[$i]) - 1; $j++) {
if ($board[$i][$j] == $board[$i][$j + 1]) {
return false;
}
}
}
for ($i = 0; $i < count($board) - 1; $i++) {
for ($j = 0; $j < count($board[$i]); $j++) {
if ($board[$i][$j] == $board[$i + 1][$j]) {
return false;
}
}
}
// If no empty cells and no adjacent tiles with the same value, the game is over
return true;
}
// Function to check if the player has won
function hasPlayerWon($board)
{
foreach ($board as $row) {
foreach ($row as $cell) {
if ($cell == 2048) {
return true;
}
}
}
return false;
}
// Example usage:
// Set the board size (e.g., 4x4)
$boardSize = 4;
// Initialize the game board
$board = initializeBoard($boardSize);
helpPage();
// Display the initial board
displayBoard($board);
// Game loop
while (true) {
// Get user input for the next move
$direction = getUserMove();
// Move tiles based on user input
moveTiles($board, $direction);
// Display the updated board
displayBoard($board);
// Check for game over
if (isGameOver($board)) {
echo "Game Over! No more valid moves.\n";
break;
}
// Check for a win condition
if (hasPlayerWon($board)) {
echo "Congratulations! You won!\n";
break;
}
}
?>