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

1. This is a two player (X\'s and O\'x) board game. 2. The game board is a 3 X 3

ID: 3857237 • Letter: 1

Question

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

2. The game board is a 3 X 3 grid, where each location can hold either and 'X' an 'O' or a space (empty) Note: we will linearize the grid and refer to the locations 1-9, for conveyance.

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

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

5.- 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:

1. Create a MATLAB Script.m file

A) Establish variable(s) to represent the nine board locations

B)Create the game loop

i. Display the current state of the board - using separate displayboad function

ii) get a "valid" move location - note this can only be an empty location

iii) Modify the board variable(s)

iv) check for a win and report if so - game must end

v) Check for a stalemate - game must end

2. Create a MATLAB funciton.m file

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

B) Write the necessary MATLAB commands to display the Tic-Tac-Toe board, using the give inputs.

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

NOTE: 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.

Explanation / Answer

%displayBoard.m

function displayBoard(tttBoard)
fprintf(' ');
for it=1:9

if(tttBoard(it)==-1)
fprintf(' ');
end
if(tttBoard(it)==1)
fprintf('X');
end
if(tttBoard(it)==2)
fprintf('O');
end
if(mod(it,3)==0 && mod(it,9)~=0)
fprintf(' ------------- ');
elseif(mod(it,9)~=0)
fprintf('|');
end

end


%tictactogame.m
fprintf('Welcome to Tic-Tac-Tow: X goes first');
fprintf(' -------------------------------------');
tttBoard=zeros(1,9);
%initialize the board
for it=1:9
tttBoard(it)=-1;
end
scnt=0;
win=false;
displayBoard(tttBoard);
while not(win)
for it=1:2
if(not(win))
fg=false;
while not(fg)
pos=input(' Please enter location to move [1-9]:');
if(pos>=1 && pos<=9)
fg=true;
tp=tttBoard(pos);
if(tp==-1)   
tttBoard(pos)=it;
scnt = scnt+1;
displayBoard(tttBoard);
end
end
end

playerWin =1 ;
for aa=1:2
if((tttBoard(1)==aa) && (tttBoard(2)==aa) && tttBoard(3)==aa)
win =true;
playerWin=aa;
elseif(tttBoard(4)==aa && tttBoard(5)==aa && tttBoard(6)==aa)
win=true;
playerWin=aa;
elseif(tttBoard(7)==aa && tttBoard(8)==aa && tttBoard(9)==aa)
win=true;
playerWin=aa;
elseif(tttBoard(1)==aa && tttBoard(5)== aa && tttBoard(9)==aa)
win=true;
playerWin=aa;
elseif(tttBoard(3)==aa && tttBoard(5)==aa && tttBoard(7)==aa)
win=true;
playerWin=aa;
elseif(tttBoard(1)==aa && tttBoard(4)==aa && tttBoard(7)==aa)
win=true;
playerWin=aa;
elseif(tttBoard(2)==aa && tttBoard(5)== aa && tttBoard(8)==aa)
win=true;
playerWin=aa;
elseif(tttBoard(3)==aa && tttBoard(6)==aa && tttBoard(9)==aa)
win=true;
playerWin=aa;
end
end
if(win==true)

if(playerWin==1)
fprintf('Congrats - X Wins !');
elseif(playerWin==2)
fprintf('Congrats - O Wins !');
end
  
end
if(scnt==9 && win~=true)
  
fprintf('GAME TIE!');
win=true;
end
end
end
end

%end matlab code

%{
output:

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!

%}