We read your photo as:

Write a C program for a Tic-Tac-Toe game.

Computer Science

ScanSolve AI Answer

Here is a short C program for a Tic-Tac-Toe game.

#include <stdio.h> // Required for input/output functions like printf and scanf

// Global 3x3 board to store game state
char board[3][3];

// Function to initialize the board with empty spaces
void initializeBoard() {
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            board[i][j] = ' '; // Each cell is initialized as an empty space
        }
    }
}

// Function to display the current state of the Tic-Tac-Toe board
void displayBoard() {
    printf("\n Tic-Tac-Toe\n\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 any player has won
// Returns 1 if 'X' wins, 2 if 'O' wins, 0 if no winner yet
int checkWin() {
    int i;

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

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

    // Check main diagonal (top-left to bottom-right) for a win
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {
        return (board[0][0] == 'X') ? 1 : 2;
    }

    // Check anti-diagonal (top-right to bottom-left) for a win
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') {
        return (board[0][2] == 'X') ? 1 : 2;
    }

    return 0; // No winner found
}

// Main function where the game logic runs
int main() {
    int player = 1; // Player 1 ('X') or Player 2 ('O')
    int i, row, col;
    char mark; // 'X' or 'O'
    int gameStatus = 0; // 0 = game in progress, 1 = X wins, 2 = O wins, -1 = draw

    initializeBoard(); // Set up the empty board

    // Game loop runs for a maximum of 9 turns (3x3 board) or until a win
    for (i = 0; i < 9 && gameStatus == 0; i++) {
        displayBoard(); // Show the current board state

        // Determine current player and their mark
        player = (i % 2 == 0) ? 1 : 2; // Player 1 for even turns, Player 2 for odd
        mark = (player == 1) ? 'X' : 'O';

        // Prompt current player for their move
        printf("Player %d (%c), enter your move (row[1-3] column[1-3]): ", player, mark);
        scanf("%d %d", &row, &col);

        // Adjust input to 0-indexed array (e.g., user enters 1, program uses 0)
        row--;
        col--;

        // Validate the player's input
        if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') {
            printf("Invalid move. That cell is either out of bounds or already taken. Try again.\n");
            i--; // Decrement 'i' to repeat the current player's turn
            continue; // Skip to the next iteration of the loop
        }

        board[row][col] = mark; // Place the player's mark on the board

        gameStatus = checkWin(); // Check if the current move resulted in a win
    }

    displayBoard(); // Display the final board state

    // Announce the game result
    if (gameStatus == 1) {
        printf("Player 1 (X) wins!\n");
    } else if (gameStatus == 2) {
        printf("Player 2 (O) wins!\n");
    } else {
        printf("It's a draw!\n"); // If no win after 9 turns, it's a draw
    }

    return 0; // Indicate successful program execution
}
Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question