Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE CODE GIVEN BELOW. IT IS INCOMPLETE. BUT CODE NEEDS TO BE INSERTED WHERE IT S

ID: 3805520 • Letter: U

Question

USE CODE GIVEN BELOW. IT IS INCOMPLETE. BUT CODE NEEDS TO BE INSERTED WHERE IT STATES INSERT CODE IN BOLD. THE OUTPUT SHOULD LOOK LIKE THE EXAMPLE GIVEN BELOW. HAVE TO USE THE CODE BELOW. DIRECTIONS IN ATTACHMENT.

//
// ticTacShell.c
//
// Shell of the game 'TicTacToe' for CpSc 1010/1011
//
#include <stdio.h>
#include <stdlib.h> // rand(), srand()
#include <time.h>    // time()


// 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 < (BOARD_SIZE * BOARD_SIZE)) && !humanWon && !computerWon) {
        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("<<<< I won! ");
    } 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 < BOARD_SIZE; row++) {
        int col;

        for (col = 0; col < BOARD_SIZE; 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 < BOARD_SIZE && !won; row++) {
        int match = 1; // boolean (0/1)
        int col;

        for (col = 0; col < BOARD_SIZE; 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 BOARD SIZE*/
}

//
// Clears the screen -- uses ANSI terminal control codes
//
void clearScreen(void) {
    const char ESC = 27;

    printf("%c[2J%c[H", ESC, ESC);
}

For this lab, you must finish the ticTacProg.c program. It is recommended that you write it in stages. l. Add the code to display the board. Run it and check it's working 2. Add the code to get the user's move read two ints with rows numbered top to bottom and columns left to right. Run it and check that it's working. 3. Add the code that tests for a vertical three-in-a-row. (Hint: look at the code for horizontal three-in-a-row.) Run it and check that it's working. 4. Add the code that tests for a diagonal three-in-a-row. Run it and check that it's working

Explanation / Answer

//Code for the Method HasWonVertical :

int hasWonVertical(char board[BOARD_SIZE][BOARD_SIZE], char mark) {
int won = 0;
int col;
for (col = 0; col < BOARD_SIZE && !won; col++) {
int match = 1;
int row;
for (row = 0; row < BOARD_SIZE; row++) {
if (board[col][row] != mark) {
match = 0;
}
}
won = match;
}
return won;
}

// Code for the Method HasWonDiagonal :

int hasWonDiagonal(char board[BOARD_SIZE][BOARD_SIZE], char mark) {
int won = 0;
int col, row;
   int match = 1;
for (col = 0, row = 0; row < BOARD_SIZE && col < BOARD_SIZE && !won; col++, row++) {   
if (board[col][row] != mark)
       {
           match = 0;
}
}

   if(match)
       return match;

   row=0; col=0;
   for (row = 0; row < BOARD_SIZE && !won; row++) {
int match = 1;   
for (col = BOARD_SIZE-1; col >=0; col--) {
if (board[row][col] != mark) {
match = 0;
}
}
won = match;
}

   return won;
}

// Code for getting Human Move :

void getHumanMove(char board[BOARD_SIZE][BOARD_SIZE]) {
int row; int col;
   while(1)
   {
       printf(" Please enter Row Number between 0-%d: ", BOARD_SIZE-1);
       scanf("%d", &row);
       printf(" Please enter Column Number between 0-%d : ", BOARD_SIZE-1);
       scanf("%d", &col);

       if(row < 0 || row > BOARD_SIZE-1 || col < 0 || col > BOARD_SIZE-1)
           printf(" Wrong position please try again.");

       else if(board[row][col] == BLANK_SYMBOL)
           break;

       else
       {
           printf(" Wrong position please try again.");
           printBoard(board);
       }
   }
   board[row][col] = HUMAN_SYMBOL;
   printBoard(board);
}

// Code to Print Board :

void printBoard(char board[BOARD_SIZE][BOARD_SIZE])
{
   int i, j, p;
   for(i =0; i < BOARD_SIZE; i++)
   {
       if(i ==0)
       {
           for(p =0; p < BOARD_SIZE; p++)          
               printf(" %d", p);
           printf(" ");
       }
       printf(" ");
       for(p =0; p <= BOARD_SIZE; p++)          
               printf("+---");
      
       printf(" %d", i);
       for(j=0;j < BOARD_SIZE;j++)
       {          
           printf(" | %c ",board[i][j]);
       }
       printf(" ");
   }
   printf(" ");
       for(p =0; p <= BOARD_SIZE; p++)          
               printf("+---");
   printf(" ");
   printf(" ");
}