include #include // Function to display the game board void displayBoard(char board[3][3]) {     printf("\n");     printf("  %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);     printf(" ---|---|---\n");     printf("  %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);     printf(" ---|---|---\n");     printf("  %c | %c | %c \n"

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

GENERATE THE CODE BELOW FROM THE UNIX ENVIRONMENT

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to display the game board
void displayBoard(char board[3][3]) {
    printf("\n");
    printf("  %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
    printf(" ---|---|---\n");
    printf("  %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
    printf(" ---|---|---\n");
    printf("  %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
    printf("\n");
}

// Function to check if the game is over (i.e., if someone has won or if there are no more moves)
int isGameOver(char board[3][3]) {
    // Check rows for a win
    for (int i = 0; i < 3; i++) {
        if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
            return 1;
        }
    }

    // Check columns for a win
    for (int i = 0; i < 3; i++) {
        if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
            return 1;
        }
    }

    // Check diagonals for a win
    if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        return 1;
    }
    if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
        return 1;
    }

    // Check if there are any more moves
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == ' ') {
                return 0;
            }
        }
    }

    // If we get here, the game is over with no winner
    return 1;
}

// Function to get the user's move
void getUserMove(char board[3][3], char player) {
    int row, col;
    do {
        printf("Enter row (1-3) for %c: ", player);
        scanf("%d", &row);
        printf("Enter column (1-3) for %c: ", player);
        scanf("%d", &col);
        row--; // adjust to 0-indexed array
        col--; // adjust to 0-indexed array
        if (row < 0 || row > 2 || col < 0 || col > 2) {
            printf("Invalid input! Please enter row and column numbers between 1 and 3.\n");
        } else if (board[row][col] != ' ') {
            printf("That spot is already taken! Please choose a different spot.\n");
        }
    } while (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ');

    board[row][col] = player;
}

int main() {
    // Set up random number generator
    srand(time(NULL));

    // Get player name
    char playerName[50];
    printf("Enter your name: ");
    scanf("%s", playerName);

    // Initialize game board
    char board[3][3] = {
        {' ', ' ', ' '},
        {' ', ' ', ' '},
        {' ', ' ', ' '}
    };

    // Print welcome message and empty board
    printf("Welcome, %s! Let's play Tic Tac Toe.\n", playerName);
    displayBoard(board);

    // Game loop
    char currentPlayer = 'X';
    while (!isGameOver(board)) {
        if (currentPlayer == 'X') {
            getUserMove(board, currentPlayer);
        } else {
            // Computer's turn
            int row, col;
            do {
                row = rand() % 3;
                col = rand() % 3;
            } while (board[row][col] != ' ');
            board[row][col] = currentPlayer;
            printf("Computer placed an %c at row %d, column %d.\n", currentPlayer, row + 1, col + 1);
        }

        // Print updated board
        displayBoard(board);

        // Switch players
        if (currentPlayer == 'X') {
            currentPlayer = 'O';
        } else {
            currentPlayer = 'X';
        }
    }

    // Game over
    if (isGameOver(board)) {
        if (currentPlayer == 'X') {
            printf("Computer wins!\n");
        } else {
            printf("%s wins!\n", playerName);
        }
    } else {
        printf("It's a tie!\n");
    }

    return 0;
}

Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Array
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