-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathischeck.m
72 lines (56 loc) · 2.15 KB
/
ischeck.m
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
% "Check: Lite Edition"
% Similar to ChessBoard's checkcheck(), just decoupled and
% does not do checkmate; this is not present in ChessBoard to
% ensure no arbitrary check querying. To avoid a nasty recursion issue
% with vmoves (see king castling @(p)...), this uses a manual check for
% king.
function result = ischeck(board, player)
result = false;
kq = board.kquery();
for r = 1:8
for c = 1:8
pos = [r,c];
% If the space at [r,c] isn't empty and we haven't
% confirmed check yet, keep looking for check.
if ~board.iserel(pos)
% Get all the moves of the piece at [r,c] that
% consumes a piece. This is a DECOUPLED ChessBoard.cmoves.
mbuffer = Buffer(30);
piece = board.get(pos);
% Check if not empty.
if ~iseabs(piece)
pplayer = piece.Player;
ptype = piece.Type;
if ptype ~= PieceType.King
allmoves = [ board.vmoves(pos, -1), board.evmoves(pos) ];
else
abuffer = Buffer(9);
for dir = Direction.dirs
abuffer.aa(board.iuntilmax(pos, dir, 1, pplayer));
end
abuffer.aa(board.evmoves(pos));
allmoves = abuffer.flush();
end
for i = 1:length(allmoves)
move = unwrap(allmoves(i), 1);
if board.isoppo(move, pplayer)
mbuffer.a(move);
end
end
end
cmoves = mbuffer.flush();
% Loop through all the cmoves.
for j = 1:length(cmoves)
cmove = cmoves{j};
%If any move in (cmoves) matches the king's position, it means
% that the king is in check. We check by ensuring
% both are permutations of each other.
if psame(cmove, kq{player})
result = true;
return;
end
end
end
end
end
end