Like Tree2Likes
  • 2 Post By Salem

Tic Tac Toe Help

This is a discussion on Tic Tac Toe Help within the C++ Programming forums, part of the General Programming Boards category; The program works but it has a bug that I can't seem to figure out how to fix efficiently. If ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    Question Tic Tac Toe Help

    The program works but it has a bug that I can't seem to figure out how to fix efficiently. If one player selects a box it will get their mark. but if the next player chooses the same box, either accidentally or on purpose, it will override the previous players choice. How do I prevent this from happening without writing out a a false statement for every single possibility.
    Code:
    #include <iostream>
    #include <math.h>
    #include <stdlib.h>
    
    
    using namespace std;
    char PlayerOneUsername[30];
    char PlayerTwoUsername[30];
    int Player1Score;
    int Player2Score;
    int TurnNumber = 1;
    char Box1 = '1';
    char Box2 = '2';
    char Box3 = '3';
    char Box4 = '4';
    char Box5 = '5';
    char Box6 = '6';
    char Box7 = '7';
    char Box8 = '8';
    char Box9 = '9';
    int SquareSelection = 0;
    char x = 'x';
    char o = 'o';
    char Repeat;
    
    
    void TwoPlayer(){
    cout<<"Enter Player One Username: ";
    cin>>PlayerOneUsername;
    cout<<"Enter Player Two Username: ";
    cin>>PlayerTwoUsername;
    system("cls");
    while(TurnNumber < 10){
    cout<< TurnNumber<<"\n";
    cout<<PlayerOneUsername<<": "<< Player1Score;
    cout<<" "<<PlayerTwoUsername<< ": "<< Player2Score<<"\n";
    cout<< Box1<<" "<<Box2<<" "<<Box3<<"\n";
    cout<< Box4<<" "<<Box5<<" "<<Box6<<"\n";
    cout<< Box7<<" "<<Box8<<" "<<Box9<<"\n";
        if (TurnNumber == 1 || TurnNumber ==3 || TurnNumber == 5 || TurnNumber == 7 || TurnNumber == 9){
        cout<<"Player One Select your Square: ";
        cin>> SquareSelection;
            if(SquareSelection == 1)
                Box1 = x;
            else if(SquareSelection == 2)
                Box2 = x;
            else if(SquareSelection == 3)
                Box3 = x;
            else if(SquareSelection == 4)
                Box4 = x;
            else if(SquareSelection == 5)
                Box5 = x;
            else if(SquareSelection == 6)
                Box6 = x;
            else if(SquareSelection == 7)
                Box7 = x;
            else if(SquareSelection == 8)
                Box8 = x;
            else if(SquareSelection == 9)
                Box9 = x;
        }
        else if(TurnNumber == 2 || TurnNumber == 4 || TurnNumber == 6 || TurnNumber == 8){
            cout<<"Player Two Select Your square: ";
            cin>> SquareSelection;
            if(SquareSelection == 1)
                Box1 = o;
            else if(SquareSelection == 2)
                Box2 = o;
            else if(SquareSelection == 3)
                Box3 = o;
            else if(SquareSelection == 4)
                Box4 = o;
            else if(SquareSelection == 5)
                Box5 = o;
            else if(SquareSelection == 6)
                Box6 = o;
            else if(SquareSelection == 7)
                Box7 = o;
            else if(SquareSelection == 8)
                Box8 = o;
            else if(SquareSelection == 9)
                Box9 = o;
            }
    TurnNumber ++;
    system("cls");
    if(Box1 == x && Box2 == x && Box3 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box4 == x && Box5 == x && Box6 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box7 == x && Box8 == x && Box9 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box1 == x && Box4 == x && Box7 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box2 == x && Box5 == x && Box8 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box3 == x && Box6 == x && Box9 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box1 == x && Box5 == x && Box9 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box3 == x && Box5 == x && Box7 == x){
    cout<<"\n Congradulations "<<PlayerOneUsername<<". You won!\n";
    Player1Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box1 == o && Box2 == o && Box3 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box4 == o && Box5 == o && Box6 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box7 == o && Box8 == o && Box9 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box1 == o && Box4 == o && Box7 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box2 == o && Box5 == o && Box8 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box3 == o && Box6 == o && Box9 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box1 == o && Box5 == o && Box9 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    else if(Box3 == o && Box5 == o && Box7 == o){
    cout<<"\n Congradulations "<<PlayerTwoUsername<<". You won!\n";
    Player2Score ++;
    TurnNumber = 10;
    system("PAUSE");}
    if(TurnNumber == 10){
    cout<<"Would you like to play again (y/n): ";
    cin>> Repeat;
        if(Repeat == 'y'){
        TurnNumber = 0;
        Box1 = '1';
        Box2 = '2';
        Box3 = '3';
        Box4 = '4';
        Box5 = '5';
        Box6 = '6';
        Box7 = '7';
        Box8 = '8';
        Box9 = '9';
        }
    }
    }
    
    
    }
    
    
    void Instructions(){
    
    
    }
    
    
    void Quit(){
    
    
    }
    
    
    int main()
    {
        int Selection;
        cout<<"####################################################\n";
        cout<<"######### Welcome! Please Select Your Game.#########\n";
        cout<<"####################################################\n";
        cout<<"\n";
        cout<<"1. Single Player\n";
        cout<<"2. Two Player\n";
        cout<<"3. Instructions\n";
        cout<<"4. Quit\n";
        cout<<"Selection: ";
        cin>>Selection;
        switch(Selection){
        case 1:
        SinglePlayer();
        break;
        case 2:
        TwoPlayer();
        break;
        case 3:
        Instructions();
        break;
        case 4:
        Quit();
        break;
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Code:
    char Box1 = '1';
    char Box2 = '2';
    char Box3 = '3';
    char Box4 = '4';
    char Box5 = '5';
    char Box6 = '6';
    char Box7 = '7';
    char Box8 = '8';
    char Box9 = '9';
    Try replacing this with
    char board[3][3];
    Or perhaps
    char Boxes[9];

    Anytime you put 1,2,3 on the end of a variable name, it should be screaming in your mind "use an array!"

    > if(SquareSelection == 1)
    > Box1 = x;
    Perhaps
    if(SquareSelection == 1 && Box1 == '1')
    iMalc and Solarwin like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21