-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bishop.cpp
112 lines (97 loc) · 2.17 KB
/
Bishop.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
#include "StdAfx.h"
#include <vector>
#include "Piece.h"
#include "Location.h"
#include "Bishop.h"
#include "King.h"
#include "Board.h"
#include "Square.h"
CBishop::CBishop(EColour theColour, CBoard* pBoard, short thePosition)
: CPiece(theColour, pBoard, thePosition)
{
}
CBishop::~CBishop(void)
{
}
bool CBishop::isMoveLegal(short xrow, short yrow, short xcolumn, short ycolumn)
{
bool bResult = false;
short rowDiff = -1;
short columnDiff = -1;
// diagonal movement verification
if ( (yrow > xrow) && (ycolumn > xcolumn) )
{
rowDiff = yrow - xrow;
columnDiff = ycolumn - xcolumn;
if (rowDiff == columnDiff)
{
bResult = true;
}
}
else if ( (yrow < xrow) && (ycolumn > xcolumn) )
{
rowDiff = xrow - yrow;
columnDiff = ycolumn - xcolumn;
if (rowDiff == columnDiff)
{
bResult = true;
}
}
else if ( (yrow > xrow) && (ycolumn < xcolumn) )
{
rowDiff = yrow - xrow;
columnDiff = xcolumn - ycolumn;
if (rowDiff == columnDiff)
{
bResult = true;
}
}
else if ( (yrow < xrow) && (ycolumn < xcolumn) )
{
rowDiff = xrow - yrow;
columnDiff = xcolumn - ycolumn;
if (rowDiff == columnDiff)
{
bResult = true;
}
}
return bResult;
}
bool CBishop::isMoveBlocked(short xrow, short yrow, short xcolumn, short ycolumn)
{
bool bResult = false;
short rowDiff = -1;
short i = 0;
// we are assuming the diference in rows equals the difference in columns since we passed the isLegalMove() test
// now test for any piece in the diagonal path of the move
if ( (yrow > xrow) )
{
rowDiff = yrow - xrow;
for (i = xrow + 1; i < rowDiff; ++i)
{
if (pBoard->getSquares()[getPositionFromRowAndColumn(i, i)]->isSquareOccupied())
{
bResult = true;
break;
}
}
}
else if ( yrow < xrow )
{
rowDiff = xrow - yrow;
for (i = yrow + 1; i < rowDiff; ++i)
{
if (pBoard->getSquares()[getPositionFromRowAndColumn(i, i)]->isSquareOccupied())
{
bResult = true;
break;
}
}
}
// same colour piece on target square is a block
if (isTargetSquareBlocked(yrow, ycolumn))
{
bResult = true;
}
return bResult;
}