Thread: If problems

  1. #1
    Registered User Techgique's Avatar
    Join Date
    Jun 2011
    Location
    San Diego
    Posts
    9

    If problems

    Hi everyone,

    I noticed an earlier tic tac toe program, but my particular problem is slightly different. I am using bloodshed to compile this bad boy and I am getting weird results. I have narrowed the problem down to my already suspect if statements that compare the board array elements to see if we have a winner. Eventually I will add a return 0 statement to finish off the program when a winner is declared, but for now there is a problem.

    I have taken out the if statements and everything enters in correctly and prints correctly including input validation.

    The weird problem is when I use the if statements to check for a winner, it automatically declares player one the winner and places an X in elements (0,0), (0,1), and (0,2). When I changed the top line in the if statement to (1,0), (1,1), and (1, 2) it places Xs in those respective elements. Somehow it is still checking for a winner and declaring a winner.

    Basically, can I even write my if statement like this or is something wrong with the way it is laid out? Although I imagine this comes with the territory, but is there a cleaner way that I could put this last piece of the puzzle together or "should" it work?

    Many thanks everyone.

    Sorry about the formatting on those statements, it is showing up properly on my compiler. Also, the wincheck function was being used earlier as an attempt to do the same thing, but I decided to try and directly input the if/else winner statements and just commented it out for now.

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <cstdlib>
    
    //Tic Tac Toe by Steve P.
    
    using namespace std;
    bool check (int, int);
    bool legal = false;
    void active (void);
    char board [3][3];
    
    int main()
    {
    	int row,col;
    	char game = 'y';
    
    	for (row=0;row<3;row++)
    	{
    		for (col=0;col<3;col++)
    		board[row][col]='*';
    	}
    
    	cout<<"GREETINGS GAMERS!!  TIME TO TIC TAC TOE!!"<<endl<<endl;
    	cout<<"You will need two players for this game."<<endl<<endl;
    	
    	active();
    	cout<<endl;
    	cout<<"^^^This is your board.^^^"<<endl;
    	
    		for (int count=0;count<9;count++)
    		{
    			cout<<"Player One, enter in the row"<<endl;
    			cout<<"that you want an X in."<<endl;
    			cout<<"Enter row here-->";
    			cin>>row;
    			cout<<"Player One, enter the column-->";
    			cin>>col;
    
    			legal = false;
    			while (legal == false)
    			{
    				legal= check(row, col);
    				if (!legal)
    				{
    					cout<<"Spot already taken."<<endl;
    					cout<<"Enter in row again.-->";
    					cin>>row;
    					cout<<"Enter in column again."<<endl;
    					cin>>col;
    				}
    			}  
    			board [row][col] = 'X';
    			active();
    			count++;
    			if (count == 9)
    			{
                          cout<<"We have a tie! Thanks for playing!"<<endl;
                          system ("PAUSE");
                          return 0;
                }
    			
    			if      (((board[0][0]='X')&&(board[0][1]='X')&&(board[0][2]='X')) || 
                         ((board[1][0]='X')&&(board[1][1]='X')&&(board[1][2]='X')) ||
                         ((board[2][0]='X')&&(board[2][1]='X')&&(board[2][2]='X')) ||
    		             ((board[0][0]='X')&&(board[1][0]='X')&&(board[2][0]='X')) ||
    	                 ((board[0][1]='X')&&(board[1][1]='X')&&(board[2][1]='X')) ||
                   	     ((board[0][2]='X')&&(board[1][2]='X')&&(board[2][2]='X')) ||
               	         ((board[0][0]='X')&&(board[1][1]='X')&&(board[2][2]='X')) ||
                   	     ((board[2][0]='X')&&(board[1][1]='X')&&(board[0][2]='X')))
                   	     {
                             cout<<"Congratulations player One! You Win!!"<<endl;
                         }
                
               	else if(((board[0][0]='O')&&(board[0][1]='O')&&(board[0][2]='O')) || 
                        ((board[1][0]='O')&&(board[1][1]='O')&&(board[1][2]='O')) ||
                        ((board[2][0]='O')&&(board[2][1]='O')&&(board[2][2]='O')) ||
                        ((board[0][0]='O')&&(board[1][0]='O')&&(board[2][0]='O')) ||
                        ((board[0][1]='O')&&(board[1][1]='O')&&(board[2][1]='O')) ||
                        ((board[0][2]='O')&&(board[1][2]='O')&&(board[2][2]='O')) ||
                        ((board[0][0]='O')&&(board[1][1]='O')&&(board[2][2]='O')) ||
                        ((board[2][0]='O')&&(board[1][1]='O')&&(board[0][2]='O')))
                        {
                            cout<<"Congratulations player Two! You Win!!"<<endl;
                        }
    
    			cout<<"Player Two, enter in the row"<<endl;
    			cout<<"that you want an O in"<<endl;
    			cout<<"Enter here-->";
    			cin>>row;
    			cout<<"Player Two, enter the column-->";
    			cin>>col;
    
    			legal = false;
    			while (legal == false)
    			{
    				legal= check(row, col);
    				if (!legal)
    				{
    					cout<<"Spot already taken."<<endl;
    					cout<<"Enter in row again.-->";
    					cin>>row;
    					cout<<"Enter in column again."<<endl;
    					cin>>col;
    				}
    			} 
    			board[row][col] = 'O';
    			active();
    			if      (((board[0][0]='X')&&(board[0][1]='X')&&(board[0][2]='X')) || 
                         ((board[1][0]='X')&&(board[1][1]='X')&&(board[1][2]='X')) ||
                         ((board[2][0]='X')&&(board[2][1]='X')&&(board[2][2]='X')) ||
    		             ((board[0][0]='X')&&(board[1][0]='X')&&(board[2][0]='X')) ||
    	                 ((board[0][1]='X')&&(board[1][1]='X')&&(board[2][1]='X')) ||
                   	     ((board[0][2]='X')&&(board[1][2]='X')&&(board[2][2]='X')) ||
               	         ((board[0][0]='X')&&(board[1][1]='X')&&(board[2][2]='X')) ||
                   	     ((board[2][0]='X')&&(board[1][1]='X')&&(board[0][2]='X')))
               	         cout<<"Congratulations player One! You Win!!"<<endl;
               	else if(((board[0][0]='O')&&(board[0][1]='O')&&(board[0][2]='O')) || 
                        ((board[1][0]='O')&&(board[1][1]='O')&&(board[1][2]='O')) ||
                        ((board[2][0]='O')&&(board[2][1]='O')&&(board[2][2]='O')) ||
                        ((board[0][0]='O')&&(board[1][0]='O')&&(board[2][0]='O')) ||
                        ((board[0][1]='O')&&(board[1][1]='O')&&(board[2][1]='O')) ||
                        ((board[0][2]='O')&&(board[1][2]='O')&&(board[2][2]='O')) ||
                        ((board[0][0]='O')&&(board[1][1]='O')&&(board[2][2]='O')) ||
                        ((board[2][0]='O')&&(board[1][1]='O')&&(board[0][2]='O')))
                        cout<<"Congratulations player Two! You Win!!"<<endl;
    		
    	    }
    		cout<<"Looks like we got ourselves a tie!"<<endl;
    
    	system ("PAUSE");
    	return 0;
    }
    
    bool check(int r, int c)
    {
    	if (board [r][c] != '*')
    		return false;
    	else
    		return true;
    }
    
    
    void active (void)
    {
    	cout<<"       TIC     TAC    TOE   "<<endl;
    	cout<<"________________________________"<<endl;
    	cout<<"         C 0     C 1    C 2   "<<endl<<endl;
    	cout<<" R 0  |   "<<board[0][0]<<"   |   "<<board[0][1]<<"   |  "<<board[0][2]<<"    "<<endl;
    	cout<<"       -----------------------"<<endl;
    	cout<<" R 1  |   "<<board[1][0]<<"   |   "<<board[1][1]<<"   |  "<<board[1][2]<<"    "<<endl;
    	cout<<"       -----------------------"<<endl;
    	cout<<" R 2  |   "<<board[2][0]<<"   |   "<<board[2][1]<<"   |  "<<board[2][2]<<"    "<<endl;
    }
    
    /*void wincheck (void)
    {
    	if (((board[0][0]='X')&&(board[0][1]='X')&&(board[0][2]='X')) || 
    		((board[1][0]='X')&&(board[1][1]='X')&&(board[1][2]='X')) ||
    		((board[2][0]='X')&&(board[2][1]='X')&&(board[2][2]='X')) ||
    		((board[0][0]='X')&&(board[1][0]='X')&&(board[2][0]='X')) ||
    		((board[0][1]='X')&&(board[1][1]='X')&&(board[2][1]='X')) ||
    		((board[0][2]='X')&&(board[1][2]='X')&&(board[2][2]='X')) ||
    		((board[0][0]='X')&&(board[1][1]='X')&&(board[2][2]='X')) ||
    		((board[2][0]='X')&&(board[1][1]='X')&&(board[0][2]='X')))
    		cout<<"Congratulations player One! You Win!!"<<endl;
    	else if (((board[0][0]='O')&&(board[0][1]='O')&&(board[0][2]='O')) || 
    		((board[1][0]='O')&&(board[1][1]='O')&&(board[1][2]='O')) ||
    		((board[2][0]='O')&&(board[2][1]='O')&&(board[2][2]='O')) ||
    		((board[0][0]='O')&&(board[1][0]='O')&&(board[2][0]='O')) ||
    		((board[0][1]='O')&&(board[1][1]='O')&&(board[2][1]='O')) ||
    		((board[0][2]='O')&&(board[1][2]='O')&&(board[2][2]='O')) ||
    		((board[0][0]='O')&&(board[1][1]='O')&&(board[2][2]='O')) ||
    		((board[2][0]='O')&&(board[1][1]='O')&&(board[0][2]='O')))
    		cout<<"Congratulations player Two! You Win!!"<<endl;
    }
    */

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    If you want to set a variable to a value you use a single equals sign ie board[0][0] = 'O'

    If you want to compare a variable to a value you use 2 equal signs ie board[0][0] == 'O'

    That is you need to use a LOT more equal signs than you have.....
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User Techgique's Avatar
    Join Date
    Jun 2011
    Location
    San Diego
    Posts
    9
    Hi novacain,

    although I'm a tad embarrassed at such a rookie mistake, that indeed was the trouble. Everything works perfectly, thank you so much!

    tg

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Techgique View Post
    Hi novacain,

    although I'm a tad embarrassed at such a rookie mistake, that indeed was the trouble. Everything works perfectly, thank you so much!

    tg
    You might want to do a little reading up on loops.... You could have simplified your code significantly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 01:47 AM
  3. Problems
    By dylanburford in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2005, 11:22 AM
  4. C problems
    By conra in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 02:58 PM