-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecker.cpp
More file actions
129 lines (110 loc) · 3.88 KB
/
Checker.cpp
File metadata and controls
129 lines (110 loc) · 3.88 KB
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
//
// Created by Ziyang Zhang on 2019-05-01.
//
#include "Checker.h"
#include "Board.h"
const map<string , string> Checker::opponent = {{"W","B"},{"B","W"}};
Checker::Checker(string color, int row, int col) {
color = (int)color[0] <= 90? color : string("")+(char) ((int)color[0]-32);
this->color = color;
this->row = row;
this->col = col;
this->isKing = false;
}
void Checker::becomeKing(){
this->isKing = true;
};
void Checker::becomeMan(){
this->isKing = false;
};
string Checker::toString() {
if (isKing){
return color;
}
else
{
return color == "." ? color : string("")+(char) ((int)color[0]+32);
}
}
vector<Move> Checker::getPossibleMoves(Board* board)
{
Direction direction;
vector<Move> result;
if (this->color == ".")
{
return result;
}
vector<vector<Position>> multiple_jump;
vector<Position> explore_direction = direction[color];
if (isKing)
{
for (int i = 0; i<direction[opponent.at(color)].size(); i++)
{
explore_direction.push_back(direction[opponent.at(color)][i]);
}
}
for (auto i = explore_direction.begin(); i != explore_direction.end(); i++) {
int pos_x = row+(*i)[0];
int pos_y = col+(*i)[1];
if (board->isInBoard(pos_x,pos_y))
{
if (board->board[pos_x][pos_y].color == ".")
{
result.emplace_back(Move(vector<Position>{Position(row,col),Position(pos_x,pos_y)}));
}
}
}
vector<Position> temp_v;
string self_color = board->board[row][col].color;
board->board[row][col].color = ".";
binary_tree_traversal(row,col,multiple_jump, *board, explore_direction, temp_v,self_color);
if (!multiple_jump.empty())
{
result.clear();
}
for (auto jump = multiple_jump.begin(); jump != multiple_jump.end(); jump++) {
(*jump).insert((*jump).begin(),Position(row,col));
result.emplace_back(Move(*jump));
}
board->board[row][col].color = self_color;
return result;
}
void Checker::binary_tree_traversal(int pos_x,int pos_y,vector<vector<Position>> & multiple_jump,Board& board,vector<Position> direction,vector<Position>move, string self_color) {
bool flag = true;
for (auto i = direction.begin(); i != direction.end(); i++) {
int temp_x = pos_x + (*i)[0];
int temp_y = pos_y + (*i)[1];
Position temp(temp_x, temp_y);
if (board.isInBoard(temp_x, temp_y)
&& board.board[temp_x][temp_y].color == opponent.at(self_color)
&& board.isInBoard(temp_x + (*i)[0], temp_y + (*i)[1])
&& board.board[temp_x + (*i)[0]][temp_y + (*i)[1]].color == ".")
{
flag = false;
break;
}
}
if (flag) {
if (!move.empty()) {
multiple_jump.push_back(move);
return;
}
}
for (auto i = direction.begin(); i != direction.end(); i++) {
int temp_x = pos_x + (*i)[0];
int temp_y = pos_y + (*i)[1];
Position temp(temp_x, temp_y);
if (board.isInBoard(temp_x, temp_y) && board.board[temp_x][temp_y].color == opponent.at(self_color)){
if (board.isInBoard(pos_x + (*i)[0] + (*i)[0], pos_y + (*i)[1] + (*i)[1]) &&
board.board[pos_x + (*i)[0] + (*i)[0]][pos_y + (*i)[1] + (*i)[1]].color == ".") {
Position temptemp{pos_x + (*i)[0] + (*i)[0], pos_y + (*i)[1] + (*i)[1]};
string backup = board.board[pos_x + (*i)[0]][pos_y + (*i)[1]].color;
board.board[pos_x + (*i)[0]][pos_y + (*i)[1]].color = ".";
move.push_back(temptemp);
this->binary_tree_traversal(temptemp[0], temptemp[1], multiple_jump, board, direction, move,self_color);
move.pop_back();
board.board[pos_x + (*i)[0]][pos_y + (*i)[1]].color = backup;
}
}
}
}