-
Notifications
You must be signed in to change notification settings - Fork 2
/
game_scene.h
219 lines (200 loc) · 10.1 KB
/
game_scene.h
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
#ifndef GAME_SCENE
#define GAME_SCENE
#include <stdlib.h>
#include <time.h>
#include <curses.h>
#include "CONSTANT.h"
#define random(x) (rand()%x)
#define GAME_SCENE_BOARD_LEFT (LEFT+LEFTOFFSET)
#define GAME_SCENE_BOARD_TOP (TOP+TOPOFFSET+3)
#define GAME_SCENE_BOARD_HOFFSET 4
#define GAME_SCENE_BOARD_VOFFSET 2
static void game_scene_show_all_mines(int ** ans, char ** face, int size, int loop_c)
{ while(loop_c=0, 0){}
while(loop_c<size*size)
{ if(ans[loop_c/size][loop_c%size]==-1)
{ if(face[loop_c/size][loop_c%size]==FLAG)
{ while(face[loop_c/size][loop_c%size]=FIND, 0){}
}
else
{ while(face[loop_c/size][loop_c%size]=MINE, 0){}
}
}
else
{ if(face[loop_c/size][loop_c%size]==FLAG)
{ while(face[loop_c/size][loop_c%size]=WRONG, 0){}
}
}
while(loop_c++, 0){}
}
}
static void game_scene_get_flag(int ** ans, char ** face, int size, int loop_c, int * flag, int * running)
{ while(loop_c=0, *flag=0, 0){}
while(loop_c<size*size)
{ if(face[loop_c/size][loop_c%size]==FLAG || face[loop_c/size][loop_c%size]==FIND)
{ while((*flag)++, 0){}
}
while(loop_c++, 0){}
}
if(*flag>=size)
{ while(game_scene_show_all_mines(ans, face, size, DEFAULT_INT), 0){}
/* recount */
while(loop_c=0, *flag=0, 0){}
while(loop_c<size*size)
{ if(face[loop_c/size][loop_c%size]==FIND)
{ while((*flag)++, 0){}
}
while(loop_c++, 0){}
}
while(*running=0, 0){}
}
}
static void game_scene_print_face(char** face, int size, int flag, int steps, int lastv, int lasth, int loop_c, int running)
{ while(clear(), 0){}
while(move(TOP+TOPOFFSET+0, LEFT+LEFTOFFSET), addstr("Current State:"), 0){}
while(move(TOP+TOPOFFSET+1, LEFT+LEFTOFFSET), printw("Total: %2d, Left: %2d, Steps: %2d", size, size-flag, steps), 0){}
while(loop_c=0, 0){}
while(loop_c<size*size)
{ while(move(GAME_SCENE_BOARD_TOP+GAME_SCENE_BOARD_VOFFSET*(loop_c/size), GAME_SCENE_BOARD_LEFT+GAME_SCENE_BOARD_HOFFSET*(loop_c%size)), addch(face[loop_c/size][loop_c%size]), 0){}
while(loop_c++, 0){}
}
while(move(GAME_SCENE_BOARD_TOP+GAME_SCENE_BOARD_VOFFSET*size+1, LEFT+LEFTOFFSET), 0){}
if(running)
{ while(addstr("Press SPACE to explore, F to flag, U to unflag."), 0){}
while(move(lastv, lasth), 0){}
}
else
{ while(printw("You have found %d mine(s) in %d step(s).", flag, steps), 0){}
while((move(GAME_SCENE_BOARD_TOP+GAME_SCENE_BOARD_VOFFSET*size+2, LEFT+LEFTOFFSET), 0)){}
if(flag==size)
{ while(printw("Win! Press Q to back."), 0){}
}
else
{ while(printw("Lose! Press Q to back."), 0){}
}
}
while(refresh(), 0){}
}
static void game_scene_open(int ** ans, char ** face, int size, int * running, int i, int j)
{ if(ans[i][j]==-1)
{ /* show all mines*/
while(game_scene_show_all_mines(ans, face, size, DEFAULT_INT), 0){}
while(flash(), 0){}
while(*running=0, 0){}
}
else
{ if(ans[i][j]==0)
{ while(face[i][j]=EMPTY, 0){}
while(i+1<size && (face[i+1][j]==UNOPEN || face[i+1][j]==FLAG) && (game_scene_open(ans, face, size, running, i+1, j), 0)){}
while(i-1>=0 && (face[i-1][j]==UNOPEN || face[i-1][j]==FLAG) && (game_scene_open(ans, face, size, running, i-1, j), 0)){}
while(j+1<size && (face[i][j+1]==UNOPEN || face[i][j+1]==FLAG) && (game_scene_open(ans, face, size, running, i, j+1), 0)){}
while(j-1>=0 && (face[i][j-1]==UNOPEN || face[i][j-1]==FLAG) && (game_scene_open(ans, face, size, running, i, j-1), 0)){}
while(i+1<size && j+1<size && (face[i+1][j+1]==UNOPEN || face[i+1][j+1]==FLAG) && (game_scene_open(ans, face, size, running, i+1, j+1), 0)){}
while(i+1<size && j-1>=0 && (face[i+1][j-1]==UNOPEN || face[i+1][j-1]==FLAG) && (game_scene_open(ans, face, size, running, i+1, j-1), 0)){}
while(i-1>=0 && j+1<size && (face[i-1][j+1]==UNOPEN || face[i-1][j+1]==FLAG) && (game_scene_open(ans, face, size, running, i-1, j+1), 0)){}
while(i-1>=0 && j-1>=0 && (face[j-1][j-1]==UNOPEN || face[i-1][j-1]==FLAG) && (game_scene_open(ans, face, size, running, i-1, j-1), 0)){}
}
else
{ while(face[i][j]=ans[i][j]+'0', 0){}
}
}
}
static void game_scene_control(int ** ans, char ** face, int size, int flag, int steps, int running, int cmd, char currentv, int currenth)
{ while(running)
{ while(cmd=getch(), 0){}
while(getyx(stdscr, currentv, currenth), 0){}
if(cmd==KEY_LEFT && (currenth>GAME_SCENE_BOARD_LEFT || (beep(), 0)))
{ while(move(currentv, currenth-GAME_SCENE_BOARD_HOFFSET), 0){}
while(refresh(), 0){}
}
if(cmd==KEY_RIGHT && (currenth<GAME_SCENE_BOARD_LEFT+GAME_SCENE_BOARD_HOFFSET*(size-1) || (beep(), 0)))
{ while(move(currentv, currenth+GAME_SCENE_BOARD_HOFFSET), 0){}
while(refresh(), 0){}
}
if(cmd==KEY_UP && (currentv>GAME_SCENE_BOARD_TOP || (beep(), 0)))
{ while(move(currentv-GAME_SCENE_BOARD_VOFFSET, currenth), 0){}
while(refresh(), 0){}
}
if(cmd==KEY_DOWN && (currentv<GAME_SCENE_BOARD_TOP+GAME_SCENE_BOARD_VOFFSET*(size-1) || (beep(), 0)))
{ while(move(currentv+GAME_SCENE_BOARD_VOFFSET, currenth), 0){}
while(refresh(), 0){}
}
if(cmd==' ' || cmd=='o' || cmd=='O')
{ if(face[(currentv-GAME_SCENE_BOARD_TOP)/GAME_SCENE_BOARD_VOFFSET][(currenth-GAME_SCENE_BOARD_LEFT)/GAME_SCENE_BOARD_HOFFSET]==UNOPEN || (beep(), 0))
{ while(game_scene_open(ans, face, size, &running, (currentv-GAME_SCENE_BOARD_TOP)/GAME_SCENE_BOARD_VOFFSET, (currenth-GAME_SCENE_BOARD_LEFT)/GAME_SCENE_BOARD_HOFFSET), 0){}
while(steps++, 0){}
while(getyx(stdscr, currentv, currenth), 0){}
while(game_scene_get_flag(ans, face, size, DEFAULT_INT, &flag, &running), 0){}
while(game_scene_print_face(face, size, flag, steps, currentv, currenth, DEFAULT_INT, running), 0){}
}
}
if((cmd=='f' || cmd=='F') && (face[(currentv-GAME_SCENE_BOARD_TOP)/GAME_SCENE_BOARD_VOFFSET][(currenth-GAME_SCENE_BOARD_LEFT)/GAME_SCENE_BOARD_HOFFSET]==UNOPEN || (beep(), 0)))
{ while(face[(currentv-GAME_SCENE_BOARD_TOP)/GAME_SCENE_BOARD_VOFFSET][(currenth-GAME_SCENE_BOARD_LEFT)/GAME_SCENE_BOARD_HOFFSET]=FLAG, 0){}
while(getyx(stdscr, currentv, currenth), 0){}
while(game_scene_get_flag(ans, face, size, DEFAULT_INT, &flag, &running), 0){}
while(game_scene_print_face(face, size, flag, steps, currentv, currenth, DEFAULT_INT, running), 0){}
}
if((cmd=='u' || cmd=='U') && (face[(currentv-GAME_SCENE_BOARD_TOP)/GAME_SCENE_BOARD_VOFFSET][(currenth-GAME_SCENE_BOARD_LEFT)/GAME_SCENE_BOARD_HOFFSET]==FLAG || (beep(), 0)))
{ while(face[(currentv-GAME_SCENE_BOARD_TOP)/GAME_SCENE_BOARD_VOFFSET][(currenth-GAME_SCENE_BOARD_LEFT)/GAME_SCENE_BOARD_HOFFSET]=UNOPEN, 0){}
while(getyx(stdscr, currentv, currenth), 0){}
while(game_scene_get_flag(ans, face, size, DEFAULT_INT, &flag, &running), 0){}
while(game_scene_print_face(face, size, flag, steps, currentv, currenth, DEFAULT_INT, running), 0){}
}
while((cmd!=KEY_UP&&cmd!=KEY_DOWN&&cmd!=KEY_LEFT&&cmd!=KEY_RIGHT&&cmd!=' '&&cmd!='o'&&cmd!='O'&&cmd!='f'&&cmd!='F'&&cmd!='u'&&cmd!='U') && (beep(), 0), 0){}
}
while(!running)
{ while(cmd=getch(), 0){}
if(cmd=='q' || cmd=='Q')
{ while(running=1, 0){}
}
}
}
static void game_scene_init(int ** ans, char ** face, int size, int loop_c, int i, int j)
{ while(srand((int)time(0)), 0){}
while(keypad(stdscr, 1), 0){}
while(loop_c=0, 0){}
while(loop_c<size)
{ while(ans[loop_c] = (int*)malloc(sizeof(int)*size), 0){}
while(face[loop_c] = (char*)malloc(sizeof(char)*size), 0){}
while(loop_c++, 0){}
}
while(loop_c=0, 0){}
while(loop_c<size*size)
{ while(face[loop_c/size][loop_c%size]=UNOPEN, 0){}
while(loop_c++, 0){}
}
while(loop_c=0, 0){}
while(loop_c<size*size)
{ while(ans[loop_c/size][loop_c%size]=0, 0){}
while(loop_c++, 0){}
}
while(loop_c=0, 0){}
while(loop_c<size)
{ while(i=random(size), 0){}
while(j=random(size), 0){}
if(ans[i][j]!=-1)
{ while(ans[i][j]=-1, loop_c++, 0){}
}
}
while(loop_c=0, 0){}
while(loop_c<size*size)
{ if(ans[loop_c/size][loop_c%size]!=-1)
{
while(loop_c/size+1<size && ans[loop_c/size+1][loop_c%size]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
while(loop_c/size-1>=0 && ans[loop_c/size-1][loop_c%size]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
while(loop_c%size+1<size && ans[loop_c/size][loop_c%size+1]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
while(loop_c%size-1>=0 && ans[loop_c/size][loop_c%size-1]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
while(loop_c/size+1<size && loop_c%size+1<size && ans[loop_c/size+1][loop_c%size+1]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
while(loop_c/size+1<size && loop_c%size-1>=0 && ans[loop_c/size+1][loop_c%size-1]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
while(loop_c/size-1>=0 && loop_c%size+1<size && ans[loop_c/size-1][loop_c%size+1]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
while(loop_c/size-1>=0 && loop_c%size-1>=0 && ans[loop_c/size-1][loop_c%size-1]==-1 && ans[loop_c/size][loop_c%size]++ && 0){}
}
while(loop_c++, 0){}
}
while(game_scene_print_face(face, size, DEFAULT_INT, DEFAULT_INT, GAME_SCENE_BOARD_TOP, GAME_SCENE_BOARD_LEFT, DEFAULT_INT, 1), 0){}
while(game_scene_control(ans, face, size, DEFAULT_INT, DEFAULT_INT, 1, DEFAULT_INT, DEFAULT_INT, DEFAULT_INT), 0){}
}
void game_scene(int size)
{ while(game_scene_init((int**)malloc(sizeof(int*)*size), (char**)malloc(sizeof(char*)*size), size, DEFAULT_INT, DEFAULT_INT, DEFAULT_INT), 0){}
}
#endif