Question
Need to code this. Confused on how to do it. Prints example like shown on at end of page.thanks // ticTacShell.c // // Shell of
// ticTacShell.c
//
// Shell of the game 'TicTacToe' for CpSc 1010/1011
//
#include
#include
#include
// Size of the board (square)
const int BOARD_SIZE = 3;
// Symbols used for the board
const char BLANK_SYMBOL = ' ';
const char COMP_SYMBOL = 'O';
const char HUMAN_SYMBOL = 'X';
// Human goes first
const int HUMANS_TURN = 0;
const int COMPUTERS_TURN = 1;
// Function prototypes
void initializeBoard(char board[BOARD_SIZE][BOARD_SIZE]);
int hasWon(char board[BOARD_SIZE][BOARD_SIZE], char mark);
int hasWonHorizontal(char board[BOARD_SIZE][BOARD_SIZE], char mark);
int hasWonVertical(char board[BOARD_SIZE][BOARD_SIZE], char mark);
int hasWonDiagonal(char board[BOARD_SIZE][BOARD_SIZE], char mark);
void getComputerMove(char board[BOARD_SIZE][BOARD_SIZE]);
void getHumanMove(char board[BOARD_SIZE][BOARD_SIZE]);
void printBoard(char board[BOARD_SIZE][BOARD_SIZE]);
void clearScreen(void);
//
// The main function should not be changed
//
int main(void) {
char board[BOARD_SIZE][BOARD_SIZE];
int humanWon = 0; // boolean (0/1)
int computerWon = 0; // boolean (0/1)
int move = 0;
// Seed the random number generator
srand(time(0));
initializeBoard(board);
while ((move
clearScreen();
if ((move % 2) == COMPUTERS_TURN) {
getComputerMove(board);
} else {
printBoard(board);
getHumanMove(board);
}
computerWon = hasWon(board, COMP_SYMBOL);
humanWon = hasWon(board, HUMAN_SYMBOL);
move++;
}
clearScreen();
printBoard(board);
if (humanWon) {
printf(">>>> You won! ");
} else if (computerWon) {
printf("
} else { // move >= BOARD_SIZE * BOARD_SIZE
printf("==== A Draw ");
}
return 0;
}
//
// Initialized the board to all BLANK_SYMBOL
//
void initializeBoard(char board[BOARD_SIZE][BOARD_SIZE]) {
int row;
for (row = 0; row
int col;
for (col = 0; col
board[row][col] = BLANK_SYMBOL;
}
}
}
//
// Determines if the 'mark' completely fills a row, column, or diagonal
// returns 1 if yes, 0 if no
//
int hasWon(char board[BOARD_SIZE][BOARD_SIZE], char mark) {
return hasWonHorizontal(board, mark)
|| hasWonVertical(board, mark)
|| hasWonDiagonal(board, mark);
}
//
// Determines if the 'mark' completely fills a row
// returns 1 if yes, 0 if no
//
int hasWonHorizontal(char board[BOARD_SIZE][BOARD_SIZE], char mark) {
int won = 0; // boolean (0/1). Assume lost until proven true
int row;
for (row = 0; row
int match = 1; // boolean (0/1)
int col;
for (col = 0; col
if (board[row][col] != mark) {
match = 0;
}
}
won = match;
}
return won;
}
//
// Determines if the 'mark' completely fills a column
// returns 1 if yes, 0 if no
//
int hasWonVertical(char board[BOARD_SIZE][BOARD_SIZE], char mark) {
/* INSERT CODE HERE */
return 0; // Stub -- make this return the correct value
}
//
// Determines if the 'mark' completely fills a diagonal
// returns 1 if yes, 0 if no
//
int hasWonDiagonal(char board[BOARD_SIZE][BOARD_SIZE], char mark) {
/* INSERT CODE HERE */
return 0; // Stub -- make this return the correct value
}
//
// Gets computer move by randomly picking an unoccupied cell
//
void getComputerMove(char board[BOARD_SIZE][BOARD_SIZE]) {
int row;
int col;
do {
row = rand() % BOARD_SIZE;
col = rand() % BOARD_SIZE;
} while (board[row][col] != BLANK_SYMBOL);
board[row][col] = COMP_SYMBOL;
}
//
// Gets human move by prompting user for row and column numbers
//
void getHumanMove(char board[BOARD_SIZE][BOARD_SIZE]) {
/* INSERT CODE HERE */
}
void printBoard(char board[BOARD_SIZE][BOARD_SIZE]) {
/* INSERT CODE HERE */
}
//
// Clears the screen -- uses ANSI terminal control codes
//
void clearScreen(void) {
const char ESC = 27;
printf("%c[2J%c[H", ESC, ESC);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started