-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
86 lines (71 loc) · 3.12 KB
/
Knight.java
File metadata and controls
86 lines (71 loc) · 3.12 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
package chessprj;
/**********************************************************************
* Houses information about the Knight chess piece
**********************************************************************/
public class Knight extends ChessPiece {
/**********************************************************************
* Constructor for knight piece
*
* @param player
* @param model
**********************************************************************/
public Knight(Player player, ChessModel model) {
super(player,model);
setStrategicValue(4);
}
/**********************************************************************
* @return piece type
**********************************************************************/
public String type() {
return "Knight";
}
/**********************************************************************
* set first move
*
* @param first
**********************************************************************/
public void setFirstMove(boolean first){
firstMove = first;
}//end setFirstMove
/**********************************************************************
* copies piece
**********************************************************************/
@Override
public IChessPiece copy(){
Knight copy = new Knight(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){
boolean valid = false;
// makes sure the move is within the confines of the board
if(!isInBoard(move)) return false;
//space is empty or occupied by opponent
if(board[move.toRow][move.toColumn] == null || board[move.toRow][move.toColumn].player()!= player()){
// up two spaces and left or right one
if(move.toRow == move.fromRow-2)
if(move.toColumn == move.fromColumn-1 || move.toColumn == move.fromColumn+1)
valid = true;
//up one space and left or right two
if(move.toRow == move.fromRow-1)
if(move.toColumn == move.fromColumn-2 || move.toColumn == move.fromColumn+2)
valid = true;
//down one space and left or right two
if(move.toRow == move.fromRow+1)
if(move.toColumn == move.fromColumn-2 || move.toColumn == move.fromColumn+2)
valid = true;
//down two spaces and left or right one
if(move.toRow == move.fromRow+2)
if(move.toColumn == move.fromColumn-1 || move.toColumn == move.fromColumn+1)
valid = true;
}//end check empty or opponent
return valid;
}
}