Question: Game 3 : Tic Tac Toe Objective: Create a two - player game where players take turns marking spaces on a 3 x 3 grid.
Game : Tic Tac Toe
Objective:
Create a twoplayer game where players take turns marking spaces on a x grid.
Steps:
Setup:
o Display a welcome message and instructions.
o Initialize a x matrix board to represent the game grid:
empty, Player X Player O
Game Logic:
o Use a loop that passes through while the turn count is less than and there is no
winner.
o Display the current board using the disp function
o Use fprintf to display what players turn it is Prompt the current player for a row and
column to mark their move using the input function. Row and Column
o Validate the move to ensure the chosen cell is empty and the input is between
within the allowable row and column range. If the move is invalid, prompt the user to
enter a new move.
o Update the board with the players move. The cell should be filled with a or that
denotes that players move.
o Change the player using conditional statements.
o Check if there is a winner after each move with the function checkWinner and a
conditional statement.
Functions:
o Create a function called checkWinner that outputs if a win has occurred and has
the inputs of the board and current player. Check for a win after each move:
A win occurs if any row, column, or diagonal has the same value all s or all
s Use a combination of logical operators and anyall functions. The diag
function will read along the diagonal of a matrix. Use diagflipud to check
along the other diagonal of the matrix.
o Declare a draw if all cells are filled and no winner is found.
Results:
o Display the final board once there is a winner.
o Use a conditional statement to announce the winner or declare a draw at the end. If
there is a winner, use fprintf to display the winning player.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
