-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
105 lines (90 loc) · 3.85 KB
/
Rook.java
File metadata and controls
105 lines (90 loc) · 3.85 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
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
package chessprj;
/**********************************************************************
* Houses information about the Rook chess piece
**********************************************************************/
public class Rook extends ChessPiece {
/**********************************************************************
* Constructor for rook piece
*
* @param player
* @param model
**********************************************************************/
public Rook(Player player, ChessModel model) {
super(player, model);
setStrategicValue(5);
}
/**********************************************************************
* @return piece type
**********************************************************************/
public String type() {
return "Rook";
}
/**********************************************************************
* set first move
*
* @param first
**********************************************************************/
public void setFirstMove(boolean first){
firstMove = first;
}//end setFirstMove
/**********************************************************************
* copies piece
**********************************************************************/
@Override
public IChessPiece copy(){
Rook copy = new Rook(player(), model);
copy.setFirstMove(firstMove);
return copy;
}
/**********************************************************************
* checks if a proposed move is valid
*
* @param move
* @param board
* @return true if move valid, else false
**********************************************************************/
public boolean isValidMove(Move move, IChessPiece[][] board) {
// makes sure the move is within the confines of the board
if(!isInBoard(move)) return false;
boolean valid = false;
int tempRow;
int tempCol;
//Horizontal moves
if(move.toRow == move.fromRow || move.toColumn == move.fromColumn){
//empty square or opponent
if(board[move.toRow][move.toColumn] == null || board[move.toRow][move.toColumn].player()!= player()){
valid = true;
//Square is a "available" can you get there?
//to the left
if(move.toColumn < move.fromColumn){
for(tempCol = move.fromColumn-1; tempCol > move.toColumn; tempCol--){
if(board[move.fromRow][tempCol] != null)
valid = false;
}//end for
}//end to the Left
//to the right
if(move.toColumn > move.fromColumn){
for(tempCol = move.fromColumn +1; tempCol < move.toColumn; tempCol++){
if(board[move.fromRow][tempCol] != null)
valid = false;
}//end for
}//end to the right
//up
if(move.toRow < move.fromRow){
for(tempRow = move.fromRow-1; tempRow > move.toRow; tempRow--){
if(board[tempRow][move.fromColumn] != null)
valid = false;
}//end for
}//Up
//Down
if(move.toRow > move.fromRow){
for(tempRow = move.fromRow+1; tempRow < move.toRow; tempRow++){
if(board[tempRow][move.fromColumn] != null)
valid = false;
}//end for
}//Down
}//end check empty or opponent
}//end Horizontal moves
return valid;
}//end isValidMove
}//end Class Rook