-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
310 lines (285 loc) · 6.51 KB
/
game.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
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
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <signal.h>
void setBufferedInput(bool);
void signal_callback_handler(int);
void generate_grid();
void show_grid();
void show_point_moves();
void rotate_grid();
int move_grid();
void move_right();
void move_down();
void move_up();
void move_left();
void add_random();
int check_loose();
int check_win();
int grid[4][4];
// int grid[4][4]= {{2,4,16,32},
// {4,16,1024,2},
// {16,32,5121,4},
// {2,4,2,32}};
int points = 0;
int total_moves = 0;
int moved = 0;
int main(){
signal(SIGINT, signal_callback_handler);
setBufferedInput(false);
printf("\033[2J\033[H" );
generate_grid();
show_point_moves();
printf("\n");
show_grid();
printf("'w, a, s, d' to control and 'q' to quit\n");
char c;
while (true) {
if(check_win()){
printf("\n");
printf(" Congratulations You Won!\n");
printf(" Points: %d | Moves: %d\n", points, total_moves);
break;
}
if(check_loose()){
printf("\n");
printf(" You Lost, Try Again Next Time.\n");
printf(" Points: %d | Moves: %d\n", points, total_moves);
break;
}
c=getchar();
if(c == 'w'){
move_up();
}
if(c == 'a'){
move_left();
}
if(c == 's'){
move_down();
}
if (c == 'd'){
move_right();
}
if(c == 'q'){
printf("ending the game\n");
break;
}
printf("\033[2J\033[H" );
show_point_moves();
printf("\n");
show_grid();
printf("'w, a, s, d' to control and 'q' to quit\n");
}
setBufferedInput(true);
}
void move_right(){
if(move_grid()){
add_random();
add_random();
}
}
void move_left(){
rotate_grid();
rotate_grid();
if(move_grid()){
add_random();
add_random();
}
rotate_grid();
rotate_grid();
}
void move_down(){
rotate_grid();
if(move_grid()){
add_random();
add_random();
}
rotate_grid();
rotate_grid();
rotate_grid();
}
void move_up(){
rotate_grid();
rotate_grid();
rotate_grid();
if(move_grid()){
add_random();
add_random();
}
rotate_grid();
}
void generate_grid(){
// generate empty grid
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
grid[i][j] = 0;
}
}
add_random();
add_random();
}
void add_random(){
srand((unsigned)time(NULL));
int cnt_empty = 0;
int empty_place[16][2];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(grid[i][j] == 0){
empty_place[cnt_empty][0] = i;
empty_place[cnt_empty][1] = j;
cnt_empty++;
}
}
}
int r1, c1, value1;
// assign value
if(rand() % 2){
value1 = 4;
}else{
value1 = 2;
}
if(cnt_empty > 0){
int temp = rand() % cnt_empty;
r1 = empty_place[temp][0];
c1 = empty_place[temp][1];
grid[r1][c1] = value1;
}
}
void show_grid(){
for(int i = 0; i < 4; i++){
printf(" |------|------|------|------|\n");
printf("\n");
for(int j = 0; j < 4; j++){
printf("%*d", 7, grid[i][j]);
}
printf("\n");
printf("\n");
}
printf(" |------|------|------|------|\n");
printf("\n");
}
void show_point_moves(){
printf(" Points: %d %*s%d\n", points, 18,"Moves: ", total_moves);
}
/*
idea from https://www.geeksforgeeks.org/inplace-rotate-square-matrix-by-90-degrees/
*/
void rotate_grid(){
int temp = 0;
int k = 4; //size of grid
for (int i=0; i<k; i++) {
for (int j=i; j<k-i-1; j++) {
temp = grid[i][j];
grid[i][j] = grid[j][k-i-1];
grid[j][k-i-1] = grid[k-i-1][k-j-1];
grid[k-i-1][k-j-1] = grid[k-j-1][i];
grid[k-j-1][i] = temp;
}
}
}
int move_grid(){ //considering moving right is default
int current, next;
bool moved = false; //used to check if the val has moved
for(int r = 0; r < 4; r++){
for(int c = 3; c > -1; c--){
if(grid[r][c] != 0){
current = c;
next = c + 1;
// move until end or next val is not 0
while(next < 4 && grid[r][next] == 0){
grid[r][next] = grid[r][current];
grid[r][current] = 0;
current += 1;
next += 1;
moved = true;
}
// add when next val is same as current val
if(next < 4 && (grid[r][current] == grid[r][next])){
grid[r][next] *= 2;
grid[r][current] = 0;
moved = true;
points += grid[r][next];
}
}
}
}
total_moves++;
return moved;
}
// check if the player lost the game
int check_loose(){
bool condi = true;
// check if no same number across the row
for(int i = 0; i < 4; i++){
for(int j = 0; j < 3; j++){
if(grid[i][j] == grid[i][j+1]){
condi = false;
}
}
}
// check if no same number across the column
for(int i = 0; i < 3; i++){
for(int j = 0; j < 4; j++){
if(grid[i][j] == grid[i+1][j]){
condi = false;
}
}
}
// check if there is zero
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(grid[i][j] ==0){
condi = false;
}
}
}
return condi;
}
//check if the player won the game
int check_win(){
bool condi = false;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(grid[i][j] == 2048){
condi = true;
return condi;
}
}
}
return condi;
}
// code from Maurits van der Schee: 2048.c
void signal_callback_handler(int signum) {
printf(" TERMINATED \n");
setBufferedInput(true);
printf("\033[?25h\033[m");
exit(signum);
}
void setBufferedInput(bool enable) {
static bool enabled = true;
static struct termios old;
struct termios new;
if (enable && !enabled) {
// restore the former settings
tcsetattr(STDIN_FILENO,TCSANOW,&old);
// set the new state
enabled = true;
} else if (!enable && enabled) {
// get the terminal settings for standard input
tcgetattr(STDIN_FILENO,&new);
// we want to keep the old setting to restore them at the end
old = new;
// disable canonical mode (buffered i/o) and local echo
new.c_lflag &=(~ICANON & ~ECHO);
// set the new settings immediately
tcsetattr(STDIN_FILENO,TCSANOW,&new);
// set the new state
enabled = false;
}
}