Code below is of tic tac toe game in C. With 2d array. But it is between player 1 and player 2. I want modification in my code so that the game is between player 1 and computer. The computer move will be through rand() function. Please help me out. Code: #include #include   char matrix[3][3] = {{'1', '2', '3'},                      {'4', '5', '6'},                      {'7', '8', '9'}}; int choice, row, column; char mark = 'O'; bool isDraw = false;   void showmatrix() {     int t;       for (t = 0; t < 3; t++) {         printf(" %c | %c | %c ", matrix[t][0],                matrix[t][1], matrix[t][2]);         if (t != 2) printf("\n---|---|---\n");     }     printf("\n");     printf("\n");   }   void user_turn() {     if (mark == 'O') {         printf("User turn : ");     } else if (mark == 'X') {         printf("Computer turn : ");     }     scanf("%d", &choice);       switch (choice) {         case 1:             row = 0;             column = 0;             break;         case 2:             row = 0;             column = 1;             break;         case 3:             row = 0;             column = 2;             break;         case 4:             row = 1;             column = 0;             break;         case 5:             row = 1;             column = 1;             break;         case 6:             row = 1;             column = 2;             break;         case 7:             row = 2;             column = 0;             break;         case 8:             row = 2;             column = 1;             break;         case 9:             row = 2;             column = 2;             break;         default:             printf("Invalid Move");     }       if (mark == 'X' && matrix[row][column] != 'X' && matrix[row][column] != 'O') {         matrix[row][column] = 'X';         mark = 'O';     } else if (mark == 'O' && matrix[row][column] != 'X' && matrix[row][column] != 'O') {         matrix[row][column] = 'O';         mark = 'X';     } else {         user_turn();     }     showmatrix(); }   bool gameover() {     for (int i = 0; i < 3; i++) { /*checking rows and columns*/         if (matrix[i][0] == matrix[i][1] && matrix[i][0] == matrix[i][2] ||             matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i])             return false;     }       for (int i = 0; i < 3; i++) { /*checking if game is drawn*/         for (int j = 0; j < 3; j++) {             if (matrix[i][j] != 'X' && matrix[i][j] != 'O')                 return true;         }     }       if (matrix[0][0] == matrix[1][1] && matrix[0][0] == matrix[2][2] || /*checking diagonals*/         matrix[0][2] == matrix[1][1] && matrix[0][2] == matrix[2][0]){         return false; }     isDraw = true;     return false; }   int main() {     while (gameover()) {         showmatrix();         user_turn();         gameover();     }     if (mark == 'X' && !isDraw) {         printf("User wins the game");     } else if (mark == 'O' && !isDraw) {         printf("Computer wins the game");     } else         printf("The game ended in a draw");       return 0;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Code below is of tic tac toe game in C. With 2d array. But it is between player 1 and player 2. I want modification in my code so that the game is between player 1 and computer. The computer move will be through rand() function. Please help me out.

Code:

#include<stdio.h>

#include <stdbool.h>

 

char matrix[3][3] = {{'1', '2', '3'},

                     {'4', '5', '6'},

                     {'7', '8', '9'}};

int choice, row, column;

char mark = 'O';

bool isDraw = false;

 

void showmatrix() {

    int t;

 

    for (t = 0; t < 3; t++) {

        printf(" %c | %c | %c ", matrix[t][0],

               matrix[t][1], matrix[t][2]);

        if (t != 2) printf("\n---|---|---\n");

    }

    printf("\n");

    printf("\n");

 

}

 

void user_turn() {

    if (mark == 'O') {

        printf("User turn : ");

    } else if (mark == 'X') {

        printf("Computer turn : ");

    }

    scanf("%d", &choice);

 

    switch (choice) {

        case 1:

            row = 0;

            column = 0;

            break;

        case 2:

            row = 0;

            column = 1;

            break;

        case 3:

            row = 0;

            column = 2;

            break;

        case 4:

            row = 1;

            column = 0;

            break;

        case 5:

            row = 1;

            column = 1;

            break;

        case 6:

            row = 1;

            column = 2;

            break;

        case 7:

            row = 2;

            column = 0;

            break;

        case 8:

            row = 2;

            column = 1;

            break;

        case 9:

            row = 2;

            column = 2;

            break;

        default:

            printf("Invalid Move");

    }

 

    if (mark == 'X' && matrix[row][column] != 'X' && matrix[row][column] != 'O') {

        matrix[row][column] = 'X';

        mark = 'O';

    } else if (mark == 'O' && matrix[row][column] != 'X' && matrix[row][column] != 'O') {

        matrix[row][column] = 'O';

        mark = 'X';

    } else {

        user_turn();

    }

    showmatrix();

}

 

bool gameover() {

    for (int i = 0; i < 3; i++) { /*checking rows and columns*/

        if (matrix[i][0] == matrix[i][1] && matrix[i][0] == matrix[i][2] ||

            matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i])

            return false;

    }

 

    for (int i = 0; i < 3; i++) { /*checking if game is drawn*/

        for (int j = 0; j < 3; j++) {

            if (matrix[i][j] != 'X' && matrix[i][j] != 'O')

                return true;

        }

    }

 

    if (matrix[0][0] == matrix[1][1] && matrix[0][0] == matrix[2][2] || /*checking diagonals*/

        matrix[0][2] == matrix[1][1] && matrix[0][2] == matrix[2][0]){

        return false;

}

    isDraw = true;

    return false;

}

 

int main() {

    while (gameover()) {

        showmatrix();

        user_turn();

        gameover();

    }

    if (mark == 'X' && !isDraw) {

        printf("User wins the game");

    } else if (mark == 'O' && !isDraw) {

        printf("Computer wins the game");

    } else

        printf("The game ended in a draw");

 

    return 0;

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education