-
Notifications
You must be signed in to change notification settings - Fork 2
/
drawBoard.m
110 lines (92 loc) · 2.64 KB
/
drawBoard.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
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
%% Tic Tac Toe board and supporting functions for the game
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Common functions for the game board
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% draws the blank Tic Tac Toe board %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = drawBoard(currentPlayer, state, row, col)
global gBoardInit ;
if gBoardInit == 1
if (isempty(state) )
updateBoard(currentPlayer, row,col);
else
redrawBoard(state);
end
else
initialiseBoard(currentPlayer, state);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initialise the board
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function initialiseBoard(currentPlayer, state)
global gBoardInit;
global x;
gBoardInit = 1;
close all;
figure('Name', 'Tic tac Toe');
plot(1,1);
if (currentPlayer == x )
xlabel('Current Player: X');
else
xlabel('Current Player: O');
end
axis([0, 3, 0, 3], "equal");
set(gca,'xTick',0:3,'xTickLabel','');
set(gca,'yTick',0:3,'yTickLabel','');
grid on;
% gridlinestyle doesn't work with gnuplot graphics_toolkit
% need to use FLTK graphics toolkit
set(get(gcf,'children'),'gridlinestyle','-')
set(get(gcf,'children'), 'linewidth', 5);
hold on;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Update the board state at single position
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function updateBoard(currentPlayer, row,col)
global x;
if (currentPlayer == x )
drawX(row,col);
xlabel('Current Player: O');
else
drawO(row,col);
xlabel('Current Player: X');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% display the current board state %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function redrawBoard(state)
global x;
global o;
for i = 1:size(state,1)
for j = 1:size(state, 2)
if (state(i,j) == o)
drawO(i,j);
elseif (state(i,j)== x)
drawX(i,j)
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% draw a X at given position %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function drawX(row, col)
center = [col-0.5,row-0.5];
plot([center(1)-0.3,center(1)+0.3],[center(2)-0.3,center(2)+0.3], 'LineWidth',4);
plot([center(1)+0.3,center(1)-0.3],[center(2)-0.3,center(2)+0.3], 'LineWidth',4);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% draw O at given position %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function drawO(row, col)
center = [col-0.5,row-0.5];
r = 0.3;
t = linspace(0,2*pi,100)';
circsx = r.*cos(t) + center(1);
circsy = r.*sin(t) + center(2);
plot(circsx,circsy, "m",'LineWidth',4);
end