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

function TicTacToe() board = [ ... [\' \', \' \', \' \']; ... [\' \', \' \', \'

ID: 3568481 • Letter: F

Question

function TicTacToe()

board = [ ...

[' ', ' ', ' ']; ...

[' ', ' ', ' ']; ...

[' ', ' ', ' '] ...

];

turn = 1; %% human:

while ~GameOver(board)

Draw(board);

if turn == 1 %% human's turn:

board = HumanMove(board);

turn = 2;

else %% computer's turn:

board = ComputerMove(board);

turn = 1;

end

end

fprintf(' ');

fprintf('########################## ');

fprintf(' ');

fprintf('Game over! Final board: ');

Draw(board);

if Winner(board, 'X')

fprintf('Human wins! ');

elseif Winner(board, 'O')

fprintf('Computer wins! ');

else

fprintf('Game was a draw... ');

end

fprintf(' ');

end

%% Draws the board on the screen:

function Draw(board)

fprintf(' ');

for r=1:3

fprintf(' ');

for c=1:3

fprintf(' %c ', board(r, c));

if c < 3

fprintf('|');

end

end

fprintf(' ');

if r < 3

fprintf(' ---|---|--- ');

end

end

fprintf(' ');

end

%% Allows the human to enter their move,

%% and updates the board:

function board = HumanMove(board)

row = input('Your turn, row? ');

col = input('Your turn, col? ');

board(row, col) = 'X';

end

%% Allows the computer to make a move:

function board = ComputerMove(board)

%% find first open spot -- we know there

%% is one since game is not over:

[rows, cols] = find(board == ' ');

%% put an O in that spot:

row = rows(1);

col = cols(1);

board(row, col) = 'O';

end

%% Returns true if the given player has won, where player

%% is either 'X' or 'O'

function won = Winner(board, player)

%% TODO! You need to write this function...

won = false;

end

%% Returns true if the board is full (i.e. there are

%% no open spaces), false if not.

function full = BoardFull(board)

%% TODO! You need to write this function...

full = false;

end

%% Returns true if the game is over, false if not:

function over = GameOver(board)

%% TODO! You need to write this function...

over = false;

end

Explanation / Answer

#include #include char board[3][3]; // the tic tac toe board. Array 3 x 3 char check(void); // Function check void init_board(void); //function inisalizing board void get_player_move(void); //Function to get players move void get_player2_move(void); //function to get player 2 move void disp_board(void); //function to display board. int main(void) { char done; printf("Tic Tac Toe Game. "); printf("The World's Number 1 Online Game. "); char player1[10]; char player2[10]; printf("Player 1 Name : ", player1); scanf("%s", &player1); printf("Player 2 Name : ", player2); scanf("%s", &player2); done = ' '; init_board(); do { disp_board(); get_player_move(); disp_board(); //Displays board after each move. done = check(); // see if there is a winner. if(done!= ' ') break; // winner get_player2_move(); done = check(); // see if there is a winner after player 2 completes its move. } while(done== ' '); if(done=='X') printf("%s has won!!! ", player1); else printf("%s has won!!!! ", player2); system("pause"); } // Initialize the board using an Array. void init_board(void) { int i=0; int j=0; for(i=0; i