-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
112 lines (89 loc) · 3.93 KB
/
King.java
File metadata and controls
112 lines (89 loc) · 3.93 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
106
107
108
109
110
111
112
package chessprj;
/**********************************************************************
* Houses information about the King chess piece
**********************************************************************/
public class King extends ChessPiece {
/**********************************************************************
* Constructor for king piece
*
* @param player
* @param model
**********************************************************************/
public King(Player player, ChessModel model) {
super(player,model);
setStrategicValue(500);
}
/**********************************************************************
* @return piece type
**********************************************************************/
public String type() {
return "King";
}
/**********************************************************************
* set first move
*
* @param first
**********************************************************************/
public void setFirstMove(boolean first){
firstMove = first;
}//end setFirstMove
/**********************************************************************
* copies piece
**********************************************************************/
@Override
public IChessPiece copy(){
King copy = new King(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;
//space is empty or occupied by opponent
if(board[move.toRow][move.toColumn] == null || board[move.toRow][move.toColumn].player()!= player()){
//up and left one square
if(move.toRow == move.fromRow -1 && move.toColumn == move.fromColumn -1)
valid = true;
//straight up one square
if(move.toRow == move.fromRow -1 && move.toColumn == move.fromColumn)
valid = true;
//up and right one square
if(move.toRow == move.fromRow -1 && move.toColumn == move.fromColumn +1)
valid = true;
//left one square
if(move.toRow == move.fromRow && move.toColumn == move.fromColumn -1)
valid = true;
//right one square
if(move.toRow == move.fromRow && move.toColumn == move.fromColumn +1)
valid = true;
//down and left one square
if(move.toRow == move.fromRow +1 && move.toColumn == move.fromColumn -1)
valid = true;
//straight down one square
if(move.toRow == move.fromRow +1 && move.toColumn == move.fromColumn)
valid = true;
//down and right one square
if(move.toRow == move.fromRow +1 && move.toColumn == move.fromColumn +1)
valid = true;
if(player() == Player.BLACK){
for(Move check: model.getWhiteAttackMoves())
if(move.toRow == check.toRow && move.toColumn == check.toColumn)
valid = false;
}//end if
if(player() == Player.WHITE){
for(Move check: model.getBlackAttackMoves())
if(move.toRow == check.toRow && move.toColumn == check.toColumn)
valid = false;
}//end if
}
return valid;
}//end isValidMove
}//end Class King