Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
zmx0142857
GitHub Repository: zmx0142857/mini-games
Path: blob/master/c/sokoban/0.2/main.c
362 views
1
#include "Sokoban.h"
2
3
// ��ʾ������ʹ������鿴���ļ���
4
5
// ȫ�ֱ���
6
Ukulele Map[MAP_H][MAP_W] = {{0}};
7
Ukulele MapCopy[MAP_H][MAP_W] = {{0}};
8
Ukulele Hero[2] = {0};
9
Ukulele Door[2] = {0};
10
Ukulele nLevel = 0;
11
Ukulele nMove = 0;
12
Ukulele nPush = 0;
13
BOOL gate_open = 0;
14
BOOL game_over = 0;
15
16
int main()
17
{
18
while (1)
19
{
20
Scene_Welcome();
21
22
switch (getch())
23
{
24
case '1': Play(1); break;
25
case '2': Scene_SelectLevel(); break;
26
case '3': Scene_Instruction(); break;
27
case '4': return 0;
28
default : system("cls");
29
}
30
}
31
return 0;
32
}
33
34
void Init()
35
{
36
zero_mem(Map, sizeof(Map));
37
zero_mem(MapCopy, sizeof(MapCopy));
38
nMove = 0;
39
nPush = 0;
40
gate_open = 0;
41
game_over = 0;
42
}
43
44
void Scene_Welcome()
45
{
46
// screen_sz(32, 70);
47
system("mode con lines=32 cols=70");
48
set_color(x000000, x0000ff); // ��ɫ
49
system("cls");
50
51
printf("\n\n\n\n");
52
printf(" ���� �� �� �� �� �������������� \n");
53
printf(" ���� �� �� ������ �������� �� \n");
54
printf(" �������������������� �� �� �� �� �� \n");
55
printf(" ������������ �� �� ���������� �� \n");
56
printf(" �������������� ������������������ ������������������ \n");
57
printf(" ������������ �� �� ���������� �� \n");
58
printf(" �������� �������� ���������������� �� \n");
59
printf(" ������ �� �� �� �� ���������� �� \n");
60
printf(" ������ ���������� �� ���������� ������ \n");
61
printf(" \n");
62
printf(" ���۾��� - 0.2 -\n\n");
63
64
set_color(x000000, xffffff); // ��ɫ
65
printf("\t\t\t>>1 ��ʼ��Ϸ\t\t\t\t\n\n");
66
printf("\t\t\t>>2 ѡ��ؿ�\t\t\t\t\n\n");
67
printf("\t\t\t>>3 ��Ϸ����\t\t\t\t\n\n");
68
printf("\t\t\t>>4 �˳���Ϸ\t\t\t\t\n\n");
69
}
70
71
void Scene_Instruction()
72
{
73
system("cls");
74
75
set_color(x000000, xffffff);
76
printf("\t\t\t����������������������\n");
77
printf("\t\t\t�� ͼ������� ��\n");
78
printf("\t\t\t�t�������������������s\n\n");
79
80
set_color(x000000, x00ffff); // ����ɫ
81
printf(" �� ");
82
83
set_color(x000000, xffffff);
84
printf("��������ˡ�\n\n\n");
85
86
set_color(x000000, x808080); // ��ɫ
87
printf(" �� ");
88
89
set_color(x000000, xffffff);
90
printf("����ǽ��\n\n\n");
91
92
set_color(x000000, x808080);
93
printf(" �� ");
94
95
set_color(x000000, xffffff);
96
printf("�������ӡ�ע�⣬���޷�ͬʱ�ƶ��������ӡ�\n\n\n");
97
98
set_color(x000000, xffff00);
99
printf(" �� ");
100
101
set_color(x000000, xffffff);
102
printf("���ӵ�Ŀ��λ�á������������Ƶ���Ӧλ���Խ������ڡ�\n\n\n");
103
104
set_color(x000000, xffff00);
105
printf(" �� ");
106
107
set_color(x000000, xffffff);
108
printf("�Ѿ�����Ŀ��λ�õ����ӡ�\n\n\n");
109
110
set_color(x000000, xff00ff); // �Ϻ�
111
printf(" �� ");
112
113
set_color(x000000, xffffff);
114
printf("���ڡ�\n\n\n");
115
116
printf(" �������� �ƶ���\n\n\n");
117
printf(" R ����������\n\n\n");
118
printf(" Ctrl+C ���������档\n");
119
120
getch();
121
}
122
123
void Scene_SelectLevel()
124
{
125
system("cls");
126
127
printf("\n\n");
128
printf("\t������ ������ ������ ������ ������ ������\n");
129
printf("\t��01�� ��02�� ��03�� ��04�� ��05�� ��06��\n");
130
printf("\t�t���s �t���s �t���s �t���s �t���s �t���s\n\n");
131
printf("\t������ ������ ������ ������ ������ ������\n");
132
printf("\t��07�� ��08�� ��09�� ��10�� ��11�� ��12��\n");
133
printf("\t�t���s �t���s �t���s �t���s �t���s �t���s\n\n");
134
printf("\t������ ������ \n");
135
printf("\t��13�� ��14�� \n");
136
printf("\t�t���s �t���s \n\n");
137
138
printf("\t\t\t��ѡ��ؿ�: ");
139
140
fflush(stdin); // ��ջ�����
141
int i;
142
scanf("%d", &i);
143
Play(i);
144
}
145
146
void LoadLevel(int n)
147
{
148
Init();
149
150
// read level from file!!!-----------------------------------------------------------
151
switch(n)
152
{
153
case 1: Lv1(); break;
154
case 2: Lv2(); break;
155
case 3: Lv3(); break;
156
case 4: Lv4(); break;
157
case 5: Lv5(); break;
158
case 6: Lv6(); break;
159
case 7: Lv7(); break;
160
case 8: Lv8(); break;
161
case 9: Lv9(); break;
162
case 10:Lv10(); break;
163
case 11:Lv11(); break;
164
case 12:Lv12(); break;
165
case 13:Lv13(); break;
166
case 14:Lv14(); break;
167
default:return;
168
}
169
170
nLevel = n;
171
Map[Hero_r][Hero_c] = HERO;
172
MapCopy[Door_r][Door_c] = DOOR;
173
PrintBoard();
174
}
175
176
void Play(int i)
177
{
178
while (i <= MAX_LEVEL)
179
{
180
LoadLevel(i);
181
while (!game_over)
182
{
183
if (Control())
184
return;
185
}
186
Sleep(255);
187
++i;
188
}
189
}
190
191
// ��Ҫ�ص�������, �򷵻�1
192
BOOL Control()
193
{
194
// ������
195
char key = getch();
196
197
if (key == 'u' || key == 'U') // U - ����
198
{
199
}
200
201
else if (key == 'r' || key == 'R') // R - ��������
202
{
203
LoadLevel(nLevel);
204
}
205
206
else if (key == 3) // Ctrl+C - ����������
207
{
208
return 1;
209
}
210
211
else if (key == -32)
212
{
213
key = getch();
214
215
if (key != 'H' && key != 'P' && key != 'K' && key != 'M')
216
return 0;
217
218
int dr = key == 'H' ? -1 : (key == 'P' ? 1 : 0);
219
int dc = key == 'K' ? -1 : (key == 'M' ? 1 : 0);
220
int next_r = Hero_r + dr;
221
int next_c = Hero_c + dc;
222
223
BOOL move = Map[next_r][next_c] == SPACE
224
|| Map[next_r][next_c] == STAR
225
|| Map[next_r][next_c] == DOOR;
226
227
BOOL push = Map[next_r][next_c] == BOX
228
|| Map[next_r][next_c] == BOX_ON_STAR;
229
230
BOOL has_space = Map[next_r + dr][next_c + dc] == SPACE;
231
BOOL has_star = Map[next_r + dr][next_c + dc] == STAR;
232
BOOL update_hero = 0;
233
234
if (move)
235
{
236
update_hero = 1;
237
}
238
else if (push && (has_space || has_star))
239
{
240
update_hero = 1;
241
Map[next_r + dr][next_c + dc] = has_star ? BOX_ON_STAR : BOX;
242
243
print_on_color("��", next_r + dr, 2*(next_c + dc),
244
x000000, has_star ? xffff00 : x808080);
245
++nPush;
246
}
247
248
if (update_hero)
249
{
250
// MapCopy���������⣺�ָ���Ĩȥ���ǼǺ�
251
BOOL star = MapCopy[Hero_r][Hero_c] == STAR;
252
Map[Hero_r][Hero_c] = star ? STAR : SPACE;
253
Map[next_r][next_c] = HERO;
254
255
print_on_color(star ? "��" : " ", Hero_r, 2*Hero_c,
256
x000000, xffff00);
257
print_on_color("��", next_r, 2*next_c,
258
x000000, x00ffff);
259
Hero_r += dr;
260
Hero_c += dc;
261
++nMove;
262
}
263
}
264
265
// �жϻ�ʤ
266
game_over = LvWon();
267
268
// �����ƶ����������
269
set_color(x000000, xffffff);
270
set_cursor(MAP_H + 2, 20);
271
printf("%2d", nMove);
272
set_cursor(MAP_H + 2, 30);
273
printf("%2d", nPush);
274
set_cursor(0, 0);
275
276
return 0;
277
}
278
279
void PrintBoard()
280
{
281
// screen_sz(20, 35);
282
system("mode con lines=20 cols=35");
283
system("cls");
284
285
int i, j;
286
for (i = 0; i < MAP_H; ++i, printf("\n"))
287
for (j = 0; j < MAP_W; ++j)
288
switch (Map[i][j])
289
{
290
case HERO: set_color(x000000, x00ffff); printf("��"); break;
291
case WALL: set_color(x000000, x808080); printf("��"); break;
292
case BOX: set_color(x000000, x808080); printf("��"); break;
293
case BOX_ON_STAR:
294
set_color(x000000, xffff00); printf("��"); break;
295
case STAR: set_color(x000000, xffff00); printf("��"); break;
296
default: printf(" ");
297
}
298
299
set_color(x000000, xffffff);
300
printf("\n\n");
301
printf(" �ؿ�%3d �ƶ�%3d ����%3d\n\n", nLevel, nMove, nPush);
302
printf(" R - ��������\n");
303
printf(" Ctrl+C - ��������\n");
304
}
305
306
BOOL LvWon()
307
{
308
if (!gate_open)
309
{
310
// ������������
311
int i, j;
312
for (i = 0; i < MAP_H; ++i)
313
for (j = 0; j < MAP_W; ++j)
314
if (Map[i][j] == BOX)
315
return 0;
316
317
Map[Door_r][Door_c] = DOOR;
318
print_on_color("��", Door_r, 2*Door_c, x000000, xff00ff);
319
gate_open = 1;
320
}
321
322
return Map[Door_r][Door_c] == HERO;
323
}
324
325