-
Notifications
You must be signed in to change notification settings - Fork 2
/
Reversi.m
51 lines (47 loc) · 1.12 KB
/
Reversi.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
%% Reversi
%
% This is a script to play Reversi (Heibai Qi). For the rules on this game,
% please check https://en.wikipedia.org/wiki/Reversi
%
% Click outside of the board. Double pass ends the game.
%
% State values 0: empty; 1: black; -1: white
%
% Long Chen 2019. May. 12.
%% Initialize the game and draw the center stones
plotboard;
u = zeros(8,8);
u(4,4) = 1;
u(5,5) = 1;
u(4,5) = -1;
u(5,4) = -1;
plotgame(u);
%% Play the game
currentColor = 1; % start from black
h = 1/8;
pass = 0;
while pass < 2 % exit with two consective pass
[x,y] = myginput(1,'circle');
j = round(x/h-0.5)+1;
i = round(y/h-0.5)+1;
if (i<1) || (i>8)|| (j<1) || (j>8) % click out of the board is pass
pass = pass + 1;
currentColor = - currentColor;
continue;
end
p = sub2ind([8,8],i,j);
if u(i,j) == 0 % no stone
% put the stone and reverse stones captured
[u,currentColor] = putstone(u,p,currentColor);
pass = 0;
end
end
%% Count
switch sign(sum(u(:)))
case 1
msgbox('Black Wins!')
case -1
msgbox('White Wins')
case 0
msgbox('Tie')
end