-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.cpp
More file actions
277 lines (245 loc) · 5.35 KB
/
chess.cpp
File metadata and controls
277 lines (245 loc) · 5.35 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
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
#include "chess.h"
#include "util.h"
#include "piece.h"
void titleDraw() {
printf("\n\n\n\n");
printf(" ##### ## ## ##### ##### #####\n");
printf(" ## ## ## ## ## ## \n");
printf(" ## ####### #### ##### #####\n");
printf(" ## ## ## ## ## ##\n");
printf(" ##### ## ## ##### ##### #####\n");
}
int menuDraw() {
int x = 22, y = 12;
gotoxy(x - 2, y);
printf("> 게임 시작");
gotoxy(x, y + 1);
printf("게임 방법");
gotoxy(x, y + 2);
printf(" 종 료 ");
while (1) {
int n = keyControl();
switch (n) {
case UP:
if (y > 12) {
gotoxy(x - 2, y);
printf(" ");
gotoxy(x - 2, --y);
printf(">");
};
break;
case DOWN:
if (y < 14) {
gotoxy(x - 2, y);
printf(" ");
gotoxy(x - 2, ++y);
printf(">");
};
break;
case SUBMIT:
return y - 12;
}
}
return 1;
}
void infoDraw() {
system("cls");
printf("\n\n");
printf(" [ 조작법 ]\n\n");
printf(" 이동 : 방향 키보드\n");
printf(" 선택 : 스페이스바\n\n\n\n\n\n\n");
printf(" 개발자 : 김신영, 염승호\n\n");
printf(" 스페이스바를 누르면 메인화면으로 이동합니다.");
while (1) {
if (keyControl() == SUBMIT) {
break;
}
}
}
void boardDraw(Piece(*board)[8])
{
gotoxy(0, 0);
int forground, background;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i + j) % 2 == 0) background = green; //set background color
else background = brown;
if (board[i][j].exist == 1) //set forground color
forground = white;
else forground = black;
setColor(forground, background);
printf("%c", board[i][j].name); //출력
}
printf("\n");
}
setColor(white, black);
}
void userPos(int* x, int* y, Piece(*board)[8], int direction, int color)
{
//int a = board[*x][*y].pos[0], b = board[*x][*y].pos[1];
gotoxy(*x, *y);
if (direction != -1) {
SetPieceColor(board[*y][*x]);
printf("%c", board[*y][*x].name);
switch (direction)
{
case UP: gotoxy(*x, -- * y); break;
case DOWN: gotoxy(*x, ++ * y); break;
case LEFT: gotoxy(-- * x, *y); break;
case RIGHT: gotoxy(++ * x, *y); break;
}
}
if ((*x + *y) % 2 == 0) setColor(red, color); //set background color
else setColor(red, color);
printf("%c", board[*y][*x].name);
}
int startGame()
{
system("cls");
int turn = 1, key;
int x = 0, y = 0;
Piece(*board)[8]; //board 선언
Piece catchPiece;
board = pieceInit(); //board 초기화
boardDraw(board); //board 출력
//gotoxy(0, 10);
//printf("Turn of White"); // 초기 시작자의 색 표시.(이후에 턴 바뀌면서 알아서 사라짐)
switchTurn(&x, &y, &turn);
userPos(&x, &y, board, -1, white); //초기 userpos(0,0)
while (1)
{
int win = 0;
int success = 0;
//setColor(black, lightgray);
key = keyControl();
switch (key)
{
case UP:
if (y > 0)
{
userPos(&x, &y, board, UP, white);
}
break;
case DOWN:
if (y < 7)
{
userPos(&x, &y, board, DOWN, white);
}break;
case LEFT:
if (x > 0)
{
userPos(&x, &y, board, LEFT, white);
}
break;
case RIGHT:
if (x < 7)
{
userPos(&x, &y, board, RIGHT, white);
}
break;
case SUBMIT:
catchPiece = board[y][x];
if (turn == catchPiece.exist) //player에 맞는 색상의 기물 선택
{
switch (catchPiece.name)
{
case 'R':
success = Rook_move(board, &catchPiece, turn, 'R', &win);
break;
case 'B':
success = Bishop_move(board, &catchPiece, turn, 'B', &win);
break;
case 'N':
success = Knight_move(board, &catchPiece, turn, &win);
break;
case 'P':
success = Pawn_move(board, &catchPiece, turn, &win);
break;
case 'Q':
success = Queen_move(board, &catchPiece, turn, 'Q', &win);
break;
case 'K':
success = King_move(board, &catchPiece, turn, &win);
break;
}
setColor(white, black);
break;
}
else continue; //player에 맞는 색상의 기물 선택 못할경우 다시 처음으로 돌아가기
}
if (success != 1) //기물 움직임 구현에 성공하지 못할경우 다시 처음으로 돌아가기
continue;
en_passant_reset(board, turn);
if (win == 1)
return 1;
else if (win == -1)
return -1;
switchTurn(&x, &y, &turn); //turn 전환
setColor(white, black);
}
}
void switchTurn(int* x, int* y, int* turn)
{
*turn *= -1;
gotoxy(0, 10);
setColor(white, black);
if (*turn == 1)
{
printf("Turn of White");
}
else printf("Turn of Black");
gotoxy(*x, *y); //위치 복구
}
void promotion(Piece(*board)[8], Piece catchPiece2)
{
int y_pos = catchPiece2.pos[0];
int x_pos = catchPiece2.pos[1];
char promote_option[] = { 'P', 'R', 'N', 'B', 'Q' };
int x = 0;
gotoxy(0, 11);
setColor(white, black);
for (int i = 0; i < 5; i++)
{
printf("%c", promote_option[i]);
}
while (1)
{
int key = keyControl();
switch (key)
{
case LEFT:
if (x > 0)
{
gotoxy(x, 11);
setColor(white, black);
printf("%c", promote_option[x]);
setColor(black, white);
gotoxy(--x, 11);
printf("%c", promote_option[x]);
}
break;
case RIGHT:
if (x < 4)
{
gotoxy(x, 11);
setColor(white, black);
printf("%c", promote_option[x]);
setColor(black, white);
gotoxy(++x, 11);
printf("%c", promote_option[x]);
}
break;
case SUBMIT:
board[y_pos][x_pos].name = promote_option[x];
gotoxy(0, 11);
for (int i = 0; i < 5; i++)
{
setColor(white, black);
printf(" ");
}
return;
}
}
}
void writeNotation(int x, int y) {
}