Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to write the Tic-Tac-Toe game in C

1 Answer

0 votes
#include <stdio.h>

/*
    A simple Tic‑Tac‑Toe game written in C.

    Highlights:
      - The board is represented as a 1D array of 9 chars.
      - Functions handle printing, move validation, win checking,
        and turn progression.
      - The game loop is clear and easy to follow.
      - Comments explain the reasoning behind each part of the program.
*/

// Print the board in a 3×3 layout
void printBoard(const char board[9]) {
    printf("\n");
    for (int i = 0; i < 9; i++) {
        printf("%c", board[i]);
        if ((i + 1) % 3 == 0)
            printf("\n");
        else
            printf(" | ");
    }
    printf("\n");
}

// Check if a player has won
int checkWin(const char board[9], char player) {
    // All winning combinations
    int wins[8][3] = {
        {0,1,2}, {3,4,5}, {6,7,8}, // rows
        {0,3,6}, {1,4,7}, {2,5,8}, // columns
        {0,4,8}, {2,4,6}           // diagonals
    };

    for (int i = 0; i < 8; i++) {
        int a = wins[i][0];
        int b = wins[i][1];
        int c = wins[i][2];

        if (board[a] == player &&
            board[b] == player &&
            board[c] == player)
            return 1;
    }

    return 0;
}

// Check if the board is full (draw)
int boardFull(const char board[9]) {
    for (int i = 0; i < 9; i++) {
        if (board[i] == ' ')
            return 0;
    }

    return 1;
}

// Attempt to place a move; return 1 if successful
int placeMove(char board[9], int pos, char player) {
    if (pos < 0 || pos >= 9)
        return 0;

    if (board[pos] != ' ')
        return 0;

    board[pos] = player;

    return 1;
}

int main() {
    char board[9];

    // Initialize board with spaces
    for (int i = 0; i < 9; i++)
        board[i] = ' ';

    char currentPlayer = 'X';

    printf("Tic‑Tac‑Toe\n");
    printBoard(board);

    while (1) {
        printf("Player %c, enter position (0‑8): ", currentPlayer);

        int pos;
        scanf("%d", &pos);

        // Try to place the move
        if (!placeMove(board, pos, currentPlayer)) {
            printf("Invalid move. Try again.\n");
            continue;
        }

        printBoard(board);

        // Check win
        if (checkWin(board, currentPlayer)) {
            printf("Player %c wins!\n", currentPlayer);
            break;
        }

        // Check draw
        if (boardFull(board)) {
            printf("It's a draw!\n");
            break;
        }

        // Switch player
        currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
    }

    return 0;
}


/*
run: 1

Tic‑Tac‑Toe

  |   |  
  |   |  
  |   |  

Player X, enter position (0‑8): 0

X |   |  
  |   |  
  |   |  

Player O, enter position (0‑8): 4

X |   |  
  | O |  
  |   |  

Player X, enter position (0‑8): 8

X |   |  
  | O |  
  |   | X

Player O, enter position (0‑8): 7

X |   |  
  | O |  
  | O | X

Player X, enter position (0‑8): 1

X | X |  
  | O |  
  | O | X

Player O, enter position (0‑8): 2

X | X | O
  | O |  
  | O | X

Player X, enter position (0‑8): 6

X | X | O
  | O |  
X | O | X

Player O, enter position (0‑8): 3

X | X | O
O | O |  
X | O | X

Player X, enter position (0‑8): 5

X | X | O
O | O | X
X | O | X

It's a draw!

*/


/*
run: 2

Tic‑Tac‑Toe

  |   |  
  |   |  
  |   |  

Player X, enter position (0‑8): 9
Invalid move. Try again.
Player X, enter position (0‑8): 0

X |   |  
  |   |  
  |   |  

Player O, enter position (0‑8): 1

X | O |  
  |   |  
  |   |  

Player X, enter position (0‑8): 0
Invalid move. Try again.
Player X, enter position (0‑8): 8

X | O |  
  |   |  
  |   | X

Player O, enter position (0‑8): 6

X | O |  
  |   |  
O |   | X

Player X, enter position (0‑8): 4

X | O |  
  | X |  
O |   | X

Player X wins!

*/

 



answered 5 days ago by avibootz
edited 5 days ago by avibootz
...