dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Tic-tac-toe help

  1. #1
    Join Date
    May 2006
    Posts
    102

    Tic-tac-toe help

    Hey all, so I'm making the obligatory Tic-Tac-Toe game. While it's mostly completed, I've hit a slight snag.

    Basically, I've assigned each grid of the board to a character variable. For example, the top row is assigned variables a, b, and c.

    Now, when each of these variables ='X', the program, should report "Good Game!".

    The problem is, given my code below, the program will display the "Good Game" message, even if only the c variable = 'X'. I think the problem lies in the exit condition of my while loop. Can any verify this?

    Thanks

    Code:
    #include <iostream>
    
    using namespace std;
    
    void game(void);
    
    
    int playerturnarray[9]={1,2,1,2,1,2,1,2,1}; //Keeps track of whose turn it is
    
    char markervalue[9]={'X','O','X','O','X','O','X','O','X'}; //Gameboard pieces
    
    //Each of the 9 squares is assigned to one of the following char variables
    char a='1'; 
    char b='2';
    char c='3';
    char d='4';
    char e='5';
    char f='6';
    char g='7';
    char h='8';
    char i2='9';
    
    
    int player_move;
    
    int i=0; //counter for the do-while loop
    
    
    int main()
    {
        
        game();
        return 0;
    };
    
        
    void game(void)
    {
         
         do
         {
         
        system ("CLS");
        
        cout << "     |     |     " << endl;
        cout << "  " << a <<"  |  "<< b <<"  |  "<< c <<"  " << endl;
        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;
        cout << "  " << d <<"  |  "<< e <<"  |  "<< f <<"  " << endl;
        cout << "_____|_____|____" << endl;
        cout << "     |     |    " << endl;
        cout << "  " << g <<"  |  "<< h <<"  |  "<< i2 <<"  " << endl;
        cout << "     |     |    " << endl;
        
        
        cout << endl << endl << endl << endl;
        
        
        
        cout << "Player " << playerturnarray[i] << ", enter a square number: ";
        cin >> player_move;
        
        if (!(player_move))
        
    {   cout << endl << "That is not an option, please enter a number between 1-9: ";
        cin.clear();
        cin.ignore(50, '\n');
        cin >> player_move;
    }
        switch (player_move)
        {
        case 1 : a=markervalue[i];
                 break;
        case 2 : b=markervalue[i];
                 break;
        case 3 : c=markervalue[i];
                 break;
        case 4 : d=markervalue[i];
                 break;
        case 5 : e=markervalue[i];
                 break;
        case 6 : f=markervalue[i];
                 break;
        case 7 : g=markervalue[i];
                 break;
        case 8 : h=markervalue[i];
                 break;
        case 9 : i2=markervalue[i];
                 break;
       default : 
        {
             cout << "That is not an option";
             cin.clear();
             cin.ignore(50, '\n');
             continue;
        };
    } // End switch
    
        i++;
    
        
    } while (a&&b&&c!='X');
    
    system("CLS");
        cout << "     |     |     " << endl;
        cout << "  " << a <<"  |  "<< b <<"  |  "<< c <<"  " << endl;
        cout << "_____|_____|_____" << endl;
        cout << "     |     |     " << endl;
        cout << "  " << d <<"  |  "<< e <<"  |  "<< f <<"  " << endl;
        cout << "_____|_____|____" << endl;
        cout << "     |     |    " << endl;
        cout << "  " << g <<"  |  "<< h <<"  |  "<< i2 <<"  " << endl;
        cout << "     |     |    " << endl;
        
        
    
    
      cout << endl << "Good Game!";
      int wait;
      cin >> wait;
    }  //Function

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Tic-tac-toe help

    Code:
    while (a&&b&&c!='X');
    Things like this don't work the way you might think.
    use
    Code:
    while ( a !='X' && b != 'X' && c!='X' );
    but then I think you actually might want
    Code:
    while ( a !='X' || b != 'X' || c!='X' );
    Kurt

  3. #3
    Join Date
    May 2006
    Posts
    102

    Re: Tic-tac-toe help

    Thanks man, that seems to do the trick (and kinda leaves me scratching my head at the same time).

    Going one step further, whats the correct way to say the following in C++:

    While (a & b & c are all not equal to 'X' OR...while a & b & c are all not equal to 'O')

    To clarify a bit if necessary:

    If a,b,c are all equal to 'X', the top row will read XXX, thus three in a row game over.
    If a,b,c are all equal to 'O', the top row will read OOO, game over as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)