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

I\'m stucked with my matlab assignment Here\'s what I got so far... fprintf(\'We

ID: 3858938 • Letter: I

Question

I'm stucked with my matlab assignment

Here's what I got so far...

fprintf('Welcome to Tic-Tac-Toe : X goes first ');

fprintf('------------------------------------- ');

loc1 = ' ';

loc2 = ' ';

loc3 = ' ';

loc4 = ' ';

loc5 = ' ';

loc6 = ' ';

loc7 = ' ';

loc8 = ' ';

loc9 = ' ';

for i = 1:5

  

   displayBoard(loc1,loc2,loc3,loc4,loc5,loc6,loc7,loc8,loc9);

   player{i} = input('Please enter location to move [1 - 9] : ');

   fprintf(' ');

while ((player{i}<1 || player{i}>9))

player{i} = input('Please enter location to move [1 - 9] : ');

fprintf(' ');

end

if player{i} == 1

loc1 = 'X';

elseif player{i} == 2

loc2 = 'X';

elseif player{i} == 3

loc3 = 'X';

elseif player{i} == 4

loc4 = 'X';

elseif player{i} == 5

loc5 = 'X';

elseif player{i} ==6

loc6 = 'X';

elseif player{i} == 7

loc7 = 'X';

elseif player{i} == 8

loc8 = 'X';

elseif player{i} == 9

loc9 = 'X';

end

displayBoard(loc1,loc2,loc3,loc4,loc5,loc6,loc7,loc8,loc9);

player{i+1} = input('Please enter location to move [1 - 9] : ');

fprintf(' ');

  

while ((player{i+1}<1 || player{i+1}>9) && (player{i}==player{i+1}))

player2 = input('Please enter location to move [1 - 9] : ');

fprintf(' ');

end

if player{i+1} == 1

loc1 = 'O';

player{i+1} = loc1;

elseif player{i+1} == 2

loc2 = 'O';

player{i+1} = loc2;

elseif player{i+1} == 3

loc3 = 'O';

player{i+1} = loc3;

elseif player{i+1} == 4

loc4 = 'O';

player{i+1} = loc4;

elseif player{i+1} == 5

loc5 = 'O';

player{i+1} = loc5;

elseif player{i+1} ==6

loc6 = 'O';

player{i+1} = loc6;

elseif player{i+1} == 7

loc7 = 'O';

player{i+1} = loc7;

elseif player{i+1} == 8

loc8 = 'O';

player{i+1} = loc8;

elseif player{i+1} == 9

loc9 = 'O';

player{i+1} = loc9;

end

if (loc1==loc4==loc7)

fprintf('Congrats - %s Wins',loc1);

elseif(loc2==loc5==loc8)

   fprintf('Congrats - %s Wins',loc2);

elseif(loc3==loc6==loc9)

   fprintf('Congrats - %s Wins',loc3);

elseif(loc1==loc5==loc9)

   fprintf('Congrats - %s Wins',loc1);

elseif(loc3==loc5==loc7)

fprintf('Congrats - %s Wins',loc3);

elseif(loc1==loc2==loc3)

   fprintf('Congrats - %s Wins',loc1);

elseif(loc4==loc5==loc6)

   fprintf('Congrats - %s Wins',loc4);

elseif(loc7==loc8==loc9)

   fprintf('Congrats - %s Wins',loc7);

break;

  

end

end

if ~((loc1==loc4==loc7) || (loc2==loc5==loc8) || (loc3==loc6==loc9) || (loc1==loc5==loc9) || (loc3==loc5==loc7))

fprintf('Sorry, it''s stalemate');

end

For the function part

function [] = displayBoard(loc1,loc2,loc3,loc4,loc5,loc6,loc7,loc8,loc9)

fprintf(' %s | %s | %s ', loc1, loc2, loc3);

fprintf('----------- ');

fprintf(' %s | %s | %s ', loc4, loc5, loc6);

fprintf('----------- ');

fprintf(' %s | %s | %s ', loc7, loc8, loc9);

end

My assignment guide

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

function [] = displayBoard(loc)

fprintf(' %s | %s | %s ', loc(1), loc(2), loc(3));
fprintf('----------- ');
fprintf(' %s | %s | %s ', loc(4), loc(5), loc(6));
fprintf('----------- ');
fprintf(' %s | %s | %s ', loc(7), loc(8), loc(9));
end

function winner = checkWin(loc)
winner = -1
if loc(1) == loc(2) && loc(2) == loc(3) && (loc(1) != 0)
winner = loc(1)
end
  
if loc(4) == loc(5) && loc(5) == loc(6) && loc(4) != 0
winner = loc(4)
end
  
if loc(7) == loc(8) && loc(8) == loc(9) && loc(7) != 0
winner = loc(7)
end
  
if loc(1) == loc(4) && loc(4) == loc(7) && loc(1) != 0
winner = loc(1)
end
  
if loc(2) == loc(5) && loc(5) == loc(8) && loc(2) != 0
winner = loc(2)
end
  
if loc(3) == loc(6) && loc(6) == loc(9) && loc(3) != 0
winner = loc(3)
end
  
if loc(1) == loc(5) && loc(5) == loc(9) && loc(1) != 0
winner = loc(1)
end
  
if loc(3) == loc(5) && loc(5) == loc(7) && loc(3) != 0
winner = loc(3)
end
end

fprintf('Welcome to Tic-Tac-Toe : X goes first ');
fprintf('------------------------------------- ');
loc = zeros(9, 1)

turn = 1
result = 0
for i = 1:9
displayBoard(loc)
move_ = input('Please enter location to move [1 - 9] : ');
fprintf(' ');
while ((move_<1 || move_>9 || loc(move_) != 0))
move_ = input('Please enter location to move [1 - 9] : ');
fprintf(' ');
end
if turn == 1
loc(move_) = 'X'
else
loc(move_) = 'O'
end
if turn == 1
turn = 0
else
turn = 1
end
if i > 4
winner = checkWin(loc)
if winner != -1
result = 1
fprintf('Congrats - %s Wins',winner);
fprintf(' ');
break
end
end

end

if result != 1
fprintf('Sorry, it''s stalemate');
fprintf(' ');
end