Thread: Tic Tac Toe Program (please help)

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    6

    Tic Tac Toe Program (please help)

    Here is my code:
    Code:
    #include <stdio.h>
    
    
    void printBoard();
    void checkDiagonal();
    void checkHrzVrt();
    
    
    int main(){
        int i = 0;                                   /* Loop counter                         */
        int player = 0;                              /* Player number - 1 or 2               */
        int go = 0;                                  /* Square selection number for turn     */
        int row = 0;                                 /* Row index for a square               */  
        int column = 0;                              /* Column index for a square            */
        int line = 0;                                /* Row or column index in checking loop */
        int winner = 0;                              /* The winning player                   */
        char board[3][3] = {{'1','2','3'},           /* Initial values are reference numbers */
                            {'4','5','6'},           /* used to select a vacant square for   */
                            {'7','8','9'}};          /* a turn.                              */
    
    
        /* The main game loop. The game continues for up to 9 turns */
        /* as long as there is no winner.                           */
        for( i = 0; i < 9 && winner == 0; i++){
            /* Display the board */
            printBoard();
            /* Select player */
            player = i%2 + 1;
            
            /* Get valid player square selection */
            do{
                printf("\n[Player %d] Where do you want to place your '%c'? ", player,(player==1)?'X':'O');
                scanf("%d", &go);
                
                row = --go/3;                                 /* Get row index of square      */
                column = go%3;                                /* Get column index of square   */
            }
            while(go < 0 || go > 9 || board[row][column] > '9');
            
                board[row][column] = (player == 1) ? 'X' : 'O';     /* Insert player symbol   */
            
                /* Check for a winning line */
                checkDiagonal();
            else
                /* Check rows and columns for a winning line */
                checkHrzVrt();
        }
        /* Game over. Display the final board. */
        printBoard();
    
    
        /* Display result message */
        if(winner == 0)
           printf("\nDRAW\n");
        else
           printf("\nCongratulations, player %d. You win!\n", winner);
    }
    
    
    void printBoard(){
        printf("\n");
        printf("|%c|%c|%c|\n", board[0][0], board[0][1], board[0][2]);
        printf("|%c|%c|%c|\n", board[1][0], board[1][1], board[1][2]);
        printf("|%c|%c|%c|\n", board[2][0], board[2][1], board[2][2]);
    }
    
    
    void checkDiagonal(){
        if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
        (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
            winner = player;
    }
    
    
    void checkHrzVrt(){
        for(line = 0; line <= 2; line ++)
            if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||
              (board[0][line] == board[1][line] && board[0][line] == board[2][line]))
                winner = player;
    }
    I would like the same code but without the ternary conditional operator at line 40. Please reply if you know how to do this.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Change it to an if with an else

    Edit: it looks awfully like the code in the book "Beginning C"
    Last edited by Hodor; 12-13-2015 at 11:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  2. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM