-
Notifications
You must be signed in to change notification settings - Fork 11
/
boardc5.cpp
193 lines (148 loc) · 5.04 KB
/
boardc5.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
#include "boardc5.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <cstdlib>
MoveC5::MoveC5(Token player,Size column,Size row) : Move(player), column(column), row(row) {}
void MoveC5::print() const {
if (player!=NOT_PLAYED) std::cout<<"column "<<this->column<<" row "<<this->row<<" for player "<<player;
else std::cout<<"c5 null move";
}
Move *MoveC5::deepcopy() const {
Move *copy=new MoveC5(player,column,row);
return copy;
}
bool MoveC5::compare(const Move& abstract_move) const {
const MoveC5 &move=dynamic_cast<const MoveC5&>(abstract_move);
return Move::compare(abstract_move) and column==move.column and row==move.row;
}
BoardC5::BoardC5(Size width,Size height,Size win_length) : lastmove(NOT_PLAYED,0,0), width(width), height(height), win_length(win_length), size(width*height), played_count(0) {
//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);
}
BoardC5::~BoardC5() {
delete [] tokens;
delete [] flat;
}
Board *BoardC5::deepcopy() const {
auto *copy=new BoardC5(width,height,win_length);
//copy last move and played_count
copy->lastmove=lastmove;
copy->played_count=played_count;
//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 *BoardC5::parse_move_string(Token player,const char *string) const {
std::stringstream stream(std::stringstream::in | std::stringstream::out);
int column=-1,row=-1;
stream<<string;
stream>>column>>row;
if (stream.fail()) return nullptr;
Move *move=new MoveC5(player,column,row);
if (is_move_valid(*move)) return move;
delete move;
return NULL;
}
void BoardC5::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;
}
bool BoardC5::is_move_valid(const Move &abstract_move) const {
const MoveC5 &move=dynamic_cast<const MoveC5&>(abstract_move);
return move.player!=NOT_PLAYED and move.column>=0 and move.column<width and move.row>=0 and move.row<height and tokens[move.column][move.row]==NOT_PLAYED;
}
Moves BoardC5::get_possible_moves(Token player) const {
Moves moves;
for (Size column=0; column<width; column++) {
for (Size row=0; row<height; row++) {
if (tokens[column][row]==NOT_PLAYED) moves.push_back(new MoveC5(player,column,row));
}
}
return moves;
}
void BoardC5::play_move(const Move &abstract_move) {
const MoveC5 &move=dynamic_cast<const MoveC5&>(abstract_move);
assert(this->is_move_valid(move));
tokens[move.column][move.row]=move.player;
played_count++;
lastmove=move;
}
bool BoardC5::play_random_move(Token player) {
if (played_count<size) {
Moves possible_moves=get_possible_moves(player);
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 (Moves::iterator iter=possible_moves.begin(); iter!=possible_moves.end(); iter++) delete *iter;
return true;
} else {
//std::cout<<"board full"<<std::endl;
return false;
}
}
Token BoardC5::check_for_win() const {
Size column=lastmove.column;
Size row=lastmove.row;
if (propagate(row,column,1,0,lastmove.player)+propagate(row,column,-1,0,lastmove.player)>win_length) return lastmove.player;
if (propagate(row,column,0,1,lastmove.player)+propagate(row,column,0,-1,lastmove.player)>win_length) return lastmove.player;
if (propagate(row,column,1,1,lastmove.player)+propagate(row,column,-1,-1,lastmove.player)>win_length) return lastmove.player;
if (propagate(row,column,1,-1,lastmove.player)+propagate(row,column,-1,1,lastmove.player)>win_length) return lastmove.player;
return NOT_PLAYED;
}
Size BoardC5::propagate(Size row,Size column,Size drow,Size dcolumn,Token player) const {
Size length=0;
while (row>=0 and row<height and column>=0 and column<width and tokens[column][row]==player) {
length++;
row+=drow;
column+=dcolumn;
}
return length;
}