-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidingPuzzle.c
220 lines (155 loc) · 3.71 KB
/
slidingPuzzle.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#define W 4
#define H 4
int ground[W][H];
int getch(void);
void initGround(void);
void setRandomGround(void);
void slideGame(void);
void printGround(void);
int controlGround(char);
void HomePage(void);
int checkGround(void);
int c1,c2;
int main(){
initGround();
setRandomGround();
system("clear");
slideGame();
return 0;
}
void HomePage(){
printf("Slide Puzzle Game!!");
}
void slideGame(){
while(1){
printGround();
char c = getch();
if(!controlGround(c)) break;
system("clear");
if(checkGround()) {
printGround();
printf("축하합니다!\n");
sleep(3);
break;
}
}
}
void initGround(){
int n = 1;
for(int i = 0 ;i<H;i++){
for(int k = 0;k<W;k++){
ground[i][k] = n;
n++;
}
}
ground[W-1][H-1] = 0;
}
void setRandomGround(){
srand(time(NULL));
int n = 256;
char c;
while(n--){
int i = rand()%4;
switch(i){
case 0:
c = 'j';
break;
case 1:
c = 'i';
break;
case 2:
c = 'k';
break;
case 3:
c = 'l';
break;
default:
break;
}
controlGround(c);
}
}
void printGround(){
printf(" ");
for(int i = 0 ;i<H;i++){
for(int k = 0;k<W;k++){
if(ground[i][k] != 0) printf("%-4d",ground[i][k]);
else printf(" ");
}
printf("\n ");
}
}
int controlGround(char c){
int i,k;
for(int j = 0 ;j<H;j++){
for(int h = 0;h<W;h++){
if(ground[j][h] == 0){
i = j;
k = h;
}
}
}
if(c == '0') return 0;
switch (c) {
case 'j'://L
if(k != 3 && ground[i][k+1] != 0){
ground[i][k] = ground[i][k + 1];
ground[i][k+1] = 0;
}
break;
case 'i'://U
if(i != 3 && ground[i+1][k] != 0){
ground[i][k] = ground[i+1][k];
ground[i+1][k] = 0;
}
break;
case 'k'://D
if(i != 0 && ground[i-1][k] != 0){
ground[i][k] = ground[i-1][k];
ground[i-1][k] = 0;
}
break;
case 'l'://R
if(k != 0 && ground[i][k-1] != 0){
ground[i][k] = ground[i][k - 1];
ground[i][k-1] = 0;
}
break;
default:
break;
}
return 1;
}
int checkGround(){
int prev = ground[0][0],cur;
if(prev != 1) return 0;
for(int i = 0;i<H;i++){
for(int k = 0;k<W;k++){
cur = ground[i][k];
if(cur == 1) continue;
if(prev - cur == 15) return 1;
if(cur - prev != 1) return 0;
else prev = cur;
}
}
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;
}