-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rook.java
74 lines (65 loc) · 2.46 KB
/
Rook.java
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
/****************************************************************************************************************************************************
* @Author: Corey M. Moura
* @Date: March 1, 2018
* @Professor: Dr. Trafftz
* @Project: Project 3 of CS163: Chess Game, player vs computer
* @Notes:
*
/****************************************************************************************************************************************************/
public class Rook extends ChessPiece
{
private Player owner;
public Rook(Player player)
{
super(player);
}
public String type(){
return "Rook";
}
public boolean isValidMove(Move move, IChessPiece[][] board) {
int changeY = Math.abs(move.fromRow - move.toRow);
int changeX = Math.abs(move.fromColumn - move.toColumn);
boolean valid = true;
/** Checks that the move is on the board **/
if (!super.isValidMove(move,board)){
return false;
}
/** Checks that your either moving in the same row or same column **/
else if((move.fromRow != move.toRow) && (move.fromColumn != move.toColumn)){
valid = false;
}
/** Checks the spaces below the rook **/
else if(move.toRow > move.fromRow){
for(int i = 1; i < changeY ; i++){
if(board[move.fromRow + i][move.fromColumn] != null){
valid = false;
}
}
}
/** Checks the spaces above the rook **/
else if(move.toRow < move.fromRow){
for(int i = 1; i < changeY ; i++){
if(board[move.fromRow - i][move.fromColumn] != null){
valid = false;
}
}
}
/** Checks the spaces right of rook **/
else if(move.toColumn > move.fromColumn){
for(int i = 1; i < changeX ; i++){
if(board[move.fromRow][move.fromColumn + i] != null){
valid = false;
}
}
}
/** Checks the spaces left of rook **/
else if(move.toColumn < move.fromColumn){
for(int i = 1; i < changeX ; i++){
if(board[move.fromRow][move.fromColumn - i] != null){
valid = false;
}
}
}
return valid;
}
}