This blog is under construction

Tuesday 2 April 2013

Tic-Tac-Toe game in C

Tic-Tac-Toe is a two player game where each player uses a signature.  The player who successfully places three respective signatures in a vertical, horizontal or diagonal row is the winner.

Step 1: Use 3 X 3 matrix to get 9 cell tic tac toe board.
Step 2: Signature 'X' is used for player 1 and 'Y' for player 2.  And the players play
           alternatively.
Step 3: Get index as input from the players.  And the index range is 1-9. i.e) from 
           cell 1 to cell 9.
Step 4: After each move, check whether that player has placed three respective
           signatures in vertical, horizontal or diagonal row.  If yes, declare that
           particular player as winner.
Step 5: If no, give chance for other player.
Step 6: If all the cells in the Tic-Tac-Toe board got filled, then the game is drawn.


C Program for Tic-Tac-Toe game:



  #include <stdio.h>
  int main() {
        int winner = 0, count = 0;
        int data[9], index, sign, player, flag, i, k, j;

        for (i = 0; i < 9; i++)
                data[i] = ' ';

        while (count < 9) {
                flag = 0;
                system("clear");
                printf("\n\n");
                printf("\t\t\t  %c | %c  | %c  \n", data[0], data[1], data[2]);
                printf("\t\t\t----+----+----\n");
                printf("\t\t\t  %c | %c  | %c  \n", data[3], data[4], data[5]);
                printf("\t\t\t----+----+---\n");
                printf("\t\t\t  %c | %c  | %c  \n", data[6], data[7], data[8]);
                if (count % 2 == 0) {
                        sign = 'X';
                        player = 1;
                } else {
                        sign = 'Y';
                        player = 2;
                }
                printf("Move for player%d(1-9):", player);
                scanf("%d", &index);
                if (index < 1 || index > 9) {
                        printf("Allowed index is 1 to 9!!\n");
                        continue;
                }
                if (data[index - 1] == 'X' || data[index - 1] == 'Y') {
                        printf("Position Already occupied!!\n");
                        sleep(1);
                        continue;
                }
                data[index - 1] = sign;
                count++;

                for (i = 0; i < 9; i++) {
                        if (i % 3 == 0)
                                flag = 0;

                        if (data[i] == sign)
                                flag++;

                        if (flag == 3) {
                                winner = 1;
                                goto win;
                        }
                }

                flag = 0;
                for (i = 0; i < 3; i++) {
                        for (k = i; k <= i + 6; k = k + 3) {
                                if (data[k] == sign)
                                        flag++;
                        }
                        if (flag == 3) {
                                winner = 1;
                                goto win;
                        }
                        flag = 0;
                }
                if ((data[0] == sign && data[4] == sign && data[8] == sign) ||
                        (data[2] == sign && data[4] == sign && data[6] ==  sign)) {
                        winner = 1;
                        goto win;
                }
        }
  win:
        system("clear");
        printf("\n\n");
        printf("\t\t\t  %c | %c  | %c  \n", data[0], data[1], data[2]);
        printf("\t\t\t----+----+----\n");
        printf("\t\t\t  %c | %c  | %c  \n", data[3], data[4], data[5]);
        printf("\t\t\t----+----+---\n");
        printf("\t\t\t  %c | %c  | %c  \n\n", data[6], data[7], data[8]);
        if (winner) {
                printf("Player %d is the winner. Congrats!!\n", player);
        } else {
                printf("Match draw.. Best of luck for both\n");
        }
        return 0;
  }



  Output: (Tic-Tac-Toe game)

                        X    |   Y   | X  
                       -------+-------+-------
                       X    |   Y   |    
                     -------+-------+-------
                             |   Y   |    

  Player 2 is the winner. Congrats!!



14 comments:

  1. This is TicTacToe...otherwise known as Noughts and Crosses and should be played with the markers "X" & "O", not "Y"!
    To fix this change..sign = 'Y'; to sign = 'O'; and (data[index - 1] == 'X' || data[index - 1] == 'Y') to (data[index - 1] == 'X' || data[index - 1] == 'O')...Thanks for the code and happy tictactoe playing :-)

    ReplyDelete
  2. CAN U PLEASE EXPLAIN THIS CODE BREIFLY

    ReplyDelete
    Replies
    1. hey bro you can also visit this link
      https://codewithvibhu.blogspot.com/2019/11/Tic-Tac-Toe-Game-program-in-C.html?m=1

      Delete
  3. can u send me simple code of tic tac toe human vs computer in c only..i cant find it anywhere..

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This program accepts the choice 10.

    ReplyDelete
  6. This program accepts 10 and also other characters from atoz but characters not assigned, please look into it.

    ReplyDelete
  7. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

    Data Science Interview Questions

    Angular JS Interview Questions

    Big Data Training in Velachery

    AWS Certified Developer

    Best Resource of Learning Devops

    Blueprism Interview Questions

    Automation Anywhere Interview Questions

    ReplyDelete
  8. Issues in different aspect are being observed associated with Gemini. Among the others, Gemini Generate New BTC address has been found one. Users may immediately manage this issue by taking the help of Gemini techies. You can reach the well-versed experts who are available 24/7 to offer pertinent guidance and remedies whenever you need it. By dialing a Gemini support number, you can share your issue with the experts directly and get instant Gemini Support Number remedies. The advisors deliver effortless and accessible remedies so that users don’t face any trouble while implementing them.

    ReplyDelete
  9. Tic-Tac-Toe game in simple C
    https://space-inst.blogspot.com/p/c-program-tic-tac-toe-game.html?m=1

    Features:

    1. Two player game.
    2. Score Card 'Save and Display'
    3. Compatible with any C Compiler
    4. 3 rounds per Game. Final score display after completion of 3 rounds and also gives scores per round.
    5. Menu Display

    ReplyDelete
  10. Well Explained. I have made tic tac toe game in java visit Tic Tac Toe Game

    ReplyDelete