Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why does my code not compile on gradescope? #include #include #define SIZE 3 void printBoard ( char board [ SIZE ] [ SIZE ] )

Why does my code not compile on gradescope?
#include
#include
#define SIZE 3
void printBoard(char board[SIZE][SIZE]);
int isValidInput(char *input, int *x, int *y);
int isOccupied(char board[SIZE][SIZE], int x, int y);
void drawO(char board[SIZE][SIZE], int x, int y);
int main(){
char board[SIZE][SIZE]={{'','',''},{'','',''},{'','',''}};
char input[100];
int turn =0; //0 for X,1 for O
int x, y;
while (1){
printBoard(board);
if (turn %2==0){
printf("x's turn >");
} else {
printf("o's turn >");
}
if (fgets(input, sizeof(input), stdin)== NULL){
printf("
");
break;
}
if (!isValidInput(input, &x, &y)){
continue;
}
x -=1; // Adjust for 0-based index
y -=1; // Adjust for 0-based index
if (isOccupied(board, x, y)){
continue;
}
if (turn %2==0){
board[x][y]='X';
} else {
drawO(board, x, y);
}
turn++;
if (turn ==9){
break;
}
}
printBoard(board);
return 0;
}
void printBoard(char board[SIZE][SIZE]){
int i, j;
for (i =0; i < SIZE; i++){
for (j =0; j < SIZE; j++){
printf("%c", board[i][j]);
if (j < SIZE -1) printf("|");
}
printf("
");
if (i < SIZE -1) printf("-----
");
}
}
int isValidInput(char *input, int *x, int *y){
int n = sscanf(input,"%d,%d", x, y);
if (n ==1){
fprintf(stderr, "Need 2 co-ordinates
");
return 0;
}
if (n !=2){
fprintf(stderr, "Illegal character in input \"%s\"
", input);
return 0;
}
if (*x <1||*x >3||*y <1||*y >3){
fprintf(stderr,"%d,%d is not a valid square; the numbers must be between 1 and 3 inclusive
",*x,*y);
return 0;
}
return 1;
}
int isOccupied(char board[SIZE][SIZE], int x, int y){
if (board[x][y]!=''){
fprintf(stderr,"%c has played %d,%d
", board[x][y], x +1, y +1);
return 1;
}
return 0;
}
void drawO(char board[SIZE][SIZE], int x, int y){
board[x][y]='O';
}
PROBLEM: Enhance the tic-tac-toe game so 2 people can play. To do this, you need to add anO that, like the X, is drawn in the square. Then prompt the user for a square identifier, and alternate between drawing Xs and Os 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\times 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 Xs and Os as appropriate;
When it is Xs turn, print Xs turn >(note the space after the >); and
When it is Os turn, print Os 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-ordinates
(the
is a newline);
When there is an illegal character in the input: Illegal character in input "%c"
(the %c is to print the offending character); and
When the square is already occupied: %c has played %d,%d
(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 inclusive
(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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions

Question

Cul es la frmula emprica de C 6 H 12 ? C3H6 CH CH2 C6H12

Answered: 1 week ago