AnonymousEng

Tic Tac Toe Game [2 Players]

Sep 19th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.92 KB | None | 0 0
  1. /*
  2.     It's a simple Tic Tac Toe game. 2 Players are playing.
  3.     My algorithms is so easy, I have made a global (array) called board and its elements are zeros
  4.  
  5.     After that if the player1 plays it will set this element to be (1)
  6.     and if player 2 plays element will be (100)
  7.  
  8.     and the who_winner algorithm:
  9.         will depend on checking the sum of all (horizontal lines, vertical lines , diagonal lines)
  10.         so if the sum equals the (3) then player 1 is the (winner=2)
  11.         if the sum = 300 then player 2 is the winner (winner = 3)
  12.         and if nothing match return winner = 0
  13.  
  14. */
  15.  
  16. #define YES 1
  17. #define NO 0
  18.  
  19. #include <stdio.h>
  20.  
  21.  
  22. int board[3][3] = {{0,0,0},{0,0,0},{0,0,0}};    //initialize the board with zeros
  23. int game_end_f = NO;                            // variable used as a flag
  24. int winner;                                     // used in the who_winner() function
  25.  
  26. void draw_board(int myboard[3][3]);             // used for printing the current board
  27. void player_x();                                // used to scan coordinates and put the proper value for player 1
  28. void player_y();                                // the same but for player2
  29. void check_game();                              // check if the game ends and then check (WIN,LOSE,TIE)
  30. int who_winner(int array[3][3]);                // check who is the winner
  31. int no_places(int array[3][3]);                 // function used to check if there any left empty cells or not
  32. void reset();                                   // reset all the board elements to be zeros
  33.  
  34. int x_cord,y_cord;                              // variables for coordinates
  35. char playerx_wins=0;                            // number of wins
  36. char playery_wins=0;
  37.  
  38. int play_again=0;
  39.  
  40.  
  41. int main()
  42. {
  43.     start:                                      //this is a label to be used by a goto statement
  44.         system("cls");                          // clears the screen
  45.         //system("clear");                      // for linux :)
  46.         reset();                                // reset all
  47.         draw_board(board);                      // draw the board for the first time
  48.  
  49.  
  50.         // while the game is not ended , continue playing :)
  51.         while(game_end_f == NO)
  52.         {
  53.             // if no one is winning continue
  54.             if(winner == 0)
  55.                 player_x();
  56.             else
  57.                 break;
  58.  
  59.  
  60.             draw_board(board);
  61.  
  62.             // check now the board
  63.             check_game();
  64.  
  65.             // if there's no winner till now and there are places left ,continue player 2 turn
  66.             if (winner == 0 && !no_places(board))
  67.             {
  68.  
  69.                 player_y();
  70.                 draw_board(board);
  71.                 check_game();
  72.  
  73.             }
  74.  
  75.             else
  76.                 break;
  77.  
  78.  
  79.         }
  80.  
  81.  
  82.         // Do you want to play again ?
  83.         printf("\n\n\tPLAYER1 Vs PLAYER2 : %d - %d",playerx_wins,playery_wins);
  84.         printf("\n\nDo you want to play again [yes = 1] [no = anything else] : ");
  85.         scanf("%d",&play_again);
  86.         if (play_again == 1)
  87.             goto start;
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
  93. // this function checks the sate of the game (TIE,WIN,LOSE)
  94. void check_game()
  95. {
  96.     if(no_places(board))
  97.     {
  98.         // TIE or WINNER
  99.         game_end_f = YES;
  100.         if ((who_winner(board) == 0))
  101.             printf("TIE!!\n");
  102.         else
  103.             who_winner(board);
  104.  
  105.     }
  106.     else
  107.     {
  108.         game_end_f = NO;
  109.     }
  110.     who_winner(board);
  111.  
  112.     // print the result if game ended :) winner 2 >> PLAYER 1           winner 3 >> PLAYER2
  113.         if(winner == 2 )
  114.         {
  115.             printf("WIN: PLAYER 1 (X)\n");
  116.             playerx_wins++;
  117.         }
  118.  
  119.         else if(winner == 3)
  120.         {
  121.             printf("WIN: PLAYER 2 (O)\n");
  122.             playery_wins++;
  123.         }
  124. }
  125.  
  126.  
  127.  
  128. int who_winner(int array[3][3])
  129. {
  130.     int i;
  131.     int sum;
  132.     winner = 0;
  133.     // check the horozintal
  134.     for(i=0; i<3 ; ++i)
  135.     {
  136.         sum = array[i][0] + array [i][1] + array[i][2];
  137.         if (sum == 3)
  138.         {
  139.             winner = 2;
  140.             return 2;
  141.         }
  142.         else if (sum == 300)
  143.         {
  144.             winner = 3;
  145.             return 3;
  146.         }
  147.         else
  148.         {
  149.             winner = 0;
  150.         }
  151.     }
  152.  
  153.  
  154.     // check the vertical
  155.     for(i=0; i<3 ; ++i)
  156.     {
  157.         sum = array[0][i] + array [1][i] + array[2][i];
  158.         if (sum == 3)
  159.         {
  160.             winner = 2;
  161.             return 2;
  162.         }
  163.         else if (sum == 300)
  164.         {
  165.             winner = 3;
  166.             return 3;
  167.         }
  168.         else
  169.         {
  170.             winner = 0;
  171.         }
  172.     }
  173.  
  174.     // check the diagonal
  175.         sum = array[0][0] + array [1][1] + array[2][2];
  176.         if (sum == 3)
  177.         {
  178.             winner = 2;
  179.             return 2;
  180.         }
  181.         else if (sum == 300)
  182.         {
  183.             winner = 3;
  184.             return 3;
  185.         }
  186.         else
  187.         {
  188.             winner = 0;
  189.         }
  190.  
  191.         sum = array[0][2] + array[1][1] + array[2][0];
  192.         if (sum == 3)
  193.         {
  194.             winner = 2;
  195.             return 2;
  196.         }
  197.         else if (sum == 300)
  198.         {
  199.             winner = 3;
  200.             return 3;
  201.         }
  202.         else
  203.         {
  204.             winner = 0;
  205.         }
  206.  
  207.     return 0;
  208. }
  209.  
  210.  
  211. // draw board function
  212. void draw_board(int myboard[3][3])
  213. {
  214.  
  215.     int i,j;
  216.     system("cls");
  217.     printf("TIC TAC TOE GAME - By Islam Negm \n\n");
  218.     printf("-------------\n");
  219.     for (i=0;i<3;i++)
  220.     {
  221.         for(j=0;j<3;j++)
  222.         {
  223.             if (myboard[i][j] == 0)
  224.                 printf("|   ");
  225.             else if(myboard[i][j] == 1)
  226.                 printf("| x ");
  227.             else if(myboard[i][j] == 100)
  228.                 printf("| o ");
  229.         }
  230.         printf("|\n");
  231.         printf("-------------\n");
  232.     }
  233. }
  234.  
  235. void player_x()
  236. {
  237.     // scan coordinates , note that row=1 means the [0] in the array :) , the users are not programmers :D
  238.     printf("PLAYER1 (X):\nEnter the row:");
  239.     scanf("%d",&x_cord);
  240.     printf("Enter the column:");
  241.     scanf("%d",&y_cord);
  242.  
  243.     // take some decisions before ( if cell is not empty or if the coordinates is not right ) then send message to player
  244.     if( (board[x_cord - 1][y_cord - 1] == 0) && x_cord<=3 && x_cord>=1 && y_cord<=3 && y_cord>=1)
  245.         board[x_cord - 1][y_cord - 1]=1; //
  246.     else
  247.     {
  248.         printf("Wrong Move! Enter the correct coordinates.\n");
  249.         player_x();
  250.     }
  251. }
  252.  
  253.  
  254. void player_y()
  255. {
  256.     // scan coordinates , note that row=1 means the [0] in the array :) , the users are not programmers :D
  257.     printf("PLAYER2 (O):\nEnter the row:");
  258.     scanf("%d",&x_cord);
  259.     printf("Enter the column:");
  260.     scanf("%d",&y_cord);
  261.  
  262.     // take some decisions before ( if cell is not empty or if the coordinates is not right ) then send message to player
  263.     if((board[x_cord - 1][y_cord - 1] == 0) && x_cord<=3 && x_cord>=1 && y_cord<=3 && y_cord>=1)
  264.         board[x_cord - 1][y_cord - 1]=100;
  265.     else
  266.     {
  267.         printf("Wrong Move! Enter the correct coordinates.\n");
  268.         player_y();
  269.     }
  270. }
  271.  
  272. // check if no places lift ( return 0 >>> if still empty places ) (return 1 >> if there aren't any places )
  273. int no_places(int array[3][3])
  274. {
  275.     int i,j;
  276.     for (i=0; i<3; ++i)
  277.     {
  278.         for (j=0; j<3; ++j)
  279.         {
  280.             if (array[i][j]==0)
  281.                 return 0;
  282.         }
  283.     }
  284.     return 1;
  285. }
  286.  
  287. void reset()
  288. {
  289.     int i,j;
  290.  
  291.     //resetting the gaming variables
  292.     game_end_f = NO;
  293.     winner  = 0 ;
  294.  
  295.     //resetting the board array
  296.     for (i=0; i<3; i++)
  297.     {
  298.         for(j=0; j<3; j++)
  299.         {
  300.             board[i][j] = 0;
  301.         }
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment