-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
136 lines (103 loc) · 4.73 KB
/
Pawn.java
File metadata and controls
136 lines (103 loc) · 4.73 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package chessprj;
/**********************************************************************
* Houses information about the Pawn chess piece
**********************************************************************/
public class Pawn extends ChessPiece {
/**********************************************************************
* Constructor for pawn piece
*
* @param player
* @param model
**********************************************************************/
public Pawn(Player player, ChessModel model) {
super(player, model);
setStrategicValue(1);
}//end Constructor
/**********************************************************************
* @return piece type
**********************************************************************/
public String type() {
return "Pawn";
}//end type
/**********************************************************************
* set first move
*
* @param first
**********************************************************************/
public void setFirstMove(boolean first){
firstMove = first;
}//end setFirstMove
/**********************************************************************
* copies piece
**********************************************************************/
@Override
public IChessPiece copy(){
Pawn copy = new Pawn(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;
//white ifs
if(player() == Player.WHITE){
//Check if pawn has not yet moved
if(firstMove){
//move two spaces on initial move
if(move.toRow == move.fromRow - 2 && move.toColumn == move.fromColumn){
valid = true;
}//end if
if(move.toRow == move.fromRow -1 && move.toColumn == move.fromColumn){
valid=true;
}//end if
}//end first move if
//move in straight lines
if(move.toRow == move.fromRow -1 && move.toColumn == move.fromColumn)
valid=true;
//don't overwrite other pieces
if(board[move.toRow][move.toColumn] != null)
valid = false;
//move diagonally to take enemy pieces
if((move.toRow == move.fromRow-1 && move.toColumn == move.fromColumn -1) || (move.toColumn == move.fromColumn + 1 && move.toRow == move.fromRow -1))
if(board[move.toRow][move.toColumn] != null)//is the space empty
if(board[move.toRow][move.toColumn].player() != player())//is it the same team?
valid = true;
}//end whitePlayer
//black ifs
if(player() == Player.BLACK){
//check if pawn has moved yet
if(firstMove){
//move two spaces on initial move
if(move.toRow == move.fromRow + 2 && move.toColumn == move.fromColumn){
valid = true;
//first turn over
}// end if
// or only move one. Your call really
if(move.toRow == move.fromRow + 1 && move.toColumn == move.fromColumn){
valid=true;
//no longer first move
} //end if
}//end first move if
//move in straight lines
if(move.toRow == move.fromRow + 1 && move.toColumn == move.fromColumn)
valid=true;
//don't overwrite other pieces.
if(board[move.toRow][move.toColumn] != null)
valid = false;
//move diagonally to take enemy pieces
if((move.toRow == move.fromRow +1 && move.toColumn == move.fromColumn -1) || (move.toColumn == move.fromColumn + 1 && move.toRow == move.fromRow +1))
if(board[move.toRow][move.toColumn] != null) //is the space empty?
if(board[move.toRow][move.toColumn].player() != player())//is it the same team?
valid = true;
}//end blackPlayer
return valid;
}//end isValidMove
}//end Class Pawn