Thread: bool function always seems to return true

  1. #1
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50

    bool function always seems to return true

    Greetings, you may know me from my posting in the C Forums.

    Having basic knowledge in Java, and thus a basic knowledge in Object Orientation, I decided to give C++ a go.

    I decided to make a console-based tic-tac-toe game.

    I'm having fun at it so far, but I have run up to a problem. I have a pointer to a Grid class (made by myself) called grid.

    Whenever I call grid->isMoreMoves() , it seems to return true, as I am stuck in a loop dependent on it eventually becoming false.

    I can't work out what I'm doing wrong? Any ideas?

    Code:
    //boxes (class field) is an array of 9 characters that represent each
    //"box" of the grid.
    
    //mark (class field) is an array of 2 characters" ( 'X' or 'O'), one for
     //each player.
    
    //This function is contained in the public section of the class.
            bool isMoreMoves()
            {
                bool moreMoves = false;
    
                for ( int i = 0; i < 9; i++)
                {
                    if ( (boxes[i] != mark[0]) || (boxes[i] != mark[1]) )
                    {
                        moreMoves = true;
                    }
                    cout << boxes[i] << " "; //This line was to determine whether or not I was getting all values
                }
                cout << endl;
    
                return moreMoves;
            }
    I'm testing out as only player 1, as I have not implemented switching players yet.
    Last edited by The Doctor; 10-22-2012 at 10:57 PM.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  2. #2
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Oh man, I feel so stupid!

    I just realized that I should have been &&'ing rather than ||'ing on line 14. *FACEPALM*
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-03-2011, 06:30 AM
  2. Why the eof returns false when it should return true?
    By kulfon in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2011, 09:57 AM
  3. if/ else if will not return else if when it is true
    By nnizzle in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2010, 05:27 AM
  4. Return True or False, Not Print
    By BB18 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 02:51 PM
  5. True or false statments used in my return...
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2002, 04:26 PM

Tags for this Thread