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

I have to create a game of tic tac toe using the numerical keypad as the board (

ID: 3935467 • Letter: I

Question

I have to create a game of tic tac toe using the numerical keypad as the board (1-9 are the moves for the game) where the users input print to a board created by a function. My board function( print_board) was given to use by the teacher but I keep getting errors. Here is my code:

board = [' ';' ';' ';' ';' ';' ';' ';' ';' '];

function [ ] = print_board( b )
%PRINT_BOARD Summary of this function goes here
% Detailed explanation goes here

XO = char(' ', 'X', 'O');

fprintf (' | | ')
fprintf (' %s | %s | %s ', XO(b(7)),XO(b(8)),XO(b(9)));
fprintf (' | | ')
fprintf ('------------ ')
fprintf (' | | ')
fprintf (' %s | %s | %s ', XO(b(4)),XO(b(5)),XO(b(6)));
fprintf (' | | ')
fprintf ('------------ ')
fprintf (' | | ')
fprintf (' %s | %s | %s ', XO(b(1)),XO(b(2)),XO(b(3)));
fprintf (' | | ')

end

The errors I am getting is:

>> print_board
| |
Not enough input arguments.

Error in print_board (line 8)
fprintf (' %s | %s | %s ', XO(b(7)),XO(b(8)),XO(b(9)));

>> board = [' ';' ';' ';' ';' ';' ';' ';' ';' '];
>> print_board(board)
| |
Index exceeds matrix dimensions.

Error in print_board (line 8)
fprintf (' %s | %s | %s ', XO(b(7)),XO(b(8)),XO(b(9)));

Explanation / Answer

The following code works:

function [ ] = printboard( b )
%PRINT_BOARD Summary of this function goes here
%   Detailed explanation goes here

fprintf ('   |   | ')
fprintf (' %s | %s | %s ', b(7),b(8),b(9));
fprintf ('   |   | ')
fprintf ('------------ ')
fprintf ('   |   | ')
fprintf (' %s | %s | %s ', b(4),b(5),b(6));
fprintf ('   |   | ')
fprintf ('------------ ')
fprintf ('   |   | ')
fprintf (' %s | %s | %s ', b(1),b(2),b(3));
fprintf ('   |   | ')

end