Thread: Debugging issues

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

    Debugging issues

    Hi there, I'm having two problems debugging my program I was wondering if anyone has any time for some input. It's a tic-tac-toe program and I'm having an odd issue with the third entry from the user. When they enter where they would like their 'o' to go the computer simply doesn't put it in the array. It seems to ignore it. It doesn't ignore the 4th entry but I thought that was really odd.

    Another thing is that the program doesn't exit the loop in main when there is a match of 3 characters in a row. I'm posting the functions that apply to these two issues. The program complies fine its just those two issues (and I'm going to make the comp more strategic) It might just be really late so I'm probably missing something obvious.

    Thanks a tonne for any help at all!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    #include <ctype.h>
    #include <time.h>
    
    /* Constants for TTT game board symbols. */
    #define EMPTY  ' '
    #define USER 'O'
    #define COMPUTER 'X'
    
    #define TRUE 1
    #define FALSE 0
    #define MIN 1
    #define MAX 9
    
    /* function proto-types (these are function declarations)*/
    
    int ValueInRange(int value, int min, int max);
    int RowIsWinner(char s1, char s2, char s3);
    char TTTopponent(char player);
    int GetIntInput();
    void DisplayHorizontalLine();
    void DisplayOneRow(char s1, char s2, char s3);
    void writetoboard( char TTTboard[], int squarenumber, char player);
    void DisplayTTTBoards(char TTTboard[]);
    int UserSaysYes(char[]);
    char getCharInput();
    void clearInputStream();
    void userturn(char TTTboard[]);
    void computerturn(char TTTboard[]);
    int randIntEven(int, int);
    int CheckPossRow(char TTTboard[]);
    int BoardFull(char TTTboard[],int max);
    void DisplayOneRowInt(int a, int b, int c);
    int randInt(int, int);
    
    
    
    int main()
    {
    
    char TTTboard[]={EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY};
    
    printf("\t\tWELCOME TO TIC-TAC-TOE!!\n\n");
    
        while ( UserSaysYes("WOULD YOU LIKE TO PLAY A GAME OF TIC TAC TOE WITH ME?")) 
        {
                 char currentplayer=USER;
                 DisplayOneRowInt(1,2,3);
                 DisplayHorizontalLine();
                 DisplayOneRowInt(4,5,6);
                 DisplayHorizontalLine();
                 DisplayOneRowInt(7,8,9);
              
              
              do
              {
                 userturn(TTTboard);
                 currentplayer=TTTopponent(currentplayer);
                 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 uses two output functions display the actual array TTT board
       PRE: none.
       POST: three TTT boards have been drawn on the output stream.    
    
    *******************************************************************************/
    void DisplayTTTBoards(char TTTboard[])
    {
       
        DisplayOneRow(TTTboard[0],TTTboard[1],TTTboard[3]);
        DisplayHorizontalLine();
        DisplayOneRow(TTTboard[3],TTTboard[4], TTTboard[5]);
        DisplayHorizontalLine();
        DisplayOneRow(TTTboard[6],TTTboard[7],TTTboard[8]);
    
    }
    
    
    
    
    
    
    
    
    
    
    
    /*******************************************************************************
    ********************************************************************************
     This function gets a valid integer input from the user.
     PRE: User has been prompted to enter an integer and min <= max.
     POST: an integer value, i, entered by user is returned, where min <= i <= max.    
    */
    
    int GetIntInput()
    {
        
        int i;
         
        do
        {
           printf("\n\nPlease enter number of the square you would like to place your 'O' in:");
           while(scanf("%i", &i) != 1)
       /* get rid of the characters that were entered */
           {
           clearInputStream();
           printf("\nThat is not valid. Please enter a valid integer: ");
     
           }
         
    
          /* clear the input stream again, in case extra characters after the integer
          were entered */
           clearInputStream();
    
         }while(ValueInRange(i,MIN,MAX)==FALSE);
            
           
           
                       return i;
              
    } 
    
    
    
    /*******************************************************************************
    ********************************************************************************
    This function takes the users choice of square and enters the appropriate character ('o' or 'x')
    PRE:
    POST:
    *******************************************************************************/
    void writetoboard( char TTTboard[], int squarenumber, char player)
    {
       
         if (player==USER)
         {
           squarenumber=(squarenumber-1);
           TTTboard[squarenumber]='O';
         } 
         else
             {
             TTTboard[squarenumber]='X';
             }  
    }
    
    
    /**********************************************************************
    This function is for the users turn
    *******************************************************************************/
    void userturn(char TTTboard[])
    {
         int SquareNumber;
         do
         {SquareNumber=GetIntInput();
         
         }while(SquareNumber=='X'||SquareNumber=='O');
         
         writetoboard(TTTboard, SquareNumber,USER); 
         DisplayTTTBoards(TTTboard);
    }
    
    
    /*******************************************************************************
    ********************************************************************************
    ******************************************************************************
    ********************************************************************************
     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 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;   
    }
    
    /*******************************************************************************
    ********************************************************************************
    This Function checks for winning rows with the help of RowIsWinner 
    *******************************************************************************/
    
    
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    DisplayOneRowInt(1,2,3);
    DisplayHorizontalLine();
    DisplayOneRowInt(4,5,6);
    DisplayHorizontalLine();
    DisplayOneRowInt(7,8,9);
    Why do you do this, when you have a function to display the whole board?


    Code:
        DisplayOneRow(TTTboard[0],TTTboard[1],TTTboard[3]);
        DisplayHorizontalLine();
        DisplayOneRow(TTTboard[3],TTTboard[4], TTTboard[5]);
        DisplayHorizontalLine();
        DisplayOneRow(TTTboard[6],TTTboard[7],TTTboard[8]);
    This is an example of where "the number of the counting shall not be 3"
    How about a 2?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help debugging in Pelles C IDE
    By gaurav9991 in forum C Programming
    Replies: 3
    Last Post: 10-30-2010, 07:15 AM
  2. Debugging Issues
    By jl864405 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 05:40 PM
  3. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  4. help needed in organising a debugging contest
    By shrijeetp in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-22-2006, 07:45 AM
  5. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM