Advertisement
Guest User

tictactoe.c

a guest
Apr 7th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. char b[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
  6. int gameStatus;
  7. int boardSquare;
  8. int turn;
  9. int turnNumber;
  10.  
  11. /*checks the status of the game and sets gameStatus accordingly (1=winner/gameover, 0=game ongoing)*/
  12. void
  13. status()
  14. {
  15.     if((b[0]!=' ' && b[1]!=' ' && b[2]!=' '&& b[0]==b[1] && b[1]==b[2]),
  16.        (b[3]!=' ' && b[4]!=' ' && b[5]!=' '&& b[3]==b[4] && b[4]==b[5]),
  17.        (b[6]!=' ' && b[7]!=' ' && b[8]!=' '&& b[6]==b[7] && b[7]==b[8]),
  18.        (b[0]!=' ' && b[3]!=' ' && b[6]!=' '&& b[0]==b[3] && b[3]==b[6]),
  19.        (b[1]!=' ' && b[4]!=' ' && b[7]!=' '&& b[1]==b[4] && b[4]==b[7]),
  20.        (b[2]!=' ' && b[5]!=' ' && b[8]!=' '&& b[2]==b[5] && b[5]==b[8]),
  21.        (b[0]!=' ' && b[4]!=' ' && b[8]!=' '&& b[0]==b[4] && b[4]==b[8]),
  22.        (b[2]!=' ' && b[4]!=' ' && b[6]!=' '&& b[2]==b[4] && b[4]==b[6])) {
  23.         gameStatus++;
  24.     }
  25. }
  26.  
  27. /*clears the screen and prints the board.*/
  28. void
  29. board()
  30. {
  31.     system("clear");
  32.     printf(" %c | %c | %c \n-----------\n %c | %c | %c \n-----------\n %c | %c | %c \n",\
  33.         b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8]);
  34.     printf("\ngameStatus: %i\n", gameStatus);
  35.     printf("boardSquare: %i\n", boardSquare);
  36.     printf("turn: %i\n", turn);
  37.     printf("turnNumber: %i\n", turnNumber);
  38. }
  39.  
  40. /*takes an input from the player, checks who's turn it is,
  41.  * and sets the corresponding square to the letter of who's turn it is*/
  42. void
  43. boardInput()
  44. {
  45.     printf("\nSelect square: ");
  46.     scanf("%i", &boardSquare);
  47.     if(turn==0) {
  48.         if(b[boardSquare]==' ') {
  49.             b[boardSquare]='X';
  50.         }
  51.     } else {
  52.         if(b[boardSquare]==' ') {
  53.             b[boardSquare]='O';
  54.         }
  55.     }
  56. }
  57.  
  58. /*Progresses the counter of turns and changes who's turn it is */
  59. void
  60. nextTurn()
  61. {
  62.     if(b[boardSquare]=='X'){
  63.         turn=1;
  64.         turnNumber++;
  65.     } else {
  66.         turn=0;
  67.         turnNumber++;
  68.     }
  69. }
  70.  
  71. int
  72. main(void)
  73. {
  74.     while(1) {
  75.         status();
  76.         if(gameStatus==0 && turnNumber==8) {
  77.             system("clear");
  78.             printf("Tie!\n");
  79.             break;
  80.         }
  81.         if(gameStatus==1) {
  82.             system("clear");
  83.             printf("Player wins!");
  84.             break;
  85.         }
  86.         board();
  87.         boardInput();
  88.         nextTurn();
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement