-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
219 lines (201 loc) · 4.75 KB
/
main.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
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
// ldd: -lncurses
#include <ncurses.h>
#include <time.h>
#include <stdlib.h>
#define ROWS 4
#define COLS 4
int board[ROWS][COLS];
int score = 0;
typedef enum {
up = KEY_UP,
down = KEY_DOWN,
left = KEY_LEFT,
right = KEY_RIGHT,
quit = KEY_BACKSPACE,
} key;
void INIT_2048() {
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
srand(time(NULL));
}
void DONE_2048() {
endwin();
exit(0);
}
void init_board() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = 0;
}
}
}
void display_board() {
move(0, 0);
printw("== 2048 =============================\n========================= score:%5i\n", score);
for (int i = 0; i < ROWS; i++) {
printw("+---------+---------+---------+---------+\n");
for (int l = 0; l < 3; l++) {
if (l == 1) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] > 0) printw("|%5i ", board[i][j]);
else printw("| ");
}
printw("|\n");
} else printw("| | | | |\n");
}
}
printw("+---------+---------+---------+---------+\n");
refresh();
}
int count_empty() {
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) count++;
}
}
return count;
}
void add_two(int empty) {
int n = rand() % empty;
int k = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
if (k == n) {
board[i][j] = 2;
return;
}
k++;
}
}
}
}
int game_over(int add) {
int n = count_empty();
if (n == 0) {
printw("============= GAME OVER =============\n");
printw("=========== (press a key) ===========");
getch();
return 1;
} else if (add) {
add_two(n);
display_board();
}
return 0;
}
int shift_board() {
int hasChanged = 0;
int pos;
for (int i = 0; i < ROWS; i++) {
pos = 0;
for (int j = 0; j < COLS; j++) {
if (board[i][j]) {
if (j > pos) {
board[i][pos] = board[i][j];
board[i][j] = 0;
hasChanged = 1;
}
pos++;
}
}
}
return hasChanged;
}
int add_adjacents() {
int hasChanged = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS - 1; j++) {
if (board[i][j] && board[i][j] == board[i][j + 1]) {
board[i][j + 1] *= 2;
score += board[i][j + 1];
board[i][j] = 0;
j++; // inutile de regarder la prochaine case
hasChanged = 1;
}
}
}
return hasChanged;
}
int update_board() {
int hasChanged = 0;
hasChanged += shift_board();
hasChanged += add_adjacents();
hasChanged += shift_board();
return hasChanged > 0;
}
key get_key(){
int key;
do {
key = getch();
}
while(key != KEY_UP && key != KEY_DOWN && key != KEY_LEFT && key != KEY_RIGHT && key != KEY_BACKSPACE);
return key;
}
void swap(int* a, int* b){
int temp = *a;
*a = *b;
*b = temp;
}
void mirror_board() {
for (int i = 0; i < ROWS; i++) {
int left = 0;
int right = 3;
while (left < right) {
swap(&board[i][left], &board[i][right]);
left++;
right--;
}
}
}
void pivot_board() {
for (int i = 0; i < ROWS; i++) {
for (int j = i + 1; j < COLS; j++) {
swap(&board[i][j], &board[j][i]);
}
}
}
int play(key dir) {
int hasChanged = 0;
switch (dir) {
case up:
mirror_board();
pivot_board();
hasChanged += update_board();
pivot_board();
mirror_board();
break;
case down:
pivot_board();
mirror_board();
hasChanged += update_board();
mirror_board();
pivot_board();
break;
case left:
// Pas besoin de rotation pour la gauche
hasChanged += update_board();
break;
case right:
mirror_board();
hasChanged += update_board();
mirror_board();
break;
}
return hasChanged;
}
int main() {
INIT_2048();
init_board();
int add = 1;
while (!game_over(add)) {
key k = get_key();
if(k == quit) break;
if(play(k)){
display_board();
}
}
DONE_2048();
}