Advertisement
peat_psuwit

Tic-tac-toe in C

Oct 2nd, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     char gameTable[3][3] = {{'_','_','_'},{'_','_','_'},{'_','_','_'}};
  6.     int i, j;
  7.     char currentPlayer;
  8.     int isWin;
  9.     int turns;
  10.  
  11.     for (turns = 0; turns < 9; turns++) {
  12.         currentPlayer = (turns % 2 == 0 ? 'O' : 'X');
  13.  
  14.         printf("Game state: \n");
  15.         for (i=0; i<3; i++) {
  16.             for (j=0; j<3; j++) {
  17.                 printf("%c ", gameTable[i][j]);
  18.             }
  19.             printf("\n");
  20.         }
  21.         printf("\n");
  22.  
  23.         do {
  24.             printf("Enter player %c's placement, (\"row col\" counting from 0): ", currentPlayer);
  25.             scanf(" %d %d", &i, &j);
  26.         } while (i < 0 || i > 2 || j < 0 || j > 2 || gameTable[i][j] != '_');
  27.         printf("\n");
  28.  
  29.         gameTable[i][j] = currentPlayer;
  30.  
  31.         //Checking winning. Could be optimised to check after 5 turns.
  32.         //Check left-to-right
  33.         for (i=0; i<3; i++) {
  34.             if (gameTable[i][0] == '_') continue;
  35.             isWin = 1;
  36.             for (j=1; j<3; j++) {
  37.                 if (gameTable[i][j] != gameTable[i][0]) {
  38.                     isWin = 0;
  39.                     break;
  40.                 }
  41.             }
  42.             if (isWin == 1) break;
  43.         }
  44.  
  45.         if (isWin == 1) break;
  46.  
  47.         //Check Top-to-bottom
  48.           for (j=0; j<3; j++) {
  49.             if (gameTable[0][j] == '_') continue;
  50.             isWin = 1;
  51.             for (i=1; i<3; i++) {
  52.                 if (gameTable[i][j] != gameTable[0][j]) {
  53.                     isWin = 0;
  54.                     break;
  55.                 }
  56.             }
  57.             if (isWin == 1) break;
  58.         }
  59.  
  60.         if (isWin == 1) break;
  61.  
  62.         //Check corners
  63.         for (i=0; i<3; i+=2 /* Skip middle column */) {
  64.             if (gameTable[i][i] == '_') continue;
  65.             isWin = 1;
  66.             for (j=1; j<3; j++) {
  67.                 if (gameTable[j][j] != gameTable[i][i]) {
  68.                     isWin = 0;
  69.                     break;
  70.                 }
  71.             }
  72.             if (isWin == 1) break;
  73.         }
  74.  
  75.         if (isWin == 1) break;
  76.     }
  77.  
  78.     printf("Game state: \n");
  79.     for (i=0; i<3; i++) {
  80.         for (j=0; j<3; j++) {
  81.             printf("%c ", gameTable[i][j]);
  82.         }
  83.         printf("\n");
  84.     }
  85.     printf("\n");
  86.  
  87.     if (isWin == 1) {
  88.         printf("The winner is '%c'\n", currentPlayer);
  89.     }
  90.     else {
  91.         printf("It's a draw. No winner.");
  92.     }
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement