-
Notifications
You must be signed in to change notification settings - Fork 1
/
Playmove.c
215 lines (161 loc) · 7.76 KB
/
Playmove.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Header.h"
int playmove(char **table, int n, struct player **play) {
char cplayer[50]; // cplayer: Color of player.
char dest_let, current_let, color, opp; // dest_let: destination letter,current_let:current letter of pawn, opp : opponent
int walls, dest_num, current_num, prev_x = 0, prev_y = 0, next_x = 0, next_y = 0; // dest_num: destination number,current_num: current number, prev-next: To have access in our board.
struct player *head;
int ch;
head = (*play);
scanf("%s", cplayer);
if (!strcmp(cplayer, "black")) {
color = 'B';
opp = 'W';
}
else if (!strcmp(cplayer, "white")) {
color = 'W';
opp = 'B';
}
else { // If playmove xxxx C5 or playmove xxxx xxxx given, xxxx means wrong arguments.
while ((ch = getchar()) != '\n'); // Read the whole "wrong" syntax line and discard it.
printf("\n? invalid syntax");
printf("\n\n");
return -1;
}
getchar(); // Ignore space.
scanf("%c%d", &dest_let, &dest_num); // Scan Destination.
if (dest_let < 65 || dest_let > (65 + n - 1) || dest_num > n || dest_num < 1) { // Check if cordinates given are valid
printf("\n? illegal move");
printf("\n\n");
return -1;
}
while (head) { // Find pawn's position
if (head->color == color && head->wall_num == 0) {
current_num = head->position_num;
current_let = head->position_let;
break;
}
else head = head->next;
}
head = (*play);
while (head) { // Keep previous walls for the new node
if (head->color == color) {
walls = head->r_walls;
break;
}
else head = head->next;
}
// Find cordinates of pawn in board
prev_x = (2 * n) - (2 * current_num) + 1;
prev_y = (4 * (current_let - 65)) + 2;
next_x = (2 * n) - (2 * dest_num) + 1; // Find cordinates of destination on board
next_y = (4 * (dest_let - 65)) + 2;
// Check if our move is valid ***Check text file***
if ((current_let == dest_let) && (current_num) == dest_num) {
printf("? illegal move\n\n");
return 1;
} // A mistake.
if ((dest_num > n) || (dest_num <= 0) || (dest_let < 65) || (dest_let > 65 + (n - 1))) {
printf("? illegal move\n\n");
return 1;
} // B mistake.
if ((current_num - dest_num) > 2 || (dest_num - current_num >2) || (dest_let - current_let > 2) || (current_let - dest_let > 2)) {
printf("? illegal move\n\n");
return 1;
} // C mistake.
if ((((current_num - dest_num == 2) && (table[prev_x + 2][prev_y] != opp)) || ((dest_num - current_num == 2) && (table[prev_x - 2][prev_y] != opp))) && (dest_let == current_let)) {
printf("? illegal move\n\n");
return 1;
} // D mistake.
if ((current_num == dest_num) && (table[prev_x][prev_y + 4] != opp) && (dest_let - current_let == 2)) {
printf("? illegal move\n\n");
return 1;
} // E mistake.
if ((current_num == dest_num) && (table[prev_x][prev_y - 4] != opp) && (current_let - dest_let == 2)) {
printf("? illegal move\n\n");
return 1;
} // F mistake.
if ((current_num - dest_num == 1) && (table[prev_x + 2][prev_y] == opp) && (dest_let == current_let)) {
printf("? illegal move\n\n");
return 1;
} // G mistake.
if ((dest_num -current_num == 1) && (table[prev_x - 2][prev_y] == opp) && (dest_let == current_let)) {
printf("? illegal move\n\n");
return 1;
} // H mistake.
if ((dest_num == current_num) && (table[prev_x][prev_y - 4] == opp) && (current_let - dest_let == 1)) {
printf("? illegal move\n\n");
return 1;
} // I mistake.
if ((dest_num == current_num) && (table[prev_x][prev_y + 4] == opp) && (dest_let - current_let == 1)) {
printf("? illegal move\n\n");
return 1;
} // J mistake.
if ((current_num - dest_num >= 1) && (dest_let == current_let) && (table[prev_x + 1][prev_y] == '=')) {
printf("? illegal move\n\n");
return 1;
} // K mistake.
if ((dest_num - current_num >= 1) && (dest_let == current_let) && (table[prev_x - 1][prev_y] == '=')) {
printf("? illegal move\n\n");
return 1;
} // L mistake.
if ((current_num == dest_num) && (dest_let - current_let >= 1) && (table[prev_x][prev_y + 2] == 'H')) {
printf("? illegal move\n\n");
return 1;
} // M mistake.
if ((current_num == dest_num) && (current_let - dest_let >= 1) && (table[prev_x][prev_y - 2] == 'H')) {
printf("? illegal move\n\n");
return 1;
} // N mistake.
if ((current_num - dest_num == 2) && (table[prev_x + 2][prev_y] == opp) && (dest_let == current_let) && (table[prev_x + 3][prev_y] == '=')) {
printf("? illegal move\n\n");
return 1;
} // O mistake.
if ((dest_num - current_num == 2) && (table[prev_x - 2][prev_y] == opp) && (dest_let == current_let) && (table[prev_x - 3][prev_y] == '=')) {
printf("? illegal move\n\n");
return 1;
} // P mistake.
if ((dest_num == current_num) && (table[prev_x][prev_y + 4] == opp) && (dest_let - current_let == 2) && (table[prev_x][prev_y + 6] == 'H')) {
printf("? illegal move\n\n");
return 1;
} // Q mistake.
if ((dest_num == current_num) && (table[prev_x][prev_y - 4] == opp) && (current_let - dest_let == 2) && (table[prev_x][prev_y - 6] == 'H')) {
printf("? illegal move\n\n");
return 1;
} // R mistake.
if ((current_num - dest_num == 1) && (current_let - dest_let == 1)) {
if (((table[prev_x][prev_y - 4] != opp) || (table[prev_x][prev_y - 6] != 'H') || (table[prev_x + 2][prev_y - 6] != 'H') || (table[prev_x + 1][prev_y - 4] == '=') || (table[prev_x][prev_y - 2] == 'H')) && ((table[prev_x + 2][prev_y] != opp) || (table[prev_x + 3][prev_y] != '=') || (table[prev_x + 3][prev_y - 4] != '=') || (table[prev_x + 2][prev_y - 2] == 'H') || (table[prev_x][prev_y - 2] == 'H') || (table[prev_x + 1][prev_y] == '='))) {
printf("? illegal move\n\n");
return 1;
}
} // S mistake.
if ((current_num - dest_num == 1) && (dest_let - current_let == 1)) {
if (((table[prev_x][prev_y + 4] != opp) || (table[prev_x][prev_y + 6] != 'H') || (table[prev_x + 2][prev_y + 6] != 'H') || (table[prev_x + 1][prev_y + 4] == '=') || (table[prev_x][prev_y + 2] == 'H')) && ((table[prev_x + 2][prev_y] != opp) || (table[prev_x + 3][prev_y] != '=') || (table[prev_x + 3][prev_y + 4] != '=') || (table[prev_x + 2][prev_y + 2] == 'H') || (table[prev_x][prev_y + 2] == 'H') || (table[prev_x + 1][prev_y] == '='))) {
printf("? illegal move\n\n");
return 1;
}
} // T mistake.
if ((dest_num - current_num == 1) && (current_let - dest_let == 1)) {
if (((table[prev_x][prev_y - 4] != opp) || (table[prev_x][prev_y - 6] != 'H') || (table[prev_x - 2][prev_y - 6] != 'H') || (table[prev_x - 1][prev_y - 4] == '=') || (table[prev_x][prev_y - 2] == 'H')) && ((table[prev_x - 2][prev_y] != opp) || (table[prev_x - 3][prev_y] != '=') || (table[prev_x - 3][prev_y - 4] != '=') || (table[prev_x - 2][prev_y - 2] == 'H') || (table[prev_x][prev_y - 2] == 'H') || (table[prev_x - 1][prev_y] == '='))) {
printf("? illegal move\n\n");
return 1;
}
} // U mistake.
if ((dest_num - current_num == 1) && (dest_let - current_let == 1)) {
if (((table[prev_x][prev_y + 4] != opp) || (table[prev_x][prev_y + 6] != 'H') || (table[prev_x - 2][prev_y + 6] != 'H') || (table[prev_x - 1][prev_y + 4] == '=') || (table[prev_x][prev_y + 2] == 'H')) && ((table[prev_x - 2][prev_y] != opp) || (table[prev_x - 3][prev_y] != '=') || (table[prev_x - 3][prev_y + 4] != '=') || (table[prev_x - 2][prev_y + 2] == 'H') || (table[prev_x][prev_y + 2] == 'H') || (table[prev_x - 1][prev_y] == '='))) {
printf("? illegal move\n\n");
return 1;
}
} // V mistake.
// We save our move to our list and our board, only if it's valid.//
table[prev_x][prev_y] = ' ';
table[next_x][next_y] = color;
save_playmove(color, dest_let, dest_num, play, walls);
printf("=\n\n");
return 0;
}
// Editors:
// sdi1500129.
// sdi1500195.