A Childrens game studio needs you to write a MATLAB Program to allow two players
ID: 3842870 • Letter: A
Question
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
Code:
Note: With the provided time,i couldnt take the screenshot of the code also i could not take the full output.
%displayBoard.m
function displayBoard(myBoard)
fprintf(' ');
for kk=1:9
if(myBoard(kk)==-1)
fprintf(' ');
end
if(myBoard(kk)==1)
fprintf('X');
end
if(myBoard(kk)==2)
fprintf('O');
end
if(mod(kk,3)==0 && mod(kk,9)~=0)
fprintf(' ------------- ');
elseif(mod(kk,9)~=0)
fprintf('|');
end
end
%ticgame.m
fprintf('Welcome to Tic-Tac-Tow: X goes first');
fprintf(' -------------------------------------');
myBoard=zeros(1,9);
%initialize the board
for kk=1:9
myBoard(kk)=-1;
end
stalemateCnt=0;
win=false;
displayBoard(myBoard);
while not(win)
for kk=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=myBoard(pos);
if(tp==-1)
myBoard(pos)=kk;
stalemateCnt = stalemateCnt+1;
displayBoard(myBoard);
end
end
end
winChr =1 ;
for aa=1:2
if((myBoard(1)==aa) && (myBoard(2)==aa) && myBoard(3)==aa)
win =true;
winchr=aa;
elseif(myBoard(4)==aa && myBoard(5)==aa && myBoard(6)==aa)
win=true;
winchr=aa;
elseif(myBoard(7)==aa && myBoard(8)==aa && myBoard(9)==aa)
win=true;
winchr=aa;
elseif(myBoard(1)==aa && myBoard(5)== aa && myBoard(9)==aa)
win=true;
winchr=aa;
elseif(myBoard(3)==aa && myBoard(5)==aa && myBoard(7)==aa)
win=true;
winchr=aa;
elseif(myBoard(1)==aa && myBoard(4)==aa && myBoard(7)==aa)
win=true;
winchr=aa;
elseif(myBoard(2)==aa && myBoard(5)== aa && myBoard(8)==aa)
win=true;
winchr=aa;
elseif(myBoard(3)==aa && myBoard(6)==aa && myBoard(9)==aa)
win=true;
winchr=aa;
end
end
if(win==true)
if(winchr==1)
fprintf('Congrats - X Wins !');
elseif(winchr==2)
fprintf('Congrats - O Wins !');
end
end
if(stalemateCnt==9 && win~=true)
fprintf('GAME TIE!');
win=true;
end
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.