-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStatus.java
More file actions
118 lines (96 loc) · 3.15 KB
/
GameStatus.java
File metadata and controls
118 lines (96 loc) · 3.15 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
import java.util.ArrayList;
public class GameStatus {
private static boolean blackCheck;
private static boolean blackCheckMate;
private static boolean blackStaleMate;
private static boolean whiteCheck;
private static boolean whiteCheckMate;
private static boolean whiteStaleMate;
private static void initialize(){
blackCheck = false;
blackCheckMate = false;
blackStaleMate = false;
whiteCheck = false;
whiteCheckMate = false;
whiteStaleMate = false;
}
public static void update(int color){
initialize();
int currentColor, opponentColor;
PieceSet currentSet = (color != PieceColor.BLACK) ? Board.getBlackSet() : Board.getWhiteSet();
PieceSet opponentSet = (color != PieceColor.BLACK) ? Board.getWhiteSet() : Board.getBlackSet();
currentColor = currentSet.getColor();
opponentColor = opponentSet.getColor();
Piece auxKing = currentSet.getAvailablePieces().get(0);
if (Piece.canBeCaptured(auxKing, auxKing.getPosition(), opponentColor) != null){
if (currentColor == PieceColor.BLACK){
blackCheck = true;
}
if (currentColor == PieceColor.WHITE){
whiteCheck = true;
}
}
if (currentColor == PieceColor.BLACK && blackCheck && auxKing.getCaptureFreeSquares().isEmpty()){
blackCheckMate = true;
} else {
if (currentColor == PieceColor.WHITE && whiteCheck && auxKing.getCaptureFreeSquares().isEmpty()){
whiteCheckMate = true;
}
}
if (currentColor == PieceColor.BLACK && !blackCheck && auxKing.getCaptureFreeSquares().isEmpty()){
int possibleMoves=0;
ArrayList<Piece> available = currentSet.getAvailablePieces();
for (Piece p : available){
possibleMoves+=p.getValidSquares().size();
}
if (possibleMoves==0)
blackStaleMate=true;
} else {
if (currentColor == PieceColor.WHITE && !whiteCheck && auxKing.getCaptureFreeSquares().isEmpty()){
int possibleMoves=0;
ArrayList<Piece> available = currentSet.getAvailablePieces();
for (Piece p : available){
possibleMoves+=p.getValidSquares().size();
}
if (possibleMoves==0)
whiteStaleMate=true;
}
}
/*
System.out.println(
"\n blackCheck: " + blackCheck +
"\n blackCheckMate: " + blackCheckMate +
"\n blackStaleMate: " + blackStaleMate +
"\n whiteCheck: " + whiteCheck +
"\n whiteCheckMate: " + whiteCheckMate +
"\n whiteStaleMate: " + whiteStaleMate + '\n');
*/
}
public static boolean isBlackCheck() {
return GameStatus.blackCheck;
}
public static boolean isBlackCheckMate() {
return GameStatus.blackCheckMate;
}
public static boolean isBlackStaleMate() {
return GameStatus.blackStaleMate;
}
public static boolean isWhiteCheck() {
return GameStatus.whiteCheck;
}
public static boolean isWhiteCheckMate() {
return GameStatus.whiteCheckMate;
}
public static boolean isWhiteStaleMate() {
return GameStatus.whiteStaleMate;
}
public static boolean isAnyChecked() {
return GameStatus.blackCheck || GameStatus.whiteCheck;
}
public static boolean isAnyCheckMated() {
return GameStatus.blackCheckMate || GameStatus.whiteCheckMate;
}
public static boolean isAnyStaleMated() {
return GameStatus.blackStaleMate || GameStatus.whiteStaleMate;
}
}