I am trying to create a code in matlab for computer and human interaction to play tic tac toe and there is an error comi
Posted: Mon May 09, 2022 7:09 am
I am trying to create a code in matlab for computer and human
interaction to play tic tac toe and there is an error coming up
saying Index in position 2 exceeds array bounds. plz help. I'll
include my code below
board = zeros(3,3);
% Draw Board
figure
plot([.5 3.5],[-1.5 -1.5], 'k','linewidth',2);
hold on
plot([.5 3.5],[-2.5 -2.5], 'k','linewidth',2)
plot([1.5 1.5],[-.5 -3.5], 'k','linewidth',2)
plot([2.5 2.5],[-.5 -3.5], 'k','linewidth',2)
hold off
axis off
text(c,-r,'X','horizontalalignment','center','fontsize',20);
move=1; % First move is by computer
for i=1:4 %Loop to choose the best starting position
if board (i,i)==1 || board(i,5-i)==1 %If it is found that there
is an 'X' in the any
disp('The computer will play with X') %of the best places
computer will play with 'X'
break;
elseif board (i,i)==2 || board(i,5-i)==2 %If it is found that
there is an 'O' in the any
disp('The computer will play with O') %of the best places
computer will play with 'O'
break;
end
end
while move<3 %Loop which decides who will make a move
if move==1 %Computer's turn
run computer_move
end
if move==2 %User's turn
run user
end
end
%Ask name and play first move that makes user's move
name = input("Hey! What is your name? Lets play some tic tac
toe! ", "S");
disp('Choose row and column to put X') %Displays a message
x=input('row = ');
y=input('column = '); %Coordinates chosen by user
if board(x,y)==0 %Choosen cell is free
board(x,y)=1; %Putting 'X' inside the chosen cell
move=1; %Move is made, the next move is by computer
else
disp('You can not choose this cell sorry')
disp(' '); %Message shows when a user has choosen already
occupied cell
end
empty=freecells(board);
if empty==0 %There are no free cells
move=3; %Game over. There are no any legal moves
disp('Game over. Draw!');
end
% Script that makes computer's move
ran=random_move(board); %Coordinates of a cell for random
move
two=two_in_a_line(board); %Coordinates of a cell for putting two
in a line
three=three_in_a_line(board); %Coordinates of a cell for putting
three in a line
three_b=three_block(board); %Coordinates of a cell to block
upcoming three 'X' in a line
if three(1)==1 %There is a winning position for the computer
board(three(2),three(3))=2; %Putting 'O' inside the board
move=3; %Game over. There are no any legal moves
disp('Game over. Computer won!');
%Loop is finished
elseif three_b(1)==1 %Preventing user from winning the game
board(three_b(2),three_b(3))=2; %Putting 'O' inside the
board
move=2; %Next move is by user
%Loop is finished
% elseif three(1)==1 %Putting the third 'O' in a line
board(three(2),three(3))=2;
move=2;
elseif three_b(1)==1 %Preventing three 'X' in a line
board(three_b(2),three_b(3))=2;
move=2;
elseif two(1)==1 %Putting the second 'O' in a line
board(two(2),two(3))=2;
move=2;
else %Making a random move
board(ran(1),ran(2))=2;
move=2;
end
empty=freecells(board); %Check for empty cells
if empty==0 %There are no free cells
move=3; %Game over. There are no any legal moves
disp('Game over. Draw!');
end
interaction to play tic tac toe and there is an error coming up
saying Index in position 2 exceeds array bounds. plz help. I'll
include my code below
board = zeros(3,3);
% Draw Board
figure
plot([.5 3.5],[-1.5 -1.5], 'k','linewidth',2);
hold on
plot([.5 3.5],[-2.5 -2.5], 'k','linewidth',2)
plot([1.5 1.5],[-.5 -3.5], 'k','linewidth',2)
plot([2.5 2.5],[-.5 -3.5], 'k','linewidth',2)
hold off
axis off
text(c,-r,'X','horizontalalignment','center','fontsize',20);
move=1; % First move is by computer
for i=1:4 %Loop to choose the best starting position
if board (i,i)==1 || board(i,5-i)==1 %If it is found that there
is an 'X' in the any
disp('The computer will play with X') %of the best places
computer will play with 'X'
break;
elseif board (i,i)==2 || board(i,5-i)==2 %If it is found that
there is an 'O' in the any
disp('The computer will play with O') %of the best places
computer will play with 'O'
break;
end
end
while move<3 %Loop which decides who will make a move
if move==1 %Computer's turn
run computer_move
end
if move==2 %User's turn
run user
end
end
%Ask name and play first move that makes user's move
name = input("Hey! What is your name? Lets play some tic tac
toe! ", "S");
disp('Choose row and column to put X') %Displays a message
x=input('row = ');
y=input('column = '); %Coordinates chosen by user
if board(x,y)==0 %Choosen cell is free
board(x,y)=1; %Putting 'X' inside the chosen cell
move=1; %Move is made, the next move is by computer
else
disp('You can not choose this cell sorry')
disp(' '); %Message shows when a user has choosen already
occupied cell
end
empty=freecells(board);
if empty==0 %There are no free cells
move=3; %Game over. There are no any legal moves
disp('Game over. Draw!');
end
% Script that makes computer's move
ran=random_move(board); %Coordinates of a cell for random
move
two=two_in_a_line(board); %Coordinates of a cell for putting two
in a line
three=three_in_a_line(board); %Coordinates of a cell for putting
three in a line
three_b=three_block(board); %Coordinates of a cell to block
upcoming three 'X' in a line
if three(1)==1 %There is a winning position for the computer
board(three(2),three(3))=2; %Putting 'O' inside the board
move=3; %Game over. There are no any legal moves
disp('Game over. Computer won!');
%Loop is finished
elseif three_b(1)==1 %Preventing user from winning the game
board(three_b(2),three_b(3))=2; %Putting 'O' inside the
board
move=2; %Next move is by user
%Loop is finished
% elseif three(1)==1 %Putting the third 'O' in a line
board(three(2),three(3))=2;
move=2;
elseif three_b(1)==1 %Preventing three 'X' in a line
board(three_b(2),three_b(3))=2;
move=2;
elseif two(1)==1 %Putting the second 'O' in a line
board(two(2),two(3))=2;
move=2;
else %Making a random move
board(ran(1),ran(2))=2;
move=2;
end
empty=freecells(board); %Check for empty cells
if empty==0 %There are no free cells
move=3; %Game over. There are no any legal moves
disp('Game over. Draw!');
end