Guest User

Untitled

a guest
May 23rd, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. static inline void drawBoard(void)
  4. {
  5.     printf("\n\n");
  6.     printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
  7.     printf("---+---+---\n");
  8.     printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
  9.     printf("---+---+---\n");
  10.     printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
  11. }
  12.  
  13. int main(void)
  14. {
  15.   int i = 0;                                   /* Loop counter                         */
  16.   int player = 0;                              /* Player number - 1 or 2               */
  17.   int go = 0;                                  /* Square selection number for turn     */
  18.   int row = 0;                                 /* Row index for a square               */  
  19.   int column = 0;                              /* Column index for a square            */
  20.   int line = 0;                                /* Row or column index in checking loop */
  21.   int winner = 0;                              /* The winning player                   */
  22.   char board[3][3] = {                         /* The board                            */
  23.                        {'1','2','3'},          /* Initial values are reference numbers */
  24.                        {'4','5','6'},          /* used to select a vacant square for   */
  25.                        {'7','8','9'}           /* a turn.                              */
  26.                      };
  27.  
  28.    /* The main game loop. The game continues for up to 9 turns */
  29.    /* As long as there is no winner                            */
  30.    for( i = 0; i<9 && winner==0; i++)
  31.    {
  32.       /* Display the board
  33.     printf("\n\n");
  34.       printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
  35.       printf("---+---+---\n");
  36.       printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
  37.       printf("---+---+---\n");
  38.       printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
  39.      */
  40.     drawBoard();
  41.      
  42.       player = i%2 + 1;                           /* Select player */
  43.  
  44.       /* Get valid player square selection */
  45.       do
  46.       {
  47.          printf("\nPlayer %d, please enter the number of the square "
  48.        "where you want to place your %c: ", player,(player==1)?'X':'O');
  49.          scanf("%d", &go);
  50.  
  51.          row = --go/3;                                 /* Get row index of square      */
  52.          column = go%3;                                /* Get column index of square   */
  53.       }while (go<0 || go>9 || board[row][column]>'9');
  54.  
  55.       board[row][column] = (player == 1) ? 'X' : 'O';        /* Insert player symbol   */
  56.  
  57.       /* Check for a winning line - diagonals first */    
  58.       if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
  59.          (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
  60.         winner = player;
  61.       else
  62.       /* Check rows and columns for a winning line */
  63.         for(line = 0; line <= 2; line ++)
  64.           if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||
  65.              (board[0][line] == board[1][line] && board[0][line] == board[2][line]))
  66.             winner = player;
  67.      
  68.  
  69.    }
  70.    /* Game is over so display the final board
  71.    printf("\n\n");
  72.    printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
  73.    printf("---+---+---\n");
  74.    printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
  75.    printf("---+---+---\n");
  76.    printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
  77.    */
  78.     drawBoard();
  79.  
  80.    /* Display result message */
  81.    if(winner == 0)
  82.       printf("\nHow boring, it is a draw\n");
  83.    else
  84.       printf("\nCongratulations, player %d, YOU ARE THE WINNER!\n", winner);
  85. }
Add Comment
Please, Sign In to add comment