-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
204 lines (184 loc) · 5.83 KB
/
main.c
File metadata and controls
204 lines (184 loc) · 5.83 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stddef.h>
typedef struct numbers Number;
int c[7][7];
void title(Number*);//게임 시작화면
void set_sol(void);//솔리테어 판
void tuto(Number*);//튜토리얼모드
void start_game(Number*);//게임 시작
void check_key(Number*);//입력 받은 키가 가능 한지 판단
void up(Number*,int, int);
void down(Number*,int, int);
void right(Number*,int, int);
void left(Number*,int, int);
int gameover(Number*);
struct numbers{
int numberOfmove;
int numberOfelements;
};
int gameover(Number* this){
if (this->numberOfelements==1)
return 1;
else
return 0;
}
Number* newNumber(int x, int y){
Number *num;
num=(Number*)malloc(sizeof(Number));
num->numberOfmove=x;
num->numberOfelements=y;
};
void up(Number *this,int x, int y) {
if ((c[x - 2][y - 1] == 0) || (c[x - 3][y - 1] != 0)){
this->numberOfmove++;
printf("move number:%d\n",this->numberOfmove);
printf("Fail.\n");
}
else {
c[x - 3][y - 1] = c[x - 1][y - 1];
c[x - 2][y - 1] = 0;
c[x - 1][y - 1] = 0;
this->numberOfmove++;
this->numberOfelements--;
printf("move number:%d, leave numbers:%d\n",this->numberOfmove,this->numberOfelements);
};
};
void down(Number *this,int x, int y) {
if ((c[x][y - 1] == 0) || (c[x + 1][y - 1] != 0)){
this->numberOfmove++;
printf("move number:%d\n",this->numberOfmove);
printf("Fail.\n");
}
else {
c[x + 1][y - 1] = c[x - 1][y - 1];
c[x][y - 1] = 0;
c[x - 1][y - 1] = 0;
this->numberOfmove++;
this->numberOfelements--;
printf("move number:%d,leave number:%d\n",this->numberOfmove,this->numberOfelements);
}
};
void right(Number *this,int x, int y) {
if ((c[x - 1][y] == 0) || (c[x - 1][y + 1] != 0)){
this->numberOfmove++;
printf("move number:%d\n",this->numberOfmove);
printf("Fail.\n");
}
else {
c[x - 1][y + 1] = c[x - 1][y - 1];
c[x - 1][y] = 0;
c[x - 1][y - 1] = 0;
this->numberOfmove++;
this->numberOfelements--;
printf("move number:%d,leave number:%d\n",this->numberOfmove,this->numberOfelements);
}
};
void left(Number *this,int x, int y) {
if ((c[x - 1][y - 2] == 0) || (c[x - 1][y - 3] != 0)){
this->numberOfmove++;
printf("move number:%d\n",this->numberOfmove);
printf("Fail.\n");
}
else {
c[x - 1][y - 3] = c[x - 1][y - 1];
c[x - 1][y - 2] = 0;
c[x - 1][y - 1] = 0;
this->numberOfmove++;
this->numberOfelements--;
printf("move number:%d,leave numbers:%d\n",this->numberOfmove,this->numberOfelements);
}
};
void title(Number* this) {
int a;
printf("Select Mode:(1.Tutorial,2.Game start)\n");
scanf("%d", &a);
if (a == 1) {
system("clear");
tuto(this);
}
if (a == 2) {
system("clear");
start_game(this);
}
}
void check_key(Number* this) {
int x, y, a, b;
void(*func[])(Number*,int,int) = {down, right,up,left};
while(1){
printf("Select Number (x,y):");
scanf("%d %d", &y, &x);
if (c[x-1][y-1]==0)
printf("fail\n");
else
break;
}
printf("Move Number(1.down 2.right 3.up 4.left):");
scanf("%d", &a);
func[a-1](this,x,y);
fflush(stdin);
}
void set_sol(void) {
printf(" "); printf("+---+---+---+\n");
printf(" "); printf("| %d | %d | %d |\n",c[0][2],c[0][3],c[0][4]);
printf(" "); printf("+---+---+---+\n");
printf(" "); printf("+---+---+---+\n");
printf(" "); printf("| %d | %d | %d |\n",c[1][2],c[1][3],c[1][4]);
printf(" "); printf("+---+---+---+\n");
printf("+---+---+"); printf("+---+---+---+"); printf("+---+---+\n");
printf("| %d | %d |",c[2][0],c[2][1]); printf("| %d | %d | %d |",c[2][2],c[2][3],c[2][4]); printf("| %d | %d |\n",c[2][5],c[2][6]);
printf("+---+---+"); printf("+---+---+---+"); printf("+---+---+\n");
printf("+---+---+"); printf("+---+---+---+"); printf("+---+---+\n");
printf("| %d | %d |",c[3][0],c[3][1]); printf("| %d | %d | %d |",c[3][2],c[3][3],c[3][4]); printf("| %d | %d |\n",c[3][5],c[3][6]);
printf("+---+---+"); printf("+---+---+---+"); printf("+---+---+\n");
printf("+---+---+"); printf("+---+---+---+"); printf("+---+---+\n");
printf("| %d | %d |",c[4][0],c[4][1]); printf("| %d | %d | %d |",c[4][2],c[4][3],c[4][4]); printf("| %d | %d |\n",c[4][5],c[4][6]);
printf("+---+---+"); printf("+---+---+---+"); printf("+---+---+\n");
printf(" "); printf("+---+---+---+\n");
printf(" "); printf("| %d | %d | %d |\n",c[5][2],c[5][3],c[5][4]);
printf(" "); printf("+---+---+---+\n");
printf(" "); printf("+---+---+---+\n");
printf(" "); printf("| %d | %d | %d |\n",c[6][2],c[6][3],c[6][4]);
printf(" "); printf("+---+---+---+\n");
return;
}
void tuto(Number* this) {
c[3][2] = 3; c[3][3] = 4 ;
set_sol();
while (1) {
check_key(this);
set_sol();
if (this->numberOfelements ==32)
break;
}
printf("clear\n");
}
void start_game(Number* this) {
FILE *fp=fopen("data","rb");
/* c[0][2] = 3, c[0][3] = 4, c[0][4] = 5; c[1][2] = 3, c[1][3] = 4, c[1][4] = 5;
c[2][0] = 1, c[2][1] = 2, c[2][2] = 3, c[2][3] = 4, c[2][4] = 5, c[2][5] = 6, c[2][6] = 7;
c[3][0] = 1, c[3][1] = 2, c[3][2] = 3, c[3][3] = 0, c[3][4] = 5, c[3][5] = 6, c[3][6] = 7;
c[4][0] = 1, c[4][1] = 2, c[4][2] = 3, c[4][3] = 4, c[4][4] = 5, c[4][5] = 6, c[4][6] = 7;
c[5][2] = 3, c[5][3] = 4, c[5][4] = 5; c[6][2] = 3, c[6][3] = 4, c[6][4] = 5;
fwrite(c,sizeof(*c),49,fp);
fclose(fp);
fp=fopen("data","rb");*/
size_t ret_code =fread(c,sizeof(*c),49,fp);
set_sol();
int k=0;
while (1) {
check_key(this);
set_sol();
if (this->numberOfelements==1)
break;
}
fclose(fp);
}
int main(void) {
Number * this=newNumber(0,33);
title(this);
return 0;
free(this);
}