-
Notifications
You must be signed in to change notification settings - Fork 0
/
players.c
241 lines (221 loc) · 5.98 KB
/
players.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
230
231
232
233
234
235
236
237
238
239
240
241
/* Michael Hanna, mwhanna
* CS 152, Spring 2017
* Project 2
*/
#include <stdlib.h>
#include <stdio.h>
#include "players.h"
/*returns a pseudo-boolean int for if the given char is a valid input*/
int valid_rc(char c){
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z');
}
/*converts from a row / column display character to the corresponding row /
column index*/
unsigned ctrc(char c){
if(c <= '9'){
return c - '0';
} else if(c <= 'Z'){
return c - 'A' + 10;
} else{
return c - 'a' + 36;
}
}
/*prompts the user for a move, telling them if the move is invalid, and returns
the valid move*/
pos human(game* g){
char c1, c2;
unsigned u1, u2;
int invalid_input, invalid_move;
pos p;
do{
printf("%s", (g->next)? "White: ": "Black: ");
scanf(" %c%c", &c1, &c2);
printf("Your input: r: %c, c: %c\n", c1, c2);
u1 = ctrc(c1);
u2 = ctrc(c2);
invalid_input = !in_bounds(g, u1, u2);
if(invalid_input){
printf("\nInput invalid; please input valid row / column #\n");
}
p = make_pos(u1, u2);
if(!invalid_input){
invalid_move = !outflanks(g, p);
if(invalid_move){
printf("\nInput move does not outflank; input valid move\n");
}
}
}while(invalid_input || invalid_move);
return make_pos(ctrc(c1), ctrc(c2));
}
/*given pos p, a valid direction (i.e. x and y that do ouflank), return the
number of pieces that will be outflanked in the x y direction from pos p*/
unsigned n_traverse (game* g, pos p, int x, int y){
square ts = (g->next)? WHITE: BLACK;
p.r += x;
p.c += y;
unsigned n = 0;
square ns = board_get(g->b, p);
while(ns != ts){
n++;
p.r += x;
p.c += y;
ns = board_get(g->b, p);
}
return n;
}
/*returns the number of outflanked pieces given a piece (of the color specified
by the turn in g) placed at p.*/
unsigned n_flips(game* g, pos p){
int i, j;
unsigned n = 0;
for(i = -1; i <= 1; i++){
for(j = -1; j <= 1; j++){
if(outflank_path(g, p, i, j)){//outflank_path in logic.c
n += n_traverse(g, p, i, j);
}
}
}
return n;
}
/*given a game, updates n to the number of flips of the highest-flipping valid
corner move, and p to the position thereof*/
void corners(game* g, unsigned* n, pos* p){
unsigned nr = g->b->nrows, nc = g->b->ncols;
pos tp = make_pos(0,0);
unsigned nf;
if(outflanks(g, tp)){
nf = n_flips(g, tp);
*p = tp;
*n = nf;
}
tp = make_pos(0, nc - 1);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
tp = make_pos(nr - 1, 0);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
tp = make_pos(nr - 1, nc - 1);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
}
/*given a game, updates n to the number of flips of the highest-flipping valid
edge move, and p to the position thereof*/
void edges(game* g, unsigned* n, pos* p){
unsigned nr = g->b->nrows, nc = g->b->ncols;
pos tp;
unsigned nf, i, j, rb = nr - 1, cb = nc - 1;
for(j = 1; j < cb; j++){
tp = make_pos(0, j);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
}
for(i = 1; i < rb; i++){
tp = make_pos(i, 0);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
tp = make_pos(i, cb);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
}
for(j = 1; j < cb; j++){
tp = make_pos(rb, j);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
}
}
/*given a game, updates n to the number of flips of the highest-flipping valid
edge move, and p to the position thereof*/
void interior(game* g, unsigned* n, pos* p){
unsigned nr = g->b->nrows, nc = g->b->ncols;
pos tp;
unsigned nf, i, j, rb = nr - 1, cb = nc - 1;
for(i = 1; i < cb; i++){
for(j = 1; j < rb; j++){
tp = make_pos(i, j);
if(outflanks(g, tp)){
nf = n_flips(g, tp);
if(nf > *n){
*p = tp;
*n = nf;
}
}
}
}
}
/*checks if the game is over, and returns the winner*/
int did_i_win(game* g){
if(!game_over(g)){
return 0;
}
unsigned go = game_outcome(g);
return (go < 2)? (go? -1: 1):0;
}
/*counts the number of pieces on the board, unweighted*/
int piece_counting(game* g){
unsigned black = 0, white = 0;
piece_count(g, &black, &white);//in logic.c
return black - white;
}
/*hard-copies a game*/
game* game_copy(game* g){
if(!g){
fprintf(stderr, "game_copy: game is null");
exit(1);
}
unsigned nrows = g->b->nrows, ncols = g->b->ncols;
game* ng = new_game(nrows, ncols, g->b->type);
ng->next = g->next;
unsigned i, j;
if(g->b->type == CELLS){
for(i = 0; i < nrows; i++){
for(j = 0; j < ncols; j++){
ng->b->u.cells[i][j] = g->b->u.cells[i][j];
}
}
} else {//copying a bitrep board is much faster
unsigned nrc = nrows*ncols, num_entries = nrc / 16;
if(nrc%16){
num_entries++;
}
for(i = 0; i < num_entries; i++){
ng->b->u.bits[i] = g->b->u.bits[i];
}
}
return ng;
}