This program should be written in the C language Write a program that declares a
ID: 3604837 • Letter: T
Question
This program should be written in the C language
Write a program that declares a 2 dimensional array of chars with 3 rows and 3 columns. Write functions called initializeBoard(), displayBoard(), and makeMove(). initializeBoard() should fill the array with space characters (‘ ‘), displayBoard() should display the current board, with the rows and columns labled, and makeMove() should ask the user to enter a row and a column and change that square to alternating X’s or a O’s. Display the board again after each move. The program should run for 9 moves.
Use the following function prototypes:
void initializeBoard(char board[][3]);
void displayBoard(char board[][3]);
void makeMove(char board[][3], char player);
(TIP: use scanf(“%d %d”, &row, &column); use char board[3][3];)
The output should like the picture below
2I 3 Enter the row and column you'd like to fill:2 2 2 o] 3 1I Enter the row and column you'd 1ike to fill:3 3 2 I o] 3 [x Enter the row and column you'd like to fill:l 1 1 o 2 [o] 3 1 1[XExplanation / Answer
Give below is the code as per specification of the program,. There are no validations to check if hte board position is already containing a character x or o. Chegg editor removes all extra spaces, so in the displayBoard function where column numbers are printed, please provide proper number of spaces so that column number fall in a line with other rows.
Please don't forget to rate the answer if it helped. Thank you.
#include <stdio.h>
void initializeBoard(char board[][3]);
void displayBoard(char board[][3]);
void makeMove(char board[][3], char player);
char checkWinner(char board[][3], int moves);
int main()
{
int moves = 1;
char board[3][3];
char player = 'o';
char winner;
initializeBoard(board);
while(moves <= 9 && (winner=checkWinner(board, moves)) == ' ')
{
displayBoard(board);
makeMove(board, player);
if(player == 'o')
player = 'x';
else
player = 'o';
moves++;
}
displayBoard(board);
if(winner == 't')
printf("It's a tie ");
else
printf("%c won ! ", winner );
}
void initializeBoard(char board[][3])
{
int i , j;
for(i = 0 ;i < 3; i++)
for( j = 0; j < 3; j++)
board[i][j] = ' ';
}
void displayBoard(char board[][3])
{
//print the column numbers in 1st line
printf(" 1 2 3 ");
for(int i = 0; i < 3; i++)
{
//print the row number
printf("%d ", (i+1));
for(int j = 0; j < 3; j++)
printf("[%c] ", board[i][j]);
printf(" ");
}
}void makeMove(char board[][3], char player)
{
int row, col;
do
{
printf("Enter the row and column you'd like to fill: ");
scanf("%d %d", &row, &col);
if(board[row-1][col-1] != ' ')
printf("The position is already filled ");
}while(board[row-1][col-1] != ' ');
board[row-1][col-1] = player;
}
//returns the winnner ('o' or 'x') if any , it tie returns 't' and if no winner yet, returns blank i.e ' '
char checkWinner(char board[][3], int moves)
{
char winner = ' '; //
//top horizontal line
if( board[0][0] == board[0][1] && board[0][1] == board[0][2])
winner = board[0][0];
//middle horizontal line
else if(board[1][0] == board[1][1] && board[1][1] == board[1][2])
winner = board[1][0];
//bottom horizontal line
else if(board[2][0] == board[2][1] && board[2][1] == board[2][2])
winner = board[2][0];
//left vertical line
else if( board[0][0] == board[1][0] && board[1][0] == board[2][0])
winner = board[0][0];
//middle vertical line
else if(board[0][1] == board[1][1] && board[1][1] == board[2][1])
winner = board[0][1];
//right verticle line
else if(board[0][2] == board[1][2] && board[1][2] == board[2][2])
winner = board[0][2];
//diagonal starting at left top
else if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
winner = board[0][0];
//diagonal starting at right top
else if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
winner = board[0][2];
else
{
if(moves == 9)
winner = 't'; //tie
else
winner = ' '; //no winner yet
}
return winner;
}
output
1 2 3
1 [ ] [ ] [ ]
2 [ ] [ ] [ ]
3 [ ] [ ] [ ]
Enter the row and column you'd like to fill: 2 2
1 2 3
1 [ ] [ ] [ ]
2 [ ] [o] [ ]
3 [ ] [ ] [ ]
Enter the row and column you'd like to fill: 3 3
1 2 3
1 [ ] [ ] [ ]
2 [ ] [o] [ ]
3 [ ] [ ] [x]
Enter the row and column you'd like to fill: 1 1
1 2 3
1 [o] [ ] [ ]
2 [ ] [o] [ ]
3 [ ] [ ] [x]
Enter the row and column you'd like to fill: 1 2
1 2 3
1 [o] [x] [ ]
2 [ ] [o] [ ]
3 [ ] [ ] [x]
Enter the row and column you'd like to fill: 1 3
1 2 3
1 [o] [x] [o]
2 [ ] [o] [ ]
3 [ ] [ ] [x]
Enter the row and column you'd like to fill: 2 3
1 2 3
1 [o] [x] [o]
2 [ ] [o] [x]
3 [ ] [ ] [x]
Enter the row and column you'd like to fill: 2 1
1 2 3
1 [o] [x] [o]
2 [o] [o] [x]
3 [ ] [ ] [x]
Enter the row and column you'd like to fill: 3 1
1 2 3
1 [o] [x] [o]
2 [o] [o] [x]
3 [x] [ ] [x]
Enter the row and column you'd like to fill: 3 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.