Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why wont this code compile on gradscope? #include #include #define SIZE 3 #define EMPTY ' ' char board [ SIZE ] [ SIZE ] ;

Why wont this code compile on gradscope?
#include
#include
#define SIZE 3
#define EMPTY ''
char board[SIZE][SIZE];
const char PLAYER_X ='X';
const char PLAYER_O ='O';
void resetBoard();
void printBoard();
int checkFreeSpaces();
void playerMove(char player);
char checkWinner();
void printWinner(char winner);
int main(){
char winner = EMPTY;
char currentPlayer = PLAYER_X;
resetBoard();
while (winner == EMPTY && checkFreeSpaces()!=0){
printBoard();
if (currentPlayer == PLAYER_X){
printf("X's turn >");
} else {
printf("O's turn >");
}
playerMove(currentPlayer);
winner = checkWinner();
currentPlayer =(currentPlayer == PLAYER_X)? PLAYER_O : PLAYER_X;
}
printBoard();
printWinner(winner);
return 0;
}
void resetBoard(){
for (int i =0; i < SIZE; i++){
for (int j =0; j < SIZE; j++){
board[i][j]= EMPTY;
}
}
}
void printBoard(){
for (int i =0; i < SIZE; i++){
for (int j =0; j < SIZE; j++){
printf("%c ", board[i][j]);
if (j < SIZE -1) printf("|");
}
printf("
");
if (i < SIZE -1) printf("---|---|---
");
}
}
int checkFreeSpaces(){
int freeSpaces = SIZE * SIZE;
for (int i =0; i < SIZE; i++){
for (int j =0; j < SIZE; j++){
if (board[i][j]!= EMPTY){
freeSpaces--;
}
}
}
return freeSpaces;
}
void playerMove(char player){
int x, y;
while (1){
if (scanf("%d %d", &x, &y)!=2){
fprintf(stderr, "Need 2 co-ordinates
");
while (getchar()!='
'); // clear the input buffer
continue;
}
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);
continue;
}
x--; y--; // adjust for 0-indexed array
if (board[x][y]!= EMPTY){
fprintf(stderr,"%c has played %d,%d
", board[x][y], x +1, y +1);
continue;
}
board[x][y]= player;
break;
}
}
char checkWinner(){
// Check rows and columns
for (int i =0; i < SIZE; i++){
if (board[i][0]!= EMPTY && board[i][0]== board[i][1] && board[i][1]== board[i][2]) return board[i][0];
if (board[0][i]!= EMPTY && board[0][i]== board[1][i] && board[1][i]== board[2][i]) return board[0][i];
}
// Check diagonals
if (board[0][0]!= EMPTY && board[0][0]== board[1][1] && board[1][1]== board[2][2]) return board[0][0];
if (board[0][2]!= EMPTY && board[0][2]== board[1][1] && board[1][1]== board[2][0]) return board[0][2];
return EMPTY;
}
void printWinner(char winner){
if (winner == PLAYER_X){
printf("X wins!
");
} else if (winner == PLAYER_O){
printf("O wins!
");
} else {
printf("It's a tie!
");
}
}

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_2

Step: 3

blur-text-image_3

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions