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 3: Tic Tac Toe
Objective:
Create a two-player game where players take turns marking spaces on a 3x3 grid.
Steps:
1. Setup:
o Display a welcome message and instructions.
o Initialize a 3x3 matrix (board) to represent the game grid:
0= empty, 1= Player 1(X),2= Player 2(O).
2. Game Logic:
o Use a loop that passes through while the turn count is less than 9 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 1-3 and Column 1-3.
o Validate the move to ensure the chosen cell is empty (=0) 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 1 or 2 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.
3. 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 1s or all
2s). Use a combination of logical operators and any/all functions. The diag
function will read along the diagonal of a matrix. Use diag(flipud()) to check
along the other diagonal of the matrix.
o Declare a draw if all cells are filled and no winner is found.
4. 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!