forked from bdsword/MatlabGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckRules.m
63 lines (60 loc) · 1.76 KB
/
checkRules.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
function result = checkRules( x, y, color )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
global chessBoard;
direction = [1,0;0,1;-1,0;0,-1];
result = true;
%---Rule 1-----
%Test if the position is empty
if chessBoard(x,y)~=0
result=false;
return;
end
%--------------
%---Rule 2-----
global chessBoardSize;
walkedChess = zeros(chessBoardSize,chessBoardSize);
liberty=0;
chessBoard(x,y)=color; %assume that chess has been put on the chess board.
countLiberty(x,y);
if liberty==0%the chess that user wanna set has no liberty
takenChess = takeChess(x,y);
if isempty(takenChess)
result=false;
chessBoard(x,y)=0;
return;
end
end
chessBoard(x,y)=0;
function countLiberty( nowX, nowY)
if nowX>19 || nowY>19 || nowX<1 || nowY<1 || walkedChess(nowX,nowY)~=0
return;
end
walkedChess(nowX,nowY)=1;
if chessBoard(nowX,nowY)==0
liberty = liberty+1;
return;
end
if chessBoard(nowX,nowY)==color
for j=1:4
countLiberty(nowX+direction(j,1),nowY+direction(j,2));
end
end
end
%--------------
%---Rule 3----
%Check if the a ko exist
global koLogger;
chessBoard(x,y)=color;
takenChess = takeChess(x,y);
chessBoard(x,y)=0;
if ~isempty(koLogger) && koLogger(1)==x && koLogger(2)==y && koLogger(3)==color
result=false;
return;
elseif size(takenChess,1)==1%The takenChess is calculated from last rule
koLogger = [takenChess,chessBoard(takenChess(1,1),takenChess(1,2))];
else
koLogger = [];
end
%-------------
end