-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRook.cpp
61 lines (54 loc) · 1.49 KB
/
Rook.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
#pragma once
#include "Chess.h"
#include"Piece.h"
#include <iostream>
using namespace std;
class Rook :virtual public Piece
{
public:
Rook(bool c) : Piece(c, 4) // Moves are set to zero when a piece is constructed
{
PieceImage.setTextureRect(IntRect(ImageSize * 0, ExtractColor(color) * ImageSize, ImageSize, ImageSize));
}
Rook() {};
bool isValid(int xfrom, int yfrom, int xto, int yto, Piece* b[8][8])
{
if (Piece::isValid(xfrom, yfrom, xto, yto, b))
{
if (xto - xfrom == 0) // Move in a vertical line
{
int col_change = (yfrom < yto ? 1 : -1);
// For moving forward in the board
for (int i = yfrom + col_change; i != yto; i += col_change) // If there is no obstacle in the way
{
if (b[i][xfrom]->getValue() != 0)
{
return false;
}
}
move = true; // The piece has moved so castling is not possible
return true;
}
if (yto - yfrom == 0) // Move in a horizontal line
{
int row_change = (xfrom < xto ? 1 : -1);
// For moving forward in the board
for (int i = (xfrom)+row_change; i < (xto); i += row_change) // If there is no obstacle in the way
{
if (b[yfrom][i]->getValue() != 0)
{
return false;
}
}
move = true; // The piece has moved so castling is not possible
return true;
}
}
return false;
}
void Print(int x, int y)
{
PieceImage.setPosition(ImageSize*x, ImageSize*y);
cout << (color ? 'W' : 'B') << 'R';
}
};