JHJiban

TicTacToe Game

Sep 4th, 2023
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. /*
  2. Course Title: Algorithm Lab
  3. Course Code: CSE215
  4. Group Name: Semicolon;
  5. Member #01
  6. Jahid Hasan Jiban
  7. ID: 192-15-13117
  8. Member #02
  9. Sahadath Hossen Saykot
  10. ID: 192-15-13106
  11. Member #03
  12. Mainul Islam
  13. ID: 192-15-13053
  14. Aparna Debnath
  15. ID: 192-15-13030
  16. Mushrat Jahan
  17. ID: 192-15-13029
  18. */
  19.  
  20. #include<stdio.h>
  21. #include<conio.h>
  22. #include<windows.h>
  23. #include<stdlib.h>
  24. #include<MMsystem.h>
  25.  
  26. char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
  27. int checkWin();
  28. void drawBoard();
  29.  
  30. int main(){
  31.     int player = 1, i,choice;
  32.     char mark;
  33.     do {
  34.         drawBoard();
  35.         player = (player%2) ? 1 : 2;
  36.         printf("Player %d, enter the choice: ",player);
  37.         scanf("%d",&choice);
  38.         mark = (player == 1) ? 'X' : 'O';
  39.         if(choice == 1 && square[1] == '1')
  40.             square[1] = mark;
  41.         else if(choice == 2 && square[2] == '2')
  42.             square[2] = mark;
  43.         else if(choice == 3 && square[3] == '3')
  44.             square[3] = mark;
  45.         else if(choice == 4 && square[4] == '4')
  46.             square[4] = mark;
  47.         else if(choice == 5 && square[5] == '5')
  48.             square[5] = mark;
  49.         else if(choice == 6 && square[6] == '6')
  50.             square[6] = mark;
  51.         else if(choice == 7 && square[7] == '7')
  52.             square[7] = mark;
  53.         else if(choice == 8 && square[8] == '8')
  54.             square[8] = mark;
  55.         else if(choice == 9 && square[9] == '9')
  56.             square[9] = mark;
  57.  
  58.         else {
  59.                 printf("Invalid Option!");
  60.                 player--;
  61.                 getch();
  62.             }
  63.             i = checkWin();
  64.             player++;
  65.  
  66.     }while(i == -1);
  67.  
  68.     drawBoard();
  69.     if(i==1){
  70.         printf("==>Player %d won!",--player);
  71.     }
  72.     else {
  73.         printf("==>Game draw!");
  74.     }
  75.         getch();
  76.         return 0;
  77. }
  78. int checkWin(){
  79.     if(square[1] == square[2] && square[2] == square[3])
  80.         return 1;
  81.     else if (square[4] == square[5] && square[5] == square[6])
  82.         return 1;
  83.     else if(square[7] == square[8] && square[8] == square[9])
  84.         return 1;
  85.     else if(square[1] == square[4] && square[4] == square[7])
  86.         return 1;
  87.     else if(square[2] == square[5] && square[5] == square[8])
  88.         return 1;
  89.     else if(square[3] == square[6] && square[6] == square[9])
  90.         return 1;
  91.     else if(square[1] == square[5] && square[5] == square[9])
  92.         return 1;
  93.     else if(square[3] == square[5] && square[5] == square[7])
  94.         return 1;
  95.     else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] !='4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
  96.         return 0;
  97.     else
  98.         return -1;
  99. }
  100.  
  101. void drawBoard(){
  102.     system("cls");
  103.     printf("\n\n\t Tic Tac Toe \n\n");
  104.     printf("Player1 (X) - Player2 (O) \n\n\n");
  105.     printf("     |     |     \n");
  106.     printf("  %c  |  %c  |  %c  \n",square[1],square[2],square[3]);
  107.     printf("_____|_____|_____\n");
  108.     printf("     |     |     \n");
  109.     printf("  %c  |  %c  |  %c  \n",square[4],square[5],square[6]);
  110.     printf("_____|_____|_____\n");
  111.     printf("     |     |     \n");
  112.     printf("  %c  |  %c  |  %c  \n",square[7],square[8],square[9]);
  113.     printf("     |     |     \n");
  114. }
Advertisement
Add Comment
Please, Sign In to add comment