-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckKing.java
215 lines (184 loc) · 6.81 KB
/
CheckKing.java
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
public class CheckKing
{
/**
* This looks through all of the moves a piece can move to, and removes any of the moves that puts the king
in danger
*/
public boolean[][] removeIllegalMoves(TileButton[][] _allTiles, boolean[][] _disabledTiles, int _playerTeamId,
int _pressedRow, int _pressedCol)
{
for(int row = 0; row < _disabledTiles.length; row++)
{
for(int col = 0; col < _disabledTiles[0].length; col++)
{
if(_disabledTiles[row][col] == false)
{
/*removes the piece info from the pressed button and adds it to the other tile to test if
the king will be vulnerable if the piece moved here. (only the info is moved, not the piece
picture)*/
String piece = _allTiles[_pressedRow][_pressedCol].getPiece();
String team = _allTiles[_pressedRow][_pressedCol].getTeam();
_allTiles[_pressedRow][_pressedCol].removePieceInfo();
String pieceAtTile = _allTiles[row][col].getPiece();
String teamAtTile = _allTiles[row][col].getTeam();
_allTiles[row][col].setPieceInfo(piece, team);
if(isIllegalMove(_allTiles, getTeam(_playerTeamId)) == true)
{
_allTiles[_pressedRow][_pressedCol].removePieceInfo();
_disabledTiles[row][col] = true;
}
if(!teamAtTile.equals("none"))
{
_allTiles[row][col].setPieceInfo(pieceAtTile, teamAtTile);
}
else
_allTiles[row][col].removePieceInfo();
_allTiles[_pressedRow][_pressedCol].setPieceInfo(piece, team);
}
}
}
return _disabledTiles;
}
/**
* This checks a single move white can make. If this move threatens the player's king, then it is an illegal move;
this will return false if it is an illegal move.
*/
public boolean isIllegalMove(TileButton[][] _allTiles, String _team)
{
return kingIsVulnerable(_allTiles, _team);//if the king is vulerable, then this is an illegal move
}
/**
* Checks every single one of the opponent's chess pieces and checks whether the king is under attack by this piece or not
*/
public boolean kingIsVulnerable(TileButton[][] _allTiles, String _team)
{
String opponentTeam = getOpponentTeam(_team);
/*checks every single tile on the board. If the tile contains a opposing player's piece, then this will
check if this piece can attack the current player's king. If it can, this method will return false*/
for(int row = 0; row < _allTiles.length; row++)
{
for(int col = 0; col < _allTiles[0].length; col++)
{
//checks if the opposing player owns a piece at this tile
if(_allTiles[row][col].getTeam().equals(opponentTeam))
{
if(pieceIsThreat(_allTiles, _team, row, col) == true)
return true;
}
}
}
return false;
}
public boolean pieceIsThreat(TileButton[][] _allTiles, String _team, int _row, int _col)
{
Movements movements = new Movements();
int kingRow = getKingRow(_allTiles, _team);
int kingCol = getKingCol(_allTiles, _team);
//all of the tiles the opponent piece can attack from their position
boolean[][] attackedTiles = movements.buttonPressed(_allTiles, _row, _col, 1);
boolean threat = false;
//checks if one of the tiles the opponent piece can attack is the current player's king
for(int row = 0; row < _allTiles.length; row++)
{
for(int col = 0; col < _allTiles[0].length; col++)
{
//if the opponent is attacking a tile that contains the current player's king, then that piece is a threat
if(attackedTiles[row][col] == false && row == kingRow && col == kingCol)
{
threat = true;
return threat;
}
}
}
return threat;
}
public int getKingRow(TileButton[][] _allTiles, String _team)
{
for(int row = 0; row < _allTiles.length; row++)
{
for(int col = 0; col < _allTiles[0].length; col++)
{
if(_allTiles[row][col].getTeam().equals(_team))
{
if(_allTiles[row][col].getPiece().equals("king"))
return row;
}
}
}
return -1;//this will never be used
}
public int getKingCol(TileButton[][] _allTiles, String _team)
{
for(int row = 0; row < _allTiles.length; row++)
{
for(int col = 0; col < _allTiles[0].length; col++)
{
if(_allTiles[row][col].getTeam().equals(_team))
{
if(_allTiles[row][col].getPiece().equals("king"))
return col;
}
}
}
return -1;//will never be used
}
public String getTeam(int _teamId)
{
if(_teamId == 0)
return "white";
else
return "black";
}
public String getOpponentTeam(String _team)
{
if(_team.equals("white"))
return "black";
else
return "white";
}
/**
* Checks if there is a checkmate or not
* @return returns true if there is a checkmate
*/
public boolean isCheckmate(TileButton[][] _allTiles, int _playerTeamId)
{
String team = getTeam(_playerTeamId);
Movements movements = new Movements();
boolean isCheckmate = false;
boolean[][] disabledTiles;
int possibleMoves = 0;
for(int row = 0; row < _allTiles.length; row++)
{
for(int col = 0; col < _allTiles[0].length; col++)
{
if(_allTiles[row][col].getTeam().equals(team))
{
disabledTiles = movements.buttonPressed(_allTiles, row, col, 0);
disabledTiles = removeIllegalMoves(_allTiles, disabledTiles, _playerTeamId, row, col);
disabledTiles[row][col] = true;
possibleMoves += numOfPossibleMoves(disabledTiles);
if(possibleMoves > 0)
break;
}
}
if(possibleMoves > 0)
break;
}
if(possibleMoves == 0)
isCheckmate = true;
return isCheckmate;
}
public int numOfPossibleMoves(boolean[][] _disabledTiles)
{
int num = 0;
for(int row = 0; row < _disabledTiles.length; row++)
{
for(int col = 0; col < _disabledTiles[0].length; col++)
{
if(_disabledTiles[row][col] == false)
num++;
}
}
return num;
}
}