Enhance the tic-tac-toe game so 2 people can play. To do this, you need to add an“O” that, like the “X”, is drawn in the square. Then prompt the user for a square identifier, and alternate between drawing “X”s and “O”s at those locations on the board. The first move is for “X”. You need to detect and reject when a user plays a square that is already taken. Stop after the board is full (that is, 9 plays). You do not have to worry about who wins.
The “O” is to be 5 × 5, centered in the middle of the square.
Here are the messages your program should print to the standard output:
The tic-tac-toe board, with “X”s and “O”s as appropriate;
When it is “X”’s turn, print “X’s turn > ” (note the space after the “>”); and
When it is “O”’s turn, print “O’s turn > ” (again, note the space after the “>”).
Here are the error messages; all are to be printed on the standard error:
When the user enters only 1 co-ordinate: “Need 2 co-ordinatesn” (the ‘n’ is a newline);
When there is an illegal character in the input: “Illegal character in input “%c”n” (the “%c” is to print the offending character); and
When the square is already occupied: “%c has played %d,%dn” (where “%c” is either “X” or “O”, whichever is already in the square, and “%d,%d” are the co-ordinates of the occupied square); and
When an invalid set of co-ordinates are entered: “%d,%d” is not a valid square; the numbers must be between 1 and 3 inclusiven” (each %d is one of the invalid numbers).
If the program reads an end of file at the prompt, print a newline and quit.
The program should exit with an exit status code of 0.
- In problem 1, if the square is occupied, you need to give the error message. “%c has played %d,%dn” (where “%c” is either “X” or “O”, whichever is already in the square, and “%d,%d” are the co-ordinates of the occupied square).
- In problem 1, the dimensions of the “O” was added; it is to be 5 × 5 centered in the square.
- For all problems, the exit status code is 0.
If a blank line is input, exit without a message, just as if an EOF were typed.
On an error, don’t reprint the board, with one exception: if the uer enters the co-ordinates of a square that is occupied, reprint the board to refresh their memory of what squares are, and are not, available.
I tried the code at the bottom and it compiles everywhere else except for Gradescope which is where I need to submit this assignment. The error I get when I submit it is Test Failed: 1 != 0 : Compilation (gcc -ansi -pedantic -Wall -o /autograder/source/tests/ttt4a/ttt4a /autograder/submission/ttt4a.c
) failed and won’t compile. Please let me know what error was made!
#include <stdio.h>
#include <stdlib.h>
#define BOARD_SIZE 3
char board[BOARD_SIZE][BOARD_SIZE]; // The tic-tac-toe board
// Initialize the board with empty spaces
void initialize_board() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
}
// Print the current state of the board
void print_board() {
printf("n");
printf(" 1 2 3n");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i + 1);
for (int j = 0; j < BOARD_SIZE; j++) {
printf("%c", board[i][j]);
if (j < BOARD_SIZE - 1) {
printf("|");
}
}
printf("n");
if (i < BOARD_SIZE - 1) {
printf(" -----n");
}
}
printf("n");
}
// Check if the given player has won
int is_winner(char player) {
// Check rows
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return 1;
}
}
// Check columns
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
return 1;
}
}
// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return 1;
}
return 0;
}
// Check if the board is full
int is_board_full() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == ' ') {
return 0; // Board is not full
}
}
}
return 1; // Board is full
}
// Check if the given move is valid
int is_valid_move(int row, int col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == ' ';
}
// Make a move for the given player at the specified row and column
void make_move(char player, int row, int col) {
board[row][col] = player;
}
int main() {
initialize_board(); // Initialize the board
char player = 'X'; // Player X starts the game
int row, col;
print_board(); // Print the initial board
while (1) {
if (player == 'X') {
printf("X's turn > ");
} else {
printf("O's turn > ");
}
int result = scanf("%d,%d", &row, &col); // Read player's input
if (result == EOF) {
printf("n");
return 0; // Exit gracefully on EOF
} else if (result != 2) {
fprintf(stderr, "Need 2 co-ordinatesn"); // Handle incorrect input format
continue;
}
row--; // Adjusting to array indexing
col--;
if (!is_valid_move(row, col)) {
fprintf(stderr, "%d,%d is not a valid square; the numbers must be between 1 and 3 inclusiven", row + 1, col + 1); // Handle invalid move
continue;
}
if (board[row][col] != ' ') {
fprintf(stderr, "%c has played %d,%dn", board[row][col], row + 1, col + 1); // Handle occupied square
print_board(); // Reprint the board to refresh the memory
continue;
}
make_move(player, row, col); // Make the move
print_board(); // Print the updated board
if (is_winner(player)) {
printf("%c wins!n", player); // Check if the current player wins
break;
}
if (is_board_full()) {`your text`
printf("It's a tie!n"); // Check if the game is a tie
break;
}
player = (player == 'X') ? 'O' : 'X'; // Switch player
}
return 0; // Exit successfully
}
Marco Hernandez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.