Can you create a 2-player Tic Tac Toe game (also known as noughts and crosses) on MATLAB, using nested loops (for, if, else, while loops) arrays, conditional execution and functions, as well as displaying the game board of X and O on a plot.
function [] = tictac
n = 1;
cla;
hold on;
c = zeros(2,5);
d = zeros(2,5);
for n = 1:6
m = (-1)^n;
if m == 1;
x1 = input('player 1 move:');
y1 = ('');
move(x1,y1)
c(1, (n+1)/2) = x1;
c(2, (n+1)/2) = y1;
else
x2 = input('player 2 move:');
y2 = ('');
move(x2,y2)
d(1, (n+1)/2) = x2;
d(2, (n+1)/2) = y2;
end
n = n+1;
end
f=sum(c,2);
g=sum(d,2);
if c(1,1)==c(1,2)==c(1,3) || c(2,1)==c(2,2)==c(2,3);
disp ('PLAYER 1 WINS')
elseif d(1,1)==d(1,2)==d(1,3) || d(2,1)==d(2,2)==d(2,3);
disp ('PLAYER 2 WINS')
elseif f(1,1) == 6 && f(2,1) == 6;
disp ('PLAYER 1 WINS')
elseif g(1,1) == 6 && f(2,1) == 6;
disp ('PLAYER 2 WINS')
else
x3 = input('player 1 move:');
y3 = input('');
move(x3,y3);
c(1,4) = x1;
c(2,4) = y1;
end
Get Answers For Free
Most questions answered within 1 hours.