-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathboardothello.cpp
312 lines (244 loc) · 8.36 KB
/
boardothello.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "boardothello.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <cstdlib>
MoveOthello::MoveOthello(Token player) : Move(player), column(0), row(0), can_play(false) {}
MoveOthello::MoveOthello(Token player,Size column,Size row) : Move(player), column(column), row(row), can_play(true) {}
void MoveOthello::print() const {
if (player!=NOT_PLAYED){
switch (can_play) {
case true:
std::cout<<"column "<<this->column<<" row "<<this->row;
break;
case false:
std::cout<<"can't play";
}
std::cout<<" for player "<<player;
}
else std::cout<<"othello null move";
}
Move *MoveOthello::deepcopy() const {
Move *copy= nullptr;
if (can_play) copy=new MoveOthello(player,column,row); //normal move
else copy=new MoveOthello(player); // "cant play" move
return copy;
}
bool MoveOthello::compare(const Move& abstract_move) const {
const auto &move=dynamic_cast<const MoveOthello&>(abstract_move);
return Move::compare(abstract_move) and column==move.column and row==move.row and can_play==move.can_play;
}
BoardOthello::BoardOthello(Size width,Size height) : width(width), height(height), size(width*height), played_count(4), cant_play_count(0), player_1_score(2), player_2_score(2) {
//allocate flat
flat=new Token[size];
for (Token *iter=flat; iter!=flat+size; iter++) *iter=NOT_PLAYED;
//allocate column pointer and playable move cache
tokens=new Token*[width];
Size k=0;
for (Token *iter=flat; iter<flat+size; iter+=height) {
tokens[k]=iter;
k++;
}
assert(k==width);
//init board
tokens[width/2-1][height/2-1]=PLAYER_1;
tokens[width/2][height/2]=PLAYER_1;
tokens[width/2-1][height/2]=PLAYER_2;
tokens[width/2][height/2-1]=PLAYER_2;
//score are updated in instance creation
}
BoardOthello::~BoardOthello() {
delete [] tokens;
delete [] flat;
}
Board *BoardOthello::deepcopy() const {
auto *copy=new BoardOthello(width,height);
//copy internal data
copy->played_count=played_count;
copy->cant_play_count=cant_play_count;
copy->player_1_score=player_1_score;
copy->player_2_score=player_2_score;
//copy flat
const Token *current_iter=flat;
for (Token *iter=copy->flat; iter!=copy->flat+size; iter++) {
*iter=*current_iter;
current_iter++;
}
return copy;
}
Move *BoardOthello::parse_move_string(Token player,const char *string) const {
std::stringstream stream(std::stringstream::in | std::stringstream::out);
if (std::string(string)=="no") { //"no play" move
Move *move=new MoveOthello(player);
if (is_move_valid(*move)) return move;
delete move;
return nullptr;
} else { //normal move
int column=-1,row=-1;
stream<<string;
stream>>column>>row;
if (stream.fail()) return nullptr;
Move *move=new MoveOthello(player,column,row);
if (is_move_valid(*move)) return move;
delete move;
return nullptr;
}
}
void BoardOthello::print() const {
std::cout<<" ";
for (Size column=0; column<width; column++) std::cout<<column;
std::cout<<std::endl;
std::cout<<" +";
for (Size column=0; column<width; column++) std::cout<<"-";
std::cout<<"+"<<std::endl;
for (Size row=0; row<height; row++) {
std::cout<<row<<"|";
for (Size column=0; column<width; column++) {
switch(tokens[column][row]) {
case NOT_PLAYED:
std::cout<<" ";
break;
case PLAYER_1:
std::cout<<"x";
break;
case PLAYER_2:
std::cout<<"o";
break;
}
}
std::cout<<"|"<<row<<std::endl;
}
std::cout<<" +";
for (Size column=0; column<width; column++) std::cout<<"-";
std::cout<<"+"<<std::endl;
std::cout<<" ";
for (Size column=0; column<width; column++) std::cout<<column;
std::cout<<std::endl;
std::cout<<"p1(x) "<<player_1_score<<" p2(o) "<<player_2_score<<std::endl; //<<" x: "<<cant_play_count<<" "<<played_count<<" "<<size<<std::endl;
}
bool BoardOthello::propagate(Token player, Size column, Size row, Size dcolumn, Size drow) const {
Token enemy=other_player(player);
Size length=0;
//check enemy token
while (row>=0 and row<height and column>=0 and column<width and tokens[column][row]==enemy) {
length++;
column+=dcolumn;
row+=drow;
}
if (not length) return false;
//check for a own token at the end
if (row>=0 and row<height and column>=0 and column<width and tokens[column][row]==player) return true;
return false;
}
bool BoardOthello::can_play_normal_move_at(Token player, Size column, Size row) const {
if (tokens[column][row]!=NOT_PLAYED) return false;
//horizontal and vertical
if (propagate(player,column+1,row,1,0)) return true;
if (propagate(player,column-1,row,-1,0)) return true;
if (propagate(player,column,row+1,0,1)) return true;
if (propagate(player,column,row-1,0,-1)) return true;
//diagonals
if (propagate(player,column+1,row+1,1,1)) return true;
if (propagate(player,column+1,row-1,1,-1)) return true;
if (propagate(player,column-1,row-1,-1,-1)) return true;
if (propagate(player,column-1,row+1,-1,1)) return true;
return false;
}
bool BoardOthello::can_play_normal_moves(Token player) const {
for (Size column=0; column<width; column++) {
for (Size row=0; row<height; row++) {
if (can_play_normal_move_at(player,column,row)) return true;
}
}
return false;
}
bool BoardOthello::is_move_valid(const Move &abstract_move) const {
const auto &move=dynamic_cast<const MoveOthello&>(abstract_move);
if (move.can_play) return can_play_normal_move_at(move.player,move.column,move.row); //just check if move is valid
else return not can_play_normal_moves(move.player); //a cant play is invalid if you can play any normal move
}
Moves BoardOthello::get_possible_moves(Token player) const {
Moves moves;
for (Size column=0; column<width; column++) {
for (Size row=0; row<height; row++) {
if (can_play_normal_move_at(player,column,row)) moves.push_back(new MoveOthello(player,column,row));
}
}
//if cant play normal moves and not player 2 "cant play" in a row then add a cant play moves
if (moves.empty() and cant_play_count<3) moves.push_back(new MoveOthello(player));
return moves;
}
Size BoardOthello::switch_tokens(Token player,Size column, Size row, Size dcolumn, Size drow) {
if (not propagate(player,column,row,dcolumn,drow)) return 0; //FIXME should write a not_propagate for performance
Token enemy=other_player(player);
Size length=0;
//switch enemy token
while (row>=0 and row<height and column>=0 and column<width and tokens[column][row]==enemy) {
tokens[column][row]=player;
length++;
column+=dcolumn;
row+=drow;
}
return length;
}
void BoardOthello::play_move(const Move &abstract_move) {
const auto &move=dynamic_cast<const MoveOthello&>(abstract_move);
assert(this->is_move_valid(move));
if (move.can_play) {
tokens[move.column][move.row]=move.player;
int dscore=0;
Token player=move.player;
Size column=move.column;
Size row=move.row;
//horizontal and vertical
dscore+=switch_tokens(player,column+1,row,1,0);
dscore+=switch_tokens(player,column-1,row,-1,0);
dscore+=switch_tokens(player,column,row+1,0,1);
dscore+=switch_tokens(player,column,row-1,0,-1);
//diagonals
dscore+=switch_tokens(player,column+1,row+1,1,1);
dscore+=switch_tokens(player,column+1,row-1,1,-1);
dscore+=switch_tokens(player,column-1,row-1,-1,-1);
dscore+=switch_tokens(player,column-1,row+1,-1,1);
//update scores
if (move.player==PLAYER_1) {
player_1_score+=dscore+1;
player_2_score-=dscore;
} else {
player_2_score+=dscore+1;
player_1_score-=dscore;
}
played_count++;
cant_play_count=0;
} else {
cant_play_count++;
}
}
bool BoardOthello::play_random_move(Token player) {
if (played_count<size) {
Moves possible_moves=get_possible_moves(player);
if (possible_moves.empty()) return false; //two "cant play moves in a row"
int selected= static_cast<int>(rand() / (RAND_MAX + 1.0) * possible_moves.size());
Moves::const_iterator selected_iter=possible_moves.begin();
while (selected>0) {
selected--;
selected_iter++;
}
play_move(**selected_iter);
//play_move(*selected);
//Move *selected=possible_moves[rand()%possible_moves.size()];
//play_move(*selected);
for (auto &possible_move : possible_moves) delete possible_move;
return true;
}
//the board is full
//std::cout<<"board full"<<std::endl;
return false;
}
Token BoardOthello::check_for_win() const {
if (played_count<size and cant_play_count<2) return NOT_PLAYED; //go on the game isn't finished
else {
if (player_1_score>=player_2_score) return PLAYER_1; //FIXME if score equals player1 win
return PLAYER_2;
}
}