-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
230 lines (177 loc) · 5.2 KB
/
main.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
//
// main.c
// ChessEngine
//
// Created by Tina Raissi on 31/03/14.
// Copyright (c) 2014 Tina Raissi. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "ChessEngine.h"
void printBoard ();
int readNotation (char *moveNotation);
int readIndex (int indexType);
int controlNotation (char *moveNotation);
void victoryMessage(int victory);
int main(int argc, const char * argv[])
{
int origIndex, destIndex, origX, origY, destX, destY;
bool legalMove = false;
short unsigned int victory = VC_NO_VICTORY;
initialize();
printBoard();
while(!victory){
while (!legalMove){
origIndex = readIndex(ORIGINE);
destIndex = readIndex(DESTINATION);
transf_index2rel(origIndex, &origX, &origY);
transf_index2rel(destIndex, &destX, &destY);
if (isMoveLegal (origX, origY, destX, destY)){
executeMove(origX, origY, destX, destY);
printBoard();
victory = checkForVictory();
victoryMessage(victory);
setColor();
}
else printf("Illegal move, let's try again!\n");
}
}
printf("END\n");
return 0;
}
void printBoard (){
printf(" ----------------\n");
int x,y, absX, absY;
for (y = 8; y > 0; y--) {
putchar('1' + (y - 1));
for(x = 1; x < 9; x++) {
putchar('|');
if (getCurrentPlayer() == PT_BLACK){
absY = 9 - y;
}
else { absY = y;}
absX = x;
switch (getSquareContent(absX, absY)) {
case PC_PAWN_WHITE:
putchar('W');
break;
case PC_PAWN_BLACK:
putchar('B');
break;
case PC_EMPTY:
putchar('.');
break;
case PC_QUEEN_WHITE:
putchar('Q');
break;
case PC_QUEEN_BLACK:
putchar('Q');
break;
default:
break;
}
}
putchar('|');
putchar('\n');
}
printf(" ----------------\n");
printf(" A B C D E F G H\n");
return;
}
int readNotation (char *moveNotation){
int x, y;
if (strchr(moveNotation, 'a'))
x = 1;
else if (strchr(moveNotation, 'b'))
x = 2;
else if (strchr(moveNotation, 'c'))
x = 3;
else if (strchr(moveNotation, 'd'))
x = 4;
else if (strchr(moveNotation, 'e'))
x = 5;
else if (strchr(moveNotation, 'f'))
x = 6;
else if (strchr(moveNotation, 'g'))
x = 7;
else
x = 8;
if (strchr(moveNotation, '1'))
y = 1;
else if (strchr(moveNotation, '2'))
y = 2;
else if (strchr(moveNotation, '3'))
y = 3;
else if (strchr(moveNotation, '4'))
y = 4;
else if (strchr(moveNotation, '5'))
y = 5;
else if (strchr(moveNotation, '6'))
y = 6;
else if (strchr(moveNotation, '7'))
y = 7;
else
y = 8;
return transf_abs2array (x, y);
}
int readIndex (int indexType) {
int x, y, index = ERROR;
char moveNotation[3];
while (index < 0){
if (indexType == ORIGINE)
printf("enter the piece you want to move: \n");
else
printf("enter where you want to move to: \n");
scanf("%s", moveNotation);
index = readNotation (moveNotation, getCurrentPlayer());
transf_index2rel(index, &x, &y);
if (!controlNotation(moveNotation)){
printf("Invalid notation!\n");
index = ERROR;
}
if (indexType == ORIGINE){
if(getSquareContent(x,y) == PC_EMPTY){
printf("The square is empty!\n");
index = ERROR;}
else if (getSquareContent(x,y) != getCurrentPlayer()+1){
printf("You are moving the opposite color!\n");
index = ERROR;
}
}
}
return index;
}
int controlNotation (char* moveNotation){
int letter, number;
letter = moveNotation[0];
number = moveNotation[1];
if ((letter >= 97 && letter <= 104) && (number >= 49 && number <= 57) ){
return true;
}
else return false;
}
void victoryMessage(victory){
switch(victory){
case VC_DRAW:
printf("Draw!\n");
break;
case VC_VICTORY:
if (getCurrentPlayer() == PT_WHITE)
printf("White wins!\n");
else
printf("Black wins!\n");
break;
case VC_NO_VICTORY:
printf("The game continues\n");
if (getCurrentPlayer() == PT_WHITE)
printf("Now it's the turn of Black!\n");
else
printf("Now it's the turn of White!\n");
break;
default:
break;
}
return;
}