-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsokoban3.c
534 lines (482 loc) · 15.7 KB
/
sokoban3.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include <stdio.h>
#include <termio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define name_max_length 10 //이름의 최대 길이
#define map_height 11
#define map_width 24 //맵의 최대 가로 길이
#define max_hole 7
#define map_max_number 3// 맵의 최대 개수
int scan_name(char name[name_max_length]); //이름을 적기 위한 함수, 리턴값은 이름의 길이
int getch(void); //엔터 없이 입력하게 하기 위한 함수_
int find_char_height(char map_file[map_height][map_width]); //캐릭터의 위치를 찾는 함수
int find_char_width(char map_file[map_height][map_width]);
void move_down(char map_file[map_height][map_width], int, int); // 캐릭터를 움직이게 하는 함수
void move_top(char map_file[map_height][map_width], int, int);
void move_left(char map_file[map_height][map_width], int, int);
void move_right(char map_file[map_height][map_width], int, int);
void print_map(char map_file[map_height][map_width]);//맵을 출력하는 함수
void change_hole(char map_file[map_height][map_width], int hole_location[max_hole][2]); //맵의 0개수를 처리하는 함수
void find_hole(char map_file[map_height][map_width], int hole_location[max_hole][2]);
void display_help(void);
void display_top(void);
int check_clear(char map_file[map_height][map_width], int hole_location[max_hole][2]);
void undo_scan(char map_file[map_height][map_width]);
int undo_print(char map_file[map_height][map_width]);
char undo[5][map_height][map_width]={0};
char map_replay[map_height][map_width]; //맵 리플레이를 저장하기 위한 변수
int main(void)
{
FILE * map_load; //맵 파일 불러오기 위한 파일 변수
char name[name_max_length]; //이름을 입력받는 함수
char insert; //입력을 입력받는 변수
int name_length, help_state=0;
int hole_location[max_hole][2]={0};
int map_number=0;
char map_file[map_height][map_width]; //맵 파일을 저장하기 위한 변수
char map_file_all[map_max_number][map_height][map_width];
name_length = scan_name(name);
map_load = fopen("map", "r"); //맵 파일 불러오기
for(int k=0;k<map_max_number;k++)
{
for(int i=0;i<map_height;i++) //맵 파일을 map_file에 저장
{
for(int j=0;j<map_width;j++)
{
fscanf(map_load, "%c", &map_file_all[k][i][j]);
}
}
}
for(int i=0;i<map_height;i++) //맵 파일을 map_file에 저장
{
for(int j=0;j<map_width;j++)
{
map_file[i][j] = map_file_all[0][i][j];
}
}
for (int i=0; i<map_height; i++)
for(int j=0; j<map_width; j++) {
undo[0][i][j]= map_file[i][j];
undo[1][i][j]= map_file[i][j];
undo[2][i][j]= map_file[i][j];
undo[3][i][j]= map_file[i][j];
undo[4][i][j]= map_file[i][j];
}
find_hole(map_file, hole_location);
print_map(map_file);
int replay_i=0;
while(1) //값을 입력받는다(e를 누르기 전까지)
{
if (replay_i==0) {
for(int i=0; i<map_height; i++){
for(int j=0; j<map_width; j++){
map_replay[i][j] = map_file[i][j];
}
}
}
replay_i =1 ;
insert = getch(); //값을 입력 받는다
switch(insert)
{
case 'j' :
if(help_state==0)
{
undo_scan(map_file);
move_down(map_file, find_char_height(map_file), find_char_width(map_file));
change_hole(map_file, hole_location);
print_map(map_file);
break;
}
else if(help_state==1)
break;
case 'k' :
if(help_state==0)
{
undo_scan(map_file);
move_top(map_file, find_char_height(map_file), find_char_width(map_file));
change_hole(map_file, hole_location);
print_map(map_file);
break;
}
else if(help_state==1)
break;
case 'l' :
if(help_state==0)
{
undo_scan(map_file);
move_right(map_file, find_char_height(map_file), find_char_width(map_file));
change_hole(map_file, hole_location);
print_map(map_file);
break;
}
else if(help_state==1)
break;
case 'h' :
if(help_state==0)
{
undo_scan(map_file);
move_left(map_file, find_char_height(map_file), find_char_width(map_file));
change_hole(map_file, hole_location);
print_map(map_file);
break;
}
else if(help_state==1)
break;
case 'd' :
if(help_state==1)
{
print_map(map_file);
help_state--;
break;
}
else if(help_state==0)
{
display_help();
help_state++;
break;
}
case 'u' :
if(help_state==0)
{
undo_print(map_file);
break;
}
else if(help_state==1)
break;
case 'r' :
if(help_state==0)
{
for(int i=0; i<map_height; i++){
for(int j=0; j<map_width; j++){
map_file[i][j] = map_replay[i][j];
}
}
print_map(map_file);
break;
}
else if(help_state==1)
break;
case 't' :
display_top();
}
if (check_clear(map_file, hole_location)==1)
{
map_number++;
for(int i=0;i<map_height;i++) //맵 파일을 map_file에 저장
{
for(int j=0;j<map_width;j++)
{
map_file[i][j]=map_file_all[map_number][i][j];
}
}
find_hole(map_file, hole_location);
print_map(map_file);
replay_i=0;
}
}
return 0;
}
int getch(void){
int ch;
struct termios buf;
struct termios save;
tcgetattr(0, &save);
buf = save;
buf.c_lflag&=~(ICANON|ECHO);
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
tcsetattr(0, TCSAFLUSH, &buf);
ch = getchar();
tcsetattr(0, TCSAFLUSH, &save);
return ch;
}
void print_map(char map_file[map_height][map_width])
{
system("clear");
printf("");
for(int i=0;i<map_height;i++) //맵 파일 출력하기
{
for(int j=0;j<map_width;j++)
{
printf("%c", map_file[i][j]);
}
}
printf("\n(Command)\n");
}
int find_char_height(char map_file[][map_width])
{
for(int i=0;i<map_height;i++)
{
for(int j=0;j<map_width;j++)
{
if(map_file[i][j]=='@')
return i;
}
}
}
int find_char_width(char map_file[][map_width])
{
for(int i=0;i<map_height;i++)
{
for(int j=0;j<map_width;j++)
{
if(map_file[i][j]=='@')
return j;
}
}
}
void move_down(char map_file[][map_width], int height, int width)
{
if(map_file[height+1][width] == '0')
{
map_file[height+1][width] = map_file[height][width];
map_file[height][width] = ' ';
}
else if(map_file[height+1][width] == '$' && map_file[height+2][width] == '$');
else if(map_file[height+1][width] == '$' && map_file[height+2][width] == '#');
else if(map_file[height+2][width] == '0' && map_file[height+1][width] == '$')
{
char temp;
map_file[height+2][width] = ' ';
temp = map_file[height+2][width];
map_file[height+2][width] = map_file[height+1][width];
map_file[height+1][width] = temp;
temp = map_file[height+1][width];
map_file[height+1][width] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height+1][width] == '$')
{
char temp = map_file[height+2][width];
map_file[height+2][width] = map_file[height+1][width];
map_file[height+1][width] = temp;
temp = map_file[height+1][width];
map_file[height+1][width] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height+1][width] != '#')
{
char temp = map_file[height+1][width];
map_file[height+1][width] = map_file[height][width];
map_file[height][width] = temp;
}
}
void move_top(char map_file[][map_width], int height, int width)
{
char temp;
if(map_file[height-1][width] == '0')
{
map_file[height-1][width] = map_file[height][width];
map_file[height][width] = ' ';
}
else if(map_file[height-1][width] == '$' && map_file[height-2][width] == '$');
else if(map_file[height-1][width] == '$' && map_file[height-2][width] == '#');
else if(map_file[height-2][width] == '0' && map_file[height-1][width] == '$')
{
char temp;
map_file[height-2][width] = ' ';
temp = map_file[height-2][width];
map_file[height-2][width] = map_file[height-1][width];
map_file[height-1][width] = temp;
temp = map_file[height-1][width];
map_file[height-1][width] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height-1][width] == '$')
{
char temp = map_file[height-2][width];
map_file[height-2][width] = map_file[height-1][width];
map_file[height-1][width] = temp;
temp = map_file[height-1][width];
map_file[height-1][width] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height-1][width] == ' ')
{
char temp = map_file[height-1][width];
map_file[height-1][width] = map_file[height][width];
map_file[height][width] = temp;
}
}
void move_right(char map_file[][map_width], int height, int width)
{
if(map_file[height][width+1] == '0')
{
map_file[height][width+1] = map_file[height][width];
map_file[height][width] = ' ';
}
else if(map_file[height][width+1] == '$' && map_file[height][width+2] == '$');
else if(map_file[height][width+1] == '$' && map_file[height][width+2] == '#');
else if(map_file[height][width+2] == '0' && map_file[height][width+1] == '$')
{
char temp;
map_file[height][width+2] = ' ';
temp = map_file[height][width+2];
map_file[height][width+2] = map_file[height][width+1];
map_file[height][width+1] = temp;
temp = map_file[height][width+1];
map_file[height][width+1] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height][width+1] == '$')
{
char temp = map_file[height][width+2];
map_file[height][width+2] = map_file[height][width+1];
map_file[height][width+1] = temp;
temp = map_file[height][width+1];
map_file[height][width+1] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height][width+1] == ' ')
{
char temp = map_file[height][width+1];
map_file[height][width+1] = map_file[height][width];
map_file[height][width] = temp;
}
}
void move_left(char map_file[][map_width], int height, int width)
{
if(map_file[height][width-1] == '0')
{
map_file[height][width-1] = map_file[height][width];
map_file[height][width] = ' ';
}
else if(map_file[height][width-1] == '$' && map_file[height][width-2] == '$');
else if(map_file[height][width-1] == '$' && map_file[height][width-2] == '#');
else if(map_file[height][width-2] == '0' && map_file[height][width-1] == '$')
{
char temp;
map_file[height][width-2] = ' ';
temp = map_file[height][width-2];
map_file[height][width-2] = map_file[height][width-1];
map_file[height][width-1] = temp;
temp = map_file[height][width-1];
map_file[height][width-1] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height][width-1] == '$')
{
char temp = map_file[height][width-2];
map_file[height][width-2] = map_file[height][width-1];
map_file[height][width-1] = temp;
temp = map_file[height][width-1];
map_file[height][width-1] = map_file[height][width];
map_file[height][width] = temp;
}
else if(map_file[height][width-1] == ' ')
{
char temp = map_file[height][width-1];
map_file[height][width-1] = map_file[height][width];
map_file[height][width] = temp;
}
}
int scan_name(char name[name_max_length])
{
int name_size; //이름 길이
printf("Start.....\n");//이름을 입력받기 위한 것
printf("Input name : ");
scanf("%10s", name);
name_size = strlen(name);
return name_size;
}
void find_hole(char map_file[map_height][map_width], int hole_location[max_hole][2])
{
int location_height = 0;
for(int i=0;i<map_height;i++)
{
for(int j=0;j<map_width;j++)
{
if(map_file[i][j]=='0')
{
hole_location[location_height][0] = i;
hole_location[location_height][1] = j;
location_height++;
}
}
}
}
void change_hole(char map_file[map_height][map_width], int hole_location[max_hole][2])
{
for(int i=0;hole_location[i][0]!=0;i++)
{
if(map_file[hole_location[i][0]][hole_location[i][1]]==' ')
{
map_file[hole_location[i][0]][hole_location[i][1]]='0';
}
}
}
void display_help()
{
system("clear");
printf("h(왼쪽), j(아래), k(위), l(오른쪽) : 창고지기 조정\n");
printf("u(undo) : 취소기능, 최대 5번 할 수 있음\n");
printf("r(replay) : 현재 맵을 처음부터 다시 시작(게임시간은 계속 유지)\n");
printf("n(new) : 첫 번째 맵부터 다시 시작(현재까지의 시간 기록 삭제)\n");
printf("e(exit) : 게임 종료.\n");
printf("s(save) : 현재 상태 파일에 저장.\n");
printf("f(file load) : save 시점에서부터 이어서 게임하게 함\n");
printf("d(display help) : 명령 내용 불러줌. 만약 명령내용이 띄어진 상태로 누르면 맵으로 이동함\n");
printf("t(top) : 게임 순위 보여줌. t만 입력하면 전체 순위, t 다음 숫자가 오면 해당 맵의 순위\n");
printf("\n***도움말을 나가고 싶으면 d를 누르시오***");
}
void display_top()
{
FILE *top_load;
top_load = fopen("map", "r");
char top_file[100][100];
for(int i=0;i<map_height;i++)
{
for(int j=0;j<map_width;j++)
{
fscanf(top_load, "%c", &top_file[i][j]);
}
}
}
int check_clear(char map_file[map_height][map_width], int hole_location[max_hole][2])
{
int hole_clear=0;
for(int i=0;i<max_hole-1;i++)
{
if(map_file[hole_location[i][0]][hole_location[i][1]]=='$')
{
hole_clear++;
}
}
if(hole_clear==max_hole-1)
{
return 1;
}
else
{
return 0;
}
}
void undo_scan(char map_file[map_height][map_width]) //맵 파일을 5번 기억 하는 함수 (움직일때마다 사용 - 이 전 5번의 움직임을 저장)
{
for(int i=0; i<map_height; i++)
for(int j=0; j<map_width; j++){
undo[4][i][j] = undo[3][i][j];
undo[3][i][j] = undo[2][i][j];
undo[2][i][j] = undo[1][i][j];
undo[1][i][j] = undo[0][i][j];
undo[0][i][j] = map_file[i][j];
}
}
int undo_print(char map_file[][map_width])
{
int undo_num =0;
if (undo_num==5)
return 0;
for(int i=0; i<map_height; i++)
for(int j=0; j<map_width; j++){
map_file[i][j] = undo[0][i][j];
undo[0][i][j] = undo[1][i][j];
undo[1][i][j] = undo[2][i][j];
undo[2][i][j] = undo[3][i][j];
undo[3][i][j] = undo[4][i][j];
}
undo_num++;
print_map(map_file);
return 0;
}