-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.c
126 lines (118 loc) · 3.11 KB
/
2048.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#define SIZE 4
int board[SIZE][SIZE];
void print() {
system("cls");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("+----");
}
printf("+\n");
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
printf("| ");
} else {
printf("|%4d", board[i][j]);
}
}
printf("|\n");
}
for (int j = 0; j < SIZE; j++) {
printf("+----");
}
printf("+\n");
}
void reset() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = 0;
}
}
}
void place() {
int numEmptyCells = 0;
int emptyCells[SIZE * SIZE][2];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
emptyCells[numEmptyCells][0] = i;
emptyCells[numEmptyCells][1] = j;
numEmptyCells++;
}
}
}
if (numEmptyCells > 0) {
int randomIndex = rand() % numEmptyCells;
int x = emptyCells[randomIndex][0];
int y = emptyCells[randomIndex][1];
board[x][y] = (rand() % 2 + 1) * 2; // place 2 or 4
}
}
void rotate(int count) {
int temp[SIZE][SIZE];
for (int k = 0; k < count; k++) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
temp[i][j] = board[i][j];
}
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = temp[SIZE - 1 - j][i];
}
}
}
}
int move(int direction) {
int changed = 0;
rotate(direction);
for (int i = 0; i < SIZE; i++) {
int current = 0;
int lastMerged = -1;
for (int j = 1; j < SIZE; j++) {
if (board[i][j] != 0) {
if (board[i][j] == board[i][current] && lastMerged != current) {
board[i][current] *= 2;
board[i][j] = 0;
lastMerged = current;
changed = 1;
} else if (board[i][current] == 0) {
board[i][current] = board[i][j];
board[i][j] = 0;
changed = 1;
} else if (++current && j != current) {
board[i][current] = board[i][j];
board[i][j] = 0;
changed = 1;
}
}
}
}
rotate(4 - direction);
return changed;
}
int main() {
int ch = 0;
srand(time(0));
while (ch != 27) {
ch = ch ? getch() : 63;
if (ch == 63) {
reset();
place();
place();
print();
} else if (ch == 224) {
char map[5] = {75, 80, 77, 72, 0};
char* found = strchr(map, getch());
if (found && move(found - map)) {
place();
print();
}
}
}
return 0;
}