-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathischeckmate.m
22 lines (19 loc) · 882 Bytes
/
ischeckmate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
% Checks if the king is in checkmate or not. This is a decoupled
% form of the checkmate checker used in checkcheck, so avoid when
% possible.
function result = ischeckmate(board, player)
result = false;
% Not in checkmate if not in check!
if board.Checks(player) % Get all valid moves for the player
vmoves = board.vpmoves(player, 0);
% Filter for only moves that resolve check
for i = length(vmoves):-1:1
vmove = vmoves{i};
if ~board.isresmove(vmove{1}, vmove{2}, player)
vmoves(i) = [];
end
end
% If no moves that resolve check, then we are in checkmate.
result = isempty(unwrap(vmoves, 1));
end
end