Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Tic-Tac-Toe MATH 210-01, Fall 2021 Ryan Pellico, Department of Mathematics, Trinity College The function tictactoe.m allows you to play a game of Tic-Tac-Toe using MATLAB’s input() function to ask the...

1 answer below »
Tic-Tac-Toe
MATH 210-01, Fall 2021
Ryan Pellico, Department of Mathematics, Trinity College
The function tictactoe.m allows you to play a game of Tic-Tac-Toe using MATLAB’s input()
function to ask the user for moves, and basic plotting tools to draw X’s and O’s in the co
esponding
space, producing a figure like the on in the left below.
1. (30 points) Make the game “look nice", similar to the figure above on the right. Choose colors fo
each part of the game, make sure the aspect ratio is set so the board looks like a square, remove
axis labels, etc.
2. (10 points) Instead of using the mod() function on line 32, write an if statement that switches
the variable whose_move back and forth between player 1 and 2 as discussed in class.
Do 3 of the following 4 for (20 points) each.
3. Write a function did_someone_win() that takes in the variable board and outputs a boolean
variable oneORzero depending on if someone has won.
4. Incorporate the function did_someone_win() into the function tictactoe.m to stop the game
as soon as someone wins. Use the text.m function to display a message on the game board that
the appropriate player won.
5. Write a function valid_move() that takes in the variables board and input_move and outputs
a boolean variable oneORzero depending on whether the most recent move is allowed.
6. Incorporate the function valid_move() into the function tictactoe.m to keep prompting the
cu
ent player in the command line until a valid move is given. (Hint: use a while loop).

close all
% bottom left (BL) coordinates of the nine game cells
% the order of the cells co
esponds to their labels
CELLcoordsBL = [0 2; 0 1; 0 0; 1 2; 1 1; 1 0; 2 2; 2 1; 2 0];
% the draw_board function sets up the initial board and labels
% the cells based on their order in the matrix CELLcoordsBL
draw_board(CELLcoordsBL)
% the 3x3 matrix board will store the moves of players
oard = zeros(3);
% boolean (TRUE/FALSE) variable to keep track of when the game has ended
game_over = 0;
% variable to keep track of whose turn it is: PLAYER 1 or PLAYER 2
whose_move = 1;
while ~game_ove

% ask the user which cell they would like to play
input_move = input("Player " + whose_move + " enter your move (1-9): ");

% update the plot to show the new move
draw_move(CELLcoordsBL(input_move,:) + [0.5, 0.5], whose_move)

% log the move in the matrix board
board(input_move) = whose_move;

% switch to the other players move
whose_move = mod(whose_move, 2) + 1;
% could also use an if statement to switch back and forth between
% PLAYER 1 and PLAYER 2

end
function draw_move(coords,player)
% this function marks either an X (if player == 1) or O (if player == 2)
% centered on the location in the vector coords
x = coords(1);
y = coords(2);
ds = 0.3;
if player == 1 % if the move was from PLAYER 1, make an X
plot([x-ds,x+ds],[y-ds,y+ds],'LineWidth',5)
plot([x-ds,x+ds],[y+ds,y-ds],'LineWidth',5)
else % otherwise the move was from PLAYER 2, so make an O
theta = linspace(0,2*pi,200);
plot(x + ds*cos(theta),y + ds*sin(theta),'LineWidth',5)
end
end
function draw_board(CELLcoordsBL)
for i = 1:2 % draw the "hashtag" pattern for the game board
plot([i i],[0 3],'LineWidth',10)
hold on
plot([0 3],[i i],'LineWidth',10)
end
for i = 1:9 % label the nine cells based on the ordering given above
text(CELLcoordsBL(i,1)+0.15,CELLcoordsBL(i,2)+0.15,num2str(i),'FontSize',20)
end

end
Answered 3 days After Oct 25, 2021

Solution

Sathishkumar answered on Oct 29 2021
113 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here