Git Product home page Git Product logo

gameoftictactoe's Introduction

gameoftictactoe

#include <stdio.h>

// Function to print the Tic Tac Toe board void printBoard(char board[3][3]) { printf("-------------\n"); for (int i = 0; i < 3; i++) { printf("| %c | %c | %c |\n", board[i][0], board[i][1], board[i][2]); printf("-------------\n"); } }

// Function to check if a player has won int checkWin(char board[3][3], char player) { // Check rows and columns for (int i = 0; i < 3; i++) { if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { return 1; // Player has won } }

// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
    (board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
    return 1; // Player has won
}

return 0; // No winner yet

}

// Function to check if the board is full int isBoardFull(char board[3][3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == ' ') { return 0; // Board is not full } } } return 1; // Board is full }

int main() { char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}; int row, col; char currentPlayer = 'X';

printf("Tic Tac Toe Game\n");

do {
    // Print the current state of the board
    printBoard(board);

    // Get the player's move
    printf("Player %c, enter your move (row and column): ", currentPlayer);
    scanf("%d %d", &row, &col);

    // Check if the chosen cell is valid
    if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != ' ') {
        printf("Invalid move! Try again.\n");
        continue;
    }

    // Update the board with the player's move
    board[row][col] = currentPlayer;

    // Check if the current player has won
    if (checkWin(board, currentPlayer)) {
        // Print the final state of the board
        printBoard(board);
        printf("Player %c wins!\n", currentPlayer);
        break;
    }

    // Check if the board is full (a tie)
    if (isBoardFull(board)) {
        // Print the final state of the board
        printBoard(board);
        printf("It's a tie!\n");
        break;
    }

    // Switch to the other player
    currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';

} while (1); // Infinite loop for the game

return 0;

}

gameoftictactoe's People

Contributors

dee7anjali avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.