-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehacked.c
309 lines (287 loc) · 5.91 KB
/
behacked.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <ncurses.h>
#define WIDTH 10
#define HEIGHT 10
#define DEBUG 1
static int grid[HEIGHT][WIDTH] = {0};
static int _ncomb(int x, int y, int color){
int n;
if(grid[y][x] != color) return 0;
grid[y][x] = 0;
n = 1;
if(y-1 >= 0) n += _ncomb(x,y-1,color);
if(y+1 < HEIGHT) n += _ncomb(x,y+1,color);
if(x-1 >= 0) n += _ncomb(x-1,y,color);
if(x+1 < WIDTH) n += _ncomb(x+1,y,color);
return n;
}
/**
* Returns the number of similar gems connected to the specified one,
* including said gem.
*/
int ncomb(int x, int y){
int color = grid[y][x];
int i,ret;
ret = _ncomb(x,y,color);
for(i=0;i<WIDTH*HEIGHT;i++){
if(grid[i/WIDTH][i%WIDTH] == 0) grid[i/WIDTH][i%WIDTH] = color;
}
return ret;
}
/**
* Sleeps for a given number of milliseconds
*/
void msleep(int n){
usleep(n*1000);
}
/**
* Just redraws the grid
*/
void redraw(void){
int i;
for(i=0;i<WIDTH*HEIGHT;i++){
int r = grid[i/WIDTH][i%WIDTH];
if(r != 0){
attron(COLOR_PAIR(r));
mvaddch(i/WIDTH,i%WIDTH,'*');
attroff(COLOR_PAIR(r));
}
}
refresh();
}
/**
* Also redraws the current score
*/
void redrawall(int score){
clear();
redraw();
mvprintw(4,WIDTH+5,"Score: %d",score);
refresh();
}
void breakcombs(int tobreak[HEIGHT][WIDTH]){
int k;
for(k=0;k<WIDTH*HEIGHT;k++){
if(tobreak[k/WIDTH][k%WIDTH]){
attron(COLOR_PAIR(grid[k/WIDTH][k%WIDTH]));
mvaddch(k/WIDTH,k%WIDTH,'o');
}
}
refresh();
msleep(150); /*is a nice number*/
for(k=0;k<WIDTH*HEIGHT;k++){
if(tobreak[k/WIDTH][k%WIDTH]){
attron(COLOR_PAIR(grid[k/WIDTH][k%WIDTH]));
mvaddch(k/WIDTH,k%WIDTH,'O');
}
}
refresh();
msleep(150);
for(k=0;k<WIDTH*HEIGHT;k++){
if(tobreak[k/WIDTH][k%WIDTH]){
attron(COLOR_PAIR(grid[k/WIDTH][k%WIDTH]));
mvaddch(k/WIDTH,k%WIDTH,' ');
grid[k/WIDTH][k%WIDTH] = 0;
}
}
refresh();
msleep(150);
}
/**
* Generates a random integer in the interval (0,n]
*/
int randn(int n){
int r;
while((r = random()) >= (RAND_MAX/n*n));
return 1+(r%n);
}
void refill(void){
int hasblank;
int x,y;
do{
hasblank = 0;
for(x=0;x<WIDTH;x++){
for(y=HEIGHT-1;y>=1;y--){
if(grid[y-1][x] != 0 && grid[y][x] == 0){
grid[y][x] = grid[y-1][x];
grid[y-1][x] = 0;
hasblank = 1;
}
}
if(grid[0][x] == 0){
grid[0][x] = randn(6);
hasblank = 1;
}
}
redraw();
msleep(150);
}while(hasblank);
}
/**
* Removes combinations (groups of four) in the grid and refills.
* Returns the number of points to be added.
*/
int rmcombs(void){
int tobreak[HEIGHT][WIDTH] = {0};
int score=0;
int x,y;
for(y=0;y<HEIGHT;y++){
for(x=0;x<WIDTH;x++){
if(ncomb(x,y) >= 4){
tobreak[y][x] = 1;
score += 5; /*per gem*/
}
}
}
breakcombs(tobreak);
refill();
return score;
}
/**
* Returns the number of potential combinations.
*/
int ncombs(void){
int x,y;
int temp;
int n;
/*test vertical swaps first...*/
for(x=0;x<WIDTH;x++){
for(y=0;y<HEIGHT-1;y++){
/*swap (x,y) and (x,y+1)*/
temp = grid[y][x];
grid[y][x] = grid[y+1][x];
grid[y+1][x] = temp;
/*count the combinations*/
n += (ncomb(x,y) >= 4);
n += (ncomb(x,y+1) >= 4);
/*swap back*/
grid[y+1][x] = grid[y][x];
grid[y][x] = temp;
}
}
/*...and then horizontal*/
for(x=0;x<WIDTH-1;x++){
for(y=0;y<HEIGHT;y++){
/*swap (x,y) and (x+1,y)*/
temp = grid[y][x];
grid[y][x] = grid[y][x+1];
grid[y][x+1] = temp;
/*count the combinations*/
n += (ncomb(x,y) >= 4);
n += (ncomb(x+1,y) >= 4);
/*swap back*/
grid[y][x+1] = grid[y][x];
grid[y][x] = temp;
}
}
return n;
}
int main(int argc, char **argv){
int score = 0;
int cx,cy; /*cursor x,y*/
int sx,sy; /*selected x,y*/
int i;
int c;
srandom(time(NULL)); /*seed random*/
initscr(); /*initialize ncurses*/
cbreak(); /*suspend line buffering*/
noecho(); /*@echo off*/
keypad(stdscr,TRUE); /*enable arrows*/
start_color(); /*enable color*/
for(i=1;i<=6;i++) init_pair(i,i,COLOR_BLACK);
mvprintw(4,7,"Behacked");
refresh();
msleep(2000);
clear();
refill();
redrawall(score);
move(5,5);
refresh();
while((i = rmcombs())){
score += i;
redrawall(score);
}
cx = cy = 5;
sx = sy = -1;
while((c = getch()) != 'q'){
switch(c){
case KEY_LEFT:
case 'h':
if(cx > 0) cx--;
break;
case KEY_RIGHT:
case 'l':
if(cx < WIDTH-1) cx++;
break;
case KEY_UP:
case 'k':
if(cy > 0) cy--;
break;
case KEY_DOWN:
case 'j':
if(cy < HEIGHT-1) cy++;
break;
#ifdef DEBUG
case 'n':
redraw();
mvprintw(HEIGHT+4,2,"(%d,%d) %d*%d",cx,cy,ncomb(cx,cy),grid[cy][cx]);
refresh();
break;
case 'N':
redraw();
mvprintw(2,WIDTH+5,"%d",ncombs());
refresh();
break;
case 'r':
redrawall(score);
break;
#endif
case ' ':
if(sx == -1){
sx = cx;
sy = cy;
attron(COLOR_PAIR(grid[cy][cx]));
mvaddch(cy,cx,'*'|A_BOLD);
attroff(COLOR_PAIR(grid[cy][cx]));
}else{
redraw(); /*gets rid of bold*/
if((sx-cx == 1 || sx-cx == -1) ^ (sy-cy == 1 || sy-cy == -1)){
int r = grid[sy][sx];
grid[sy][sx] = grid[cy][cx];
grid[cy][cx] = r;
redrawall(score);
msleep(150);
if((i = rmcombs())){
do{
score += i;
redrawall(score);
}while((i = rmcombs()));
if(ncombs() == 0){
mvprintw(4,(WIDTH/2)-5,"GAME OVER");
getch();
endwin();
exit(0);
}
}else{
r = grid[sy][sx];
grid[sy][sx] = grid[cy][cx];
grid[cy][cx] = r;
redraw();
}
}
sy = sx = -1;
}
break;
#ifdef DEBUG
default:
mvprintw(HEIGHT+2,5,"%d",c);
#endif
}
move(cy,cx);
refresh();
}
endwin();
return 0;
}