-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bishop.cpp
54 lines (45 loc) · 1.02 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
#include "Bishop.h"
#include "Board.h"
//! Basic constructor.
/**
@param color Black or white.
@param x X coordinate to start at.
@param y Y coordinate to start at.
*/
Bishop::Bishop(Color color, int x, int y) : Piece(color, x, y, BISHOP)
{
}
//! Destructor.
Bishop::~Bishop()
{
}
bool Bishop::pinning(int x, int y)
{
bool pinning = true;
Position diff;
diff.x = x - getPos().x;
diff.y = y - getPos().y;
// Used for increments
int norm_x = (diff.x == 0 ? 0 : diff.x / abs(diff.x));
int norm_y = (diff.y == 0 ? 0 : diff.y / abs(diff.y));
// Can only move diagonally
if(abs(diff.x) == abs(diff.y))
{
for(int counter = 1; counter < abs(diff.x); counter++)
{
if(getBoard()->getPieceAt(getPos().x + counter * norm_x, getPos().y + counter * norm_y) != NULL) {
pinning = false;
break;
}
}
}
else
pinning = false;
// Check the last piece, which is (x,y)
if(pinning)
{
if(getBoard()->getPieceAt(x, y) != NULL && getBoard()->getPieceAt(x, y)->getColor() == getColor())
pinning = false;
}
return pinning;
}