-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueen.cpp
More file actions
42 lines (37 loc) · 1.66 KB
/
Queen.cpp
File metadata and controls
42 lines (37 loc) · 1.66 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
#include <iostream>
#include <string>
#include "Queen.h"
// constructor
Queen::Queen(int _row, int _col, bool _isWhite, std::string _name): Piece (_row, _col, _isWhite, _name) {ptype = "Queen";}
// destructor
Queen::~Queen(){std::cout << "Destroying queen" << std::endl;}
/*
Boolean method to see if the Queen can move to the specified location from its current location.
*/
bool Queen::isLegalMoveTo(int _row, int _col)
{ // _row, _col is the "to" location of our desired move
if (abs(row - _row) != (abs(col - _col)) && (row != _row && col != _col)) // can move diagonally
{
/*
If it does not move diagonally, it can move vertically or horizontally, but not both.
*/
if (row != _row && col != _col)
return false;
}
else if (!board->isClear(row, col, _row, _col)) // are there pieces blocking this potential move?
return false;
/*
Check if the final location is occupied by a same-colored piece. If yes, then we are blocked by our own piece. If no, then we have a capture – which will be handled by the Piece :: moveTo ( ) method.
*/
Piece* endPiece = board->pieceAt(_row, _col); // see if there is a piece at the "to" location
if (endPiece != nullptr)
{ // check if the piece at the "to" location is of the same color
if (endPiece->isWhite == isWhite)
{
std::cout << "queen.isLegalMoveTo(): This move is blocked at the to location by a same-colored piece!" << std::endl;
return false;
} // cannot move to or capture our own piece!
return true;
} // done checking piece at "to" location
return true;
}