Topics: User Defined Functions and MATLAB Conditionals A Childrens game studio n
ID: 3847493 • 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
Here is the code with comments. Please do rate the answer if it helped Thanks.
board=[ [' ' ' ' ' '],
[' ' ' ' ' '],
[' ' ' ' ' ']]; %intital state of board
turn = 1 ; %x's turn to play
moves = 0 ;% no of moves, total will be 9
winner=-1
player='X'
while moves < 9
moves=moves + 1 ;
make_move(board,turn);
turn=next_move(board,turn);
if turn == -1 %already some winner
break
end
end
if (winner==1)
disp('Player X wins')
elseif (winner==0)
disp ('Player O wins')
else
disp ('Its a draw')
end
function disp_board(board)
for i=1:1:3
fprintf(' %c | %c | %c ',board(i,1),board(i,2),board(i,3) );
fprintf(" ------------------- ");
end
end
function make_move(board,turn)
rep=true;
while rep
if turn==1
msg= "Player X: Enter a move 1-9";
player='X';
else
msg="Player O: Enter a move 1-9";
player='O';
end
num=input(msg);
%check if the location entered is not occupied yet
if (num== 1 && board(1,1)==' ' )
board(1,1)=player;
rep=false;
elseif (num==2 && board(1,2)==' ')
board(1,2)=player;
rep=false;
elseif (num==3 && board(1,3)==' ')
board(1,3)=player;
rep=false;
elseif (num==4 && board(2,1)==' ')
board(2,1)=player;
rep=false;
elseif(num== 5 && board(2,2)==' ')
board(2,2)=player;
rep=false;
elseif (num==6 && board(2,3)==' ')
board(2,3)=player;
rep=false;
elseif (num== 7 && board(3,1)==' ')
board(3,1)=player;
rep=false;
elseif (num==8 && board(3,2)==' ')
board(3,2)=player;
rep=false;
elseif (num==9 && board(3,3)==' ')
board(3,3)=player;
rep=false;
else
rep=true;
end
if(rep==false)
break
end
end
end
%function to calculate if the player in turn is winner or if not winner , change to next player's turn
function turn=next_move(board,turn)
if((board(1,1)==player && board(1,2)==player && board(1,3)==player) || %top horizontal
(board(2,1)==player && board(2,2)==player && board(2,3)==player) || %center horizontal
(board(3,1)==player && board(3,2)==player && board(3,3)==player) || %bottom horizontal
(board(1,1)==player && board(2,1)==player && board(3,1)==player) || %left vertical
(board(1,2)==player && board(2,2)==player && board(3,2)==player) || %center vertical
(board(1,3)==player && board(2,3)==player && board(3,3)==player) || %right vertical
(board(1,1)==player && board(2,2)==player && board(3,3)==player) || %diagonal from 1,1
(board(1,3)==player && board(2,2)==player && board(3,1)==player) ) %diagonal from 1,3
winner=turn;
turn=-1;
else
turn=1-turn;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.