-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.cpp
More file actions
64 lines (57 loc) · 1.44 KB
/
Rook.cpp
File metadata and controls
64 lines (57 loc) · 1.44 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
#include "pch.h"
#include "Rook.h"
#include "board.h"
bool Rook::move(Piece* boardMove[8][8], int srcX, int srcY, int destX, int destY)
{
Piece* src = boardMove[srcX][srcY];
Piece* dest = boardMove[destX][destY];
bool invalid = false;
if (srcX != destX || srcY != destY)
{
if (srcX == destX)
{
int yIncrement = (destY - srcY) / (abs(destY - srcY));
for (int i = srcY + yIncrement; i != destY; i += yIncrement)
{
if (boardMove[destX][i]->getColor() != 'N')
return false;
}
}
else
if (srcY == destY)
{
int xIncrement = (destX - srcX) / (abs(destX - srcX));
for (int i = srcX + xIncrement; i != destX; i += xIncrement)
{
if (boardMove[i][destY]->getColor() != 'N')
return false;
}
}
else
return false;
}
if (invalid == false)
{
if (src->getColor() == 'W') //if piece is white
{
delete boardMove[srcX][srcY];
delete boardMove[destX][destY];
boardMove[srcX][srcY] = new EmptyCell('N', 'E');
boardMove[destX][destY] = new Rook('W', 'R');
return true;
}
else //if piece is black
{
delete boardMove[srcX][srcY];
delete boardMove[destX][destY];
boardMove[srcX][srcY] = new EmptyCell('N', 'E');
boardMove[destX][destY] = new Rook('B', 'R');
return true;
}
}
else
{//Return some erorr or something. Probably return false;
std::cout << "That is an invalid move for rook";
return false;
}
}