Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Topics: User Defined Functions and MATLAB Conditionals A Childrens game studio n

ID: 3857015 • Letter: T

Question

Topics: User Defined Functions and MATLAB Conditionals

A Childrens game studio needs you to write a MATLAB Program to allow two players to play the game Tic-Tac-Toe on a computer:

This is a two player (X's and O's) board game.

The game board is a 3 X 3 grid, where each location can hold either an 'X' an 'O' or a space (empty)

Note: we will linearize the grid and refer to the its locations as 1 - 9, for conveyance.

The players take turns placing their mark into one of the (empty) locations in the grid.

The game is won when a player has their mark in all three locations of any row, column, or diagonal.

The game can also reach a stalemate: neither player has won (by above criteria) however all there are no more empty locations in the grid.

Outline:

Create a MATLAB Script .m file

Establish variable(s) to represent the nine board locations

Create the game loop

i.Display the current state of the board – using separate displayBoard function

ii.Get a “valid” move location – note this can only be an empty location [1, 9]

iii.Modify the board variable(s)

iv.Check for a win and report it if so – game must end

v.Check for a stalemate – game must end

Create a MATLAB Function .m file

Establish its name (displayBoard), input(s) , and output (there really is not any)

Write the necessary MATLAB commands to display the Tic-Tac-Toe board, using the given input(s)

Make sure to test your script and function when they are done¸

Notes(s):

Keep track of the total number of moves, for an easy stalemate detection.

Your location choice user-validation loop must also not accept any location that is not currently empty.

At times things will feel very tedious - doing the "same" thing nine times ...

Sample Run(s):

Welcome to Tic-Tac-Toe : X goes first

-------------------------------------

    |     |   

---------------

    |     |   

---------------

    |     |   

Please enter location to move [1 - 9] : 10

Please enter location to move [1 - 9] : -1

Please enter location to move [1 - 9] : 1

X |     |   

---------------

    |     |   

---------------

    |     |   

Please enter location to move [1 - 9] : 1

Please enter location to move [1 - 9] : 2

X | O |   

---------------

    |     |   

---------------

    |     |   

Please enter location to move [1 - 9] : 3

X | O | X

---------------

    |     |   

---------------

    |     |   

Please enter location to move [1 - 9] : 4

X | O | X

---------------

O |     |   

---------------

    |     |   

Please enter location to move [1 - 9] : 5

X | O | X

---------------

O | X |   

---------------

    |     |   

Please enter location to move [1 - 9] : 6

X | O | X

---------------

O | X | O

---------------

    |     |   

Please enter location to move [1 - 9] : 7

X | O | X

---------------

O | X | O

---------------

X |     |   

Congrats - X Wins!

Explanation / Answer

the tic tac toe program in matlab is as below

function [] = tictactoe()
close all
figure('Name','Tic Tac Toe Game');
plot(-1. -1)
axis([0 3 0 3])
set(gca,'x-Tick',0:3)
set(gca,'y-Tick',0:3)
set(gca,'x-Label','')
set(gca,'y-Label','')
xlabel('Player X')
grid on
shg
  
is_x = 1; %current player
state = [[-1 -1 -1]
[-1 -1 -1]
[-1 -1 -1]]; % the state's -1 none, 0 = O, 1 = X
winner = -1; % check a winner or there is a tie
  
  
while winner == -1
next = play(is_x, state);
if next == -1 %ask them to try again
title('Invalid move, please try again');
else
state = next;
title('');
is_x = mod(is_x + 1,2);
if is_x == 1
xlabel('Player: X');
else
xlabel('Player: O');
end
winner = won(state);
end
end
  
if winner == 0 % O win
warndlg('O wins');
title('O wins');
xlabel('');
elseif winner == 1 % X win
warndlg('X wins');
title('X wins');
xlabel('');
else % tie case
warndlg('Tie');
title('Tie');
xlabel('');
end
end


function state = play(is_x, state)
[x y] = ginput(1);
[col row] = position(x, y);
row = 2 - row;
if state(col+1, row+1) ~= -1
state = -1;
else
state(col+1, row+1) = is_x;
if is_x
drawX(col, 2 - row);
else
drawO(col, 2 - row);
end
end
end

% won is for winners
function won = won(state)
% Hori check
if (state(1,1) == state(1,2) && state(1,1) == state(1,3) && state(1,1) ~= -1)
won = state(1,1);
elseif (state(2,1) == state(2,2) && state(2,1) == state(2,3) && state(2,1) ~= -1)
won = state(2,1);
elseif (state(3,1) == state(3,2) && state(3,1) == state(3,3) && state(3,1) ~= -1)
won = state(3,1);
% Vert check
elseif (state(1,1) == state(2,1) && state(1,1) == state(3,1) && state(3,1) ~= -1)
won = state(1,1);
elseif (state(1,2) == state(2,2) && state(1,2) == state(3,2) && state(1,2) ~= -1)
won = state(1,2);
elseif (state(1,3) == state(2,3) && state(1,3) == state(3,3) && state(1,3) ~= -1)
won = state(1,3);
% Diagonal check
elseif (state(1,1) == state(2,2) && state(1,1) == state(3,3) && state(1,1) ~= -1)
won = state(1,1);
elseif (state(1,3) == state(2,2) && state(1,3) == state(3,1) && state(2,2) ~= -1)
won = state(1,3);
% tie case
elseif ~ismember(state, -1)
won = 2;
else
won = -1;
end
end

function [col row] = position(x, y)
col = floor(x);
row = floor(y);
if col > 2
col = 2;
end
  
if row > 2
row = 2;
end
end
%draw the x player
function drawX(col, row)
hold on
x = 0:1;
pos = 0:1;
neg = 1-x;
plot(x+col, pos+row)
plot(x+col, neg+row)
hold off
end
%draw o player
function drawO(col, row)
hold on
k = 0:0.1:2*pi;
x = cos(k)/2+0.5;
y = sin(k)/2+0.5;
plot(x+col, y+row)
hold off
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote