Thread: exit loop not working properly

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    exit loop not working properly

    Is this a valid do while loop?My program doesn't end when there is a match of three x's or 3 o's. I'm using a one dimensional array to store the values for the user and computers x's and o's for a game of tic tac toe.

    TTTboard[] has 9 spaces to store values in. Can I not use function calls for my do while loops parameters?

    I'd really appreciate any input!

    Thanks a tonne,

    melodia




    Code:
         do
              {
                 userturn(TTTboard);
                 computerturn(TTTboard);
                
              }while(CheckPossRow(TTTboard)==FALSE||BoardFull(TTTboard,MAX)==FALSE);
             
        }
      printf("\tThank you for playing TIC-TAC-TOE! See you soon!\n\n");
      system("PAUSE");   
      return 0;
    }
    
    
    /*******************************************************************************
    ********************************************************************************
    This Function checks with the help of RowIsWinner if there are any winning rows
    *******************************************************************************/
    
    
    int CheckPossRow(char TTTboard[])
    {
    char Sq1=TTTboard[0];
    char Sq2=TTTboard[1];
    char Sq3=TTTboard[2];
    char Sq4=TTTboard[3];
    char Sq5=TTTboard[4];
    char Sq6=TTTboard[5];
    char Sq7=TTTboard[6];
    char Sq8=TTTboard[7];
    char Sq9=TTTboard[8];
    
    
    if(RowIsWinner(Sq1,Sq2,Sq3)==TRUE)
       return TRUE;
    else if(RowIsWinner(Sq4,Sq5,Sq6)==TRUE)
       return TRUE;
    else if(RowIsWinner(Sq7,Sq8,Sq9)==TRUE)
       return TRUE;
    
    else if(RowIsWinner(Sq1,Sq4,Sq7)==TRUE)
       return TRUE;
    else if(RowIsWinner(Sq2,Sq5,Sq8)==TRUE)
       return TRUE;
    else if (RowIsWinner(Sq3,Sq6,Sq9)==TRUE)
       return TRUE;
    else if(RowIsWinner(Sq1,Sq5,Sq9)==TRUE)
       return TRUE;
    else if(RowIsWinner(Sq3,Sq5,Sq7)==TRUE)
       return TRUE;
    else
       return FALSE;   
    
    }
    
    /*******************************************************************************
    ********************************************************************************
    This function checks to see if the board (array) is full or has empty spaces free 
    
    *******************************************************************************/
    int BoardFull(char TTTboard[],int max)
    {
      int i;
       for (i=0; i < max; i++)
       {
    	 if (TTTboard[i] == EMPTY)
    	 {
    	    return(FALSE);  /* it was found */
    	 }
       }
       return(TRUE);  /* if it was not found */
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    sorry here is the row is winner function that is called

    Code:
    /*******************************************************************************
    ********************************************************************************
    ******************************************************************************
    ********************************************************************************
     This function determines if a row of tic-tac-toe squares is a winning row.
     PRE: s1, s2, and s3 are defined
     POST: returns true if a row (horizontal, vertical, or diagonal) on
             a tic-tac-toe board containing [s1, s2, s3],
             is a winning row, and false otherwise.    
    *******************************************************************************/
    
    int RowIsWinner(char s1, char s2, char s3)
    {
        if (s1==s2 && s2==s3 && s1!= EMPTY)
       
           return TRUE;
       
        else
          
           return FALSE;   
    }

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    while(CheckPossRow(TTTboard)==FALSE||BoardFull(TTT board,MAX)==FALSE);

    Right now you continue the loop if there is no winner OR if the board is not full but what you want to do is continue if there is no winner AND the board is not full.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    Wink

    Thank you so much! It seems like its always something small that I repeatedly miss! I really appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Strncpy not working properly in windows?...
    By alman9898 in forum C Programming
    Replies: 10
    Last Post: 09-28-2009, 05:05 AM
  3. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM
  4. Terminating loop properly?
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 12-24-2004, 06:34 AM
  5. loop not working
    By Jarrette in forum C++ Programming
    Replies: 12
    Last Post: 04-27-2003, 11:34 PM