-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessPiece.java
More file actions
104 lines (79 loc) · 2.92 KB
/
ChessPiece.java
File metadata and controls
104 lines (79 loc) · 2.92 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
/**************************************************************
* Functionality for parent ChessPiece
*
* @author Richard
* @author Steven
* @version 3/13/2022
**************************************************************/
public abstract class ChessPiece {
private Player owner;
/*************************************************************
* Constructor sets the piece's owner to the current player
*************************************************************/
protected ChessPiece(Player player) {
this.owner = player;
}
public abstract String type();
/*************************************************************
* Returns the player that owns this piece
*************************************************************/
public Player player() {
return owner;
}
/*************************************************************
* Verifies that the ChessPiece has moved from the previous
* position
*************************************************************/
private boolean hasPieceMoved(Move move) {
if (move.toRow != move.fromRow || move.toColumn != move.fromColumn) {
return true;
}
return false;
}
/*************************************************************
* Verifies the piece is moving to a position on the board
*************************************************************/
private boolean isOnBoard(Move move, ChessPiece[][] board) {
if (move.toRow >= 0 && move.toRow < board.length &&
move.toColumn >= 0 && move.toColumn < board.length) {
return true;
}
return false;
}
/*************************************************************
* Verifies that the piece being moved is at the expected
* position
*************************************************************/
private boolean isInitialPositionAccurate(Move move, ChessPiece[][] board) {
if (this == board[move.fromRow][move.fromColumn]) {
return true;
}
return false;
}
/*************************************************************
* Verifies that the piece is not being moved to a position
* that contains a piece of the same color
*************************************************************/
private boolean positionNotSameColor(Move move, ChessPiece[][] board) {
if (board[move.toRow][move.toColumn] == null) {
return true;
}
if (this.player() != board[move.toRow][move.toColumn].player()) {
return true;
}
return false;
}
/*************************************************************
* Verifies that the piece can move to the selected
* position
*************************************************************/
public boolean isValidMove(Move move, ChessPiece[][] board) {
if (!isOnBoard(move, board))
return false;
if (!(hasPieceMoved(move) &&
isInitialPositionAccurate(move, board) &&
positionNotSameColor(move, board)))
return false;
return true;
}
}