-
Notifications
You must be signed in to change notification settings - Fork 0
/
queen.cpp
143 lines (119 loc) · 3.25 KB
/
queen.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
/*queen.cpp - Written by Michael Wermert and Pengda Xie
* Description - This file contains the method definitions for the queen class
* declared at piece.h .
*/
#include "piece.h"
using namespace std;
//constructor
Queen::Queen(int row, int col, char color):Piece(row, col, color, 'Q', 90)
{
}
void Queen::ValidMoves(multimap<int, int> &moves, const vector<vector<Piece*> > &board)
{
moves.clear();
//check left
for(int i = this->col - 1; i >= 0; --i)
{
if(board[row][i] == nullptr)
moves.insert(make_pair(this->row, i));
else
{
if(board[row][i]->GetColor() != this->piece_color)
moves.insert(make_pair(this->row, i));
break;
}
}
//check right
for(unsigned int i = this->col + 1; i < board[this->row].size(); ++i)
{
if(board[row][i] == nullptr)
moves.insert(make_pair(this->row, i));
else
{
if(board[row][i]->GetColor() != this->piece_color)
moves.insert(make_pair(this->row, i));
break;
}
}
//check top
for(int i = this->row - 1; i >= 0; i--)
{
if(board[i][this->col] == nullptr)
moves.insert(make_pair(i, this->col));
else
{
if(board[i][this->col]->GetColor() != this->piece_color)
moves.insert(make_pair(i, this->col));
break;
}
}
//check bottom
for( int i = this->row + 1; i < 8 ;i++)
{
if(board[i][this->col] == nullptr)
moves.insert(make_pair(i, this->col));
else
{
if(board[i][this->col]->GetColor() != this->piece_color)
moves.insert(make_pair(i, this->col));
break;
}
}
int r, c;
//check top left
for(r = this->row - 1, c = this->col - 1; r >= 0 && c >= 0;)
{
if(board[r][c] == nullptr)
moves.insert(make_pair(r, c));
else
{
if(board[r][c]->GetColor() != this->piece_color)
moves.insert(make_pair(r, c));
break;
}
--r;
--c;
}
//check top right
for(r = this->row - 1, c = this->col + 1; r >= 0 && c < (int)board.size();)
{
if(board[r][c] == nullptr)
moves.insert(make_pair(r, c));
else
{
if(board[r][c]->GetColor() != this->piece_color)
moves.insert(make_pair(r, c));
break;
}
--r;
++c;
}
//check bottom left
for(r = this->row + 1, c = this->col - 1; r < (int)board.size() && c >= 0;)
{
if(board[r][c] == nullptr)
moves.insert(make_pair(r, c));
else
{
if(board[r][c]->GetColor() != this->piece_color)
moves.insert(make_pair(r, c));
break;
}
++r;
--c;
}
//check bottom right
for(r = this->row + 1, c = this->col + 1; r < (int)board.size() && c < (int)board.size();)
{
if(board[r][c] == nullptr)
moves.insert(make_pair(r, c));
else
{
if(board[r][c]->GetColor() != this->piece_color)
moves.insert(make_pair(r, c));
break;
}
++r;
++c;
}
}