Thread: Tic Tac Toe Game

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    Tic Tac Toe Game

    Hi,
    I have the most of the program working. Problem I'm having right now is that the game ends after 3 turns, rather than checking if player has gotten 3 of his/her "X" or "O" diagonally/horizontal/vertical. Function which checks if given player wins is called "bool checkwinner(char check)". Any help would be appreciated. Thank you for your time.

    Code:
    #include <stdio.h>      
    #include <stdlib.h>      
    #include <stdbool.h>
    
    char board[] = " 1 | 2 | 3\n-----------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9";
                 // Used to display the current state of the board.
    int checkboard[] ={1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7};
                 // Used to figure out winning combinations: three blocks in a row
                 // vertically, horizontally or diagonally
                 // Challenge: Figure out the logic in this!
    bool spaces[10];    // A record of which spaces (1..9) are free(true) or taken (false)
                        // Element 0 not used because the blocks are numbers 1..9
    int nSpaces;        // A count of the number of free spaces left.
    
    bool moveHandler(int player,int n);
    bool checkwinner(char check);
    int playerinput(int player);
    
    int k=0, j=0;
    
    int main(void)
    {
     int player;        // Identifier of current player
     int key;           // Key entered by user
     int status;
     bool terminationCondition;        // Placeholder - to be replaced by you.
     player = rand() %2 +1;    // Randomly choose an initial player.
     // Initial all spaces to be free
    
    
    
     terminationCondition = false;
     while (status != 1)     // To be replaced by your logic for terminating a round
     { 
    
       // Alternate between the two players
       
       if (player == 1) {
                  player = 2;
       } else {
              player = 1;
       }
       
       // Print the current board
       printf ("\n\n %s", board);
       
       // Prompt the user for their next input
       key = playerinput(player);
       
       // Make the move keyed in by the user
       status = moveHandler(player,key);
       
       printf("%d\n",status);
       
       // Decide if you have a winner or if the game is over.
     
     
     
       
      }
      system ("pause");
      return 0;
    }
    
    /* Implements the next move in the tic-tac-toe game
     * marking the square as taken
     *
     * @param  player     Identifier of the current player
     * @param  space      The block selected (input) by the user
     * @return Return true if the move is valid, false if the space is already taken
     */
     
    bool moveHandler(int player,int space)
    {
      char check; 
      int i;
      // set X for player one, or O for player 2
      if (player == 2) {
                 check = 'O';
      } else {
             check = 'X';
      }
      
      
      
      // Check if block(or space) is already used. If so, re-prompt the user
      // for another selection.
      while (spaces[space] == true)
      {
        printf ("\nInvalid Space");
        space=playerinput(player);
      }
      // Remove block from spaces array: Make the block(or space) as taken.
      // Reduce the number of free-spaces by one.
      spaces[space]=true;
      //nSpaces--;
      
      // Update the display of the board by replacing the block with check mark in board
      //  eg. Replace the number 9 with 'x'
      //  eg. Replace the number 6 with 'o'
      //  To convert a character into its integer equivalent use:
      //     char aChar;         
      //     int intValue = aChar - '0';
      //  If aChar was character '9', then intValue will be the number 9.
        
     
        char chSpace = (char)(((int)'0')+space); //I didn't understand the above method so I used my own method, it converts int to char
        
        for (i==0;i<60;i++) {
            if (chSpace == board[i]) {
                        board[i] = check;
            }
        }
    
      
     
    
    
    
      // Replace space with check mark in checkboard array
      
      if (player == 1) {
      checkboard[space] = 11; // this means this place is taken by player 1
      }
      
      if (player == 2) {
         checkboard[space] = 22; //this means this place is taken by player 2
      }
      // This will help us figure out if there is 3-in-a-row ... if we've won yet.
    
    
    
    
    
    
      // Run the checkwinner function
      return checkwinner(check);
      
        
    }
    
    /* Checkwinner function
     *
     * This function checks whether or not a player has a winning
     * combination based on the checkboard array.
     * @param      check    The character for the current player's turn (x or o)
     * @return     True if the player has 3-in-a-row; false otherwise.
     */
     
    bool checkwinner(char check)
    {
     // Set the array element variables
     int i;
     
    
    for (i=1;i<24;i++) {
        printf("%d   ",checkboard[i]);
    }
    
     // Loop through checkboard to check winner
     if (checkboard[0] == 11) {
                       k++;
     }
     
     if (checkboard[0] == 22) {
                       j++;
     }
     
     for (i=1;i<24;i++) {
         
         if (i%3 == 0) {
                 k = 0;
                 j = 0;
         }
         
         if (checkboard[i] == 11) {
                           k++;
         }
         
         if (checkboard[i] == 22) {
                       j++;
         }
         
         if (k == 3) {
               return true;
         }
         
         if (j == 3) {
              return true;
         }
         
         else {
              return false;
         }
     }
    
    
    
    
    
    }
    
    /* Prompts the player for their choice of block
     * @param  player     Identifier of current player 
     * @return  The number of the block that they want to put an 'x' or 'o' in. (1..9)
     */
    int playerinput(int player)
    {
      int key;
     
      do
      {
       printf ("\n\nPlayer %d : Please select a space ", player);
       int status = scanf("%d",&key);
      } while ( (key<1) && (key>9) );
      return key;
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Firstly, I think your return value is inconsistent with how it is used. You seem to loop while the result is true, but return true only when the game is won.

    Second, Why are you using the magic numbers 22 and 11 for X or O? Don't.

    Third, you need to devise an algorithm on paper for identifying 3-in a row before you can code it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This doesn't assign zero to i at the start of the loop.
    Code:
        for (i==0;i<60;i++) {
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Quote Originally Posted by King Mir View Post
    Firstly, I think your return value is inconsistent with how it is used. You seem to loop while the result is true, but return true only when the game is won.

    Second, Why are you using the magic numbers 22 and 11 for X or O? Don't.

    Third, you need to devise an algorithm on paper for identifying 3-in a row before you can code it.
    Thanks for the reply but for second part I didn't know any easier way to store which boxes have been used and check for it later on if the game has been won.
    Also I figured my algorithm is wrong to check if someone wins the game, going to do that now. Thanks again for the advice.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Quote Originally Posted by iMalc View Post
    This doesn't assign zero to i at the start of the loop.
    Code:
        for (i==0;i<60;i++) {
    That was the mistake why it would end the game, Thanks for pointing it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-18-2010, 09:42 PM
  2. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 04:58 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM