CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Creating a good computer AI for a tic tac toe game.

    I'm almost done with this little mini project that I've been doing for a couple of days and I was wondering how I can go about creating a computer AI given the way that I structured my code. Also, suggestions how I can make my code look better is always welcomed.

    main.cpp
    Code:
    #include <iostream>
    #include "Board.h"
    #include <limits>
    #include <cctype>
    
    
    using namespace std;
    
    
    int main()
    {
        // Welcoming message.
        cout << "Welcome users to my very own tic-tac-toe game. " << endl;
    
    
        // Object to access the class functions
        Board process;
        char response1; // A variable to gather input from player1 if they want to quit the game or not
        char response2; // A variable to gather input from player2 if they want to quit the game or not
        int tileCounter = 0; // Represents how many tiles have been used.
        int playerWins1 = 0; // How many times player1 won a match.
        int PlayerLose1 = 0; // How many times Player1 lost a match.
        int playerLose2 = 0; // How many times player 2 lost a match.
        int playerWins2 = 0; // How many times player2 won a match.
        int tie = 0;         // How many times a match ended up in a tie.
    
    
        while(toupper(response1) != 'N' || toupper(response2) != 'N')
        {
            process.choosingLetter(); // Players choose their letter for the game.
            cout << endl << endl;
            process.draw(); // Creates the board.
            tileCounter = 0; // reinitializes after each game.
    
    
            // Game loop
            // If there are no 3 matches, keep playing.
            while(!process.isGameOver(tileCounter,playerWins1,PlayerLose1, playerWins2, playerLose2, tie)) // If there are no 3 matches, keep playing.
            {
                cout << endl;
                process.playerMove1(); // Outputs player1's move.
                cout << endl << endl << endl;
                process.draw(); // Call the board again to see the changes.
                cout << endl;
                tileCounter++;
                if(process.isGameOver(tileCounter,playerWins1,PlayerLose1, playerWins2, playerLose2, tie) == true)
                {
                    break;
                }
                process.playerMove2(); // Outputs player2's move.
                cout << endl << endl;
                process.draw();
                tileCounter++;
            }
    
    
            process.playAgain(response1, response2);
        }
        process.scoreBoard(playerWins1,PlayerLose1, playerWins2, playerLose2, tie);
    
    
        return 0;
    }
    board.h
    Code:
    #ifndef BOARD_H
    #define BOARD_H
    
    
    class Board
    {
    public:
        Board();
        void draw();
        void choosingLetter();
        void playerMove1();
        void playerMove2();
        void playerInputCheck1();
        void playerInputCheck2();
        void playerLocationCheck1();
        void playerLocationCheck2();
        bool isGameOver(int &, int &, int &, int &, int &, int &);
        void refresh();
        void playAgain(char &, char &);
        void scoreBoard(int &, int &, int &, int &, int &);
    private:
        char board[3][3];
        char PlayerLetter1; // The letter that player 1 will be.
        char PlayerLetter2; // The letter that player 2 will be
        std::string playerInput1;  // Input from player1.
        std::string playerInput2;  // Input from player2.
    };
    
    
    #endif


    board.cpp
    Code:
    #include <iostream>
    #include <limits>
    #include "Board.h"
    
    
    using namespace std;
    
    
    Board::Board()
    {
        for(unsigned int i = 0; i < 3; i++)
            for(unsigned int j = 0; j < 3; j++)
                board[i][j] = ' ';
    }
    
    
    void Board::draw()
    {
        for(unsigned int i = 0; i < 3; i++)
        {
            cout << "\t\t";
            for(unsigned int j = 0;  j < 3;  j++)
            {
                // Display the board.
                cout << "|" << board[i][j] << "|";
            }
            cout << endl; // Create a space between each row and column.
        }
    
    
    }
    
    
    
    
    void Board::choosingLetter()
    {
        // Prompt the user to choose the letter X or O
        cout << "\nPlayer 1, which letter would you like to start as? X or O? ";
        cout << endl;
    
    
        cout << "\n*Note X or O are the only acceptable inputs. ";
        cin >> PlayerLetter1;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    
        // Check for valid input.
        while(PlayerLetter1 != 'X' && PlayerLetter1 != 'O')
        {
            cerr << "\nError, invalid input. ";
            cin >> PlayerLetter1;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    
    
        // If player 1 chooses X, then player2 will be O and vice versa.
        if(PlayerLetter1 == 'X')
            PlayerLetter2 = 'O';
        else if(PlayerLetter1 == 'O')
            PlayerLetter2 = 'X';
    }
    
    
    void Board::playerMove1()
    {
        cout << endl;
    
    
        // Commands that dictates where the letter will be placed.
        cout << "\t\t" << "Legend Key" << endl << endl;
    
    
        cout << "UL=UpperLeft  UC=UpperCenter   UR=UpperRight " << endl;
        cout << "LC=LeftCenter MC=MiddleCenter  RC=RightCenter " << endl;
        cout << "BL=BottomLeft BM=BottomMiddle  BR=BottomRight " << endl;
    
    
        cout << endl;
    
    
        // Prompting Player1.
        cout << "\nPlayer 1, pick a field. ";
    
    
        // Checks for valid letter placement and non duplicate locations.
        playerLocationCheck1();
    }
    
    
    void Board::playerMove2()
    {
        cout << endl;
    
    
        // Commands that dictates where the letter will be placed.
        cout << "\t\t" << "Legend Key" << endl << endl;
    
    
        cout << "UL=UpperLeft  UC=UpperCenter   UR=UpperRight " << endl;
        cout << "LC=LeftCenter MC=MiddleCenter  RC=RightCenter " << endl;
        cout << "BL=BottomLeft BM=BottomMiddle  BR=BottomRight " << endl;
    
    
        cout << endl;
    
    
        // Prompting Player2.
        cout << "\nPlayer 2, pick a field. ";
    
    
        // Checks for valid letter placement and non duplicate locations.
        playerLocationCheck2();
    }
    
    
    void Board::playerInputCheck1()
    {
        cin >> playerInput1;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    
        while(playerInput1 != "UL" && playerInput1 != "UC" && playerInput1 != "UR"
           && playerInput1 != "LC" && playerInput1 != "MC" && playerInput1 != "RC"
           && playerInput1 != "BL" && playerInput1 != "BM" && playerInput1 != "BR")
        {
            cerr << "\nError, unknown command entered. Read the legend key. ";
            cin >> playerInput1;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    
    
    void Board::playerInputCheck2()
    {
        cin >> playerInput2;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    
        while(playerInput2 != "UL" && playerInput2 != "UC" && playerInput2 != "UR"
           && playerInput2 != "LC" && playerInput2 != "MC" && playerInput2 != "RC"
           && playerInput2 != "BL" && playerInput2 != "BM" && playerInput2 != "BR")
        {
            cerr << "\nError, unknown command entered. Read the legend key. ";
            cin >> playerInput2;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    
    
    void Board::playerLocationCheck1()
    {
        bool isEmpty;
    
    
        while(!isEmpty)
        {
            // Checks for valid commands.
            playerInputCheck1();
    
    
            if(playerInput1=="UL" && (board[0][0] != 'X' && board[0][0] != 'O'))
            {
                board[0][0] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="UC" && (board[0][1] != 'X' && board[0][1] != 'O'))
            {
                board[0][1] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="UR" && (board[0][2] != 'X' && board[0][2] != 'O'))
            {
                board[0][2] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="LC" && (board[1][0] != 'X' && board[1][0] != 'O'))
            {
                board[1][0] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="MC" && (board[1][1] != 'X' && board[1][1] != 'O'))
            {
                board[1][1] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="RC" && (board[1][2] != 'X' && board[1][2] != 'O'))
            {
                board[1][2] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="BL" && (board[2][0] != 'X' && board[2][0] != 'O'))
            {
                board[2][0] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="BM" && (board[2][1] != 'X' && board[2][1] != 'O'))
            {
                board[2][1] = PlayerLetter1;
                isEmpty = true;
            }
            else if(playerInput1=="BR" && (board[2][2] != 'X' && board[2][2] != 'O'))
            {
                board[2][2] = PlayerLetter1;
                isEmpty = true;
            }
            else
            {
                cerr << "\nError, this position has already been taken Try again. ";
                isEmpty = false;
            }
        }
    }
    
    
    void Board::playerLocationCheck2()
    {
        bool isEmpty;
    
    
        while(!isEmpty)
        {
            // Checks for valid commands.
            playerInputCheck2();
    
    
            if(playerInput2=="UL" && (board[0][0] != 'X' && board[0][0] != 'O'))
            {
                board[0][0] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="UC" && (board[0][1] != 'X' && board[0][1] != 'O'))
            {
                board[0][1] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="UR" && (board[0][2] != 'X' && board[0][2] != 'O'))
            {
                board[0][2] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="LC" && (board[1][0] != 'X' && board[1][0] != 'O'))
            {
                board[1][0] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="MC" && (board[1][1] != 'X' && board[1][1] != 'O'))
            {
                board[1][1] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="RC" && (board[1][2] != 'X' && board[1][2] != 'O'))
            {
                board[1][2] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="BL" && (board[2][0] != 'X' && board[2][0] != 'O'))
            {
                board[2][0] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="BM" && (board[2][1] != 'X' && board[2][1] != 'O'))
            {
                board[2][1] = PlayerLetter2;
                isEmpty = true;
            }
            else if(playerInput2=="BR" && (board[2][2] != 'X' && board[2][2] != 'O'))
            {
                board[2][2] = PlayerLetter2;
                isEmpty = true;
            }
            else
            {
                cerr << "\nError, this position has already been taken Try again. ";
                isEmpty = false;
            }
        }
    }
    
    
    bool Board::isGameOver(int &tileCounter, int &playerWins1, int &PlayerLose1, int &playerWins2, int &playerLose2, int &tie)
    {
        bool gameOver1 = false; // If there are 3 X's.
        bool gameOver2 = false; // If there are 3 O's.
        bool tieGame = false; // If there are no more tiles.
        bool noGameOver; // If there are no 3 X's or 3 O's.
    
    
    
    
        if(board[0][0] == 'X' && board[0][1] == 'X' && board [0][2] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[1][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[2][0] == 'X' && board[2][1] == 'X' && board [2][2] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[0][0] == 'X' && board[1][0] == 'X' && board [2][0] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[0][1] == 'X' && board[1][1] == 'X' && board [2][1] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[0][2] == 'X' && board[1][2] == 'X' && board [2][2] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[0][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[0][2] == 'X' && board[1][1] == 'X' && board [2][0] == 'X')
        {
            gameOver1 = true;
        }
        else if(board[0][0] == 'O' && board[0][1] == 'O' && board [0][2] == 'O')
        {
            gameOver2 = true;
        }
        else if(board[1][0] == 'O' && board[1][1] == 'O' && board [1][2] == 'O')
        {
            gameOver2 = true;
        }
        else if(board[2][0] == 'O' && board[2][1] == 'O' && board [2][2] == 'O')
        {
            gameOver2 = true;
        }
        else if(board[0][0] == 'O' && board[1][0] == 'O' && board [2][0] == 'O')
        {
            gameOver2 = true;
        }
        else if(board[0][1] == 'O' && board[1][1] == 'O' && board [2][1] == 'O')
        {
            gameOver2 = true;
        }
        else if(board[0][2] == 'O' && board[1][2] == 'O' && board [2][2] == 'O')
        {
            gameOver2 = true;
        }
        else if(board[0][0] == 'O' && board[1][1] == 'O' && board [1][2] == 'O')
        {
            gameOver2 = true;
        }
        else if(board[0][2] == 'O' && board[1][1] == 'O' && board [2][0] == 'O')
        {
            gameOver2 = true;
        }
    
    
        if(gameOver1 == true)
        {
            if(PlayerLetter1 == 'X')
            {
                playerWins1++;
                playerLose2++;
                cout << "Game over! Player 1 is the victor! " << endl;
                return gameOver1;
            }
            else if(PlayerLetter2 == 'X')
            {
                playerWins2++;
                PlayerLose1++;
                cout << "Game over! Player 2 is the victor! " << endl;
                return gameOver1;
            }
        }
        else if(gameOver2 == true)
        {
            if(PlayerLetter1 == 'O')
            {
                playerWins1++;
                playerLose2++;
                cout << "Game over! Player 1 is the victor! " << endl;
                return gameOver2;
            }
            else if(PlayerLetter2 == 'O')
            {
                playerWins2++;
                PlayerLose1++;
                cout << "Game over! Player 2 is the victor! " << endl;
                return gameOver2;
            }
        }
        else if(tileCounter == 9)
        {
            tie++;
            tieGame = true;
            cout << "Tie game! No one wins! " << endl;
            return tieGame;
        }
        else if(gameOver1 && gameOver2 == false)
        {
            noGameOver = false;
        }
    
    
        return noGameOver;
    }
    
    
    void Board::refresh()
    {
        char newTile = ' '; // gets rid of all the X's and O's
    
    
        for(unsigned int i = 0; i < 3; i++)
            for(unsigned int j = 0; j < 3; j++)
                board[i][j] = newTile;
    }
    
    
    void Board::playAgain(char &playerResponse1, char &playerResponse2)
    {
            cout << "\n\nWould you like to play again player1? [y/n] ";
            cin >> playerResponse1;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    
            while(toupper(playerResponse1) != 'Y' && toupper(playerResponse1) != 'N')
            {
                cerr << "\nError, Invalid response. ";
                cin >> playerResponse1;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
            }
    
    
            cout << "\n\nWould you like to play again player2? [y/n] ";
            cin >> playerResponse2;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    
            while(toupper(playerResponse2) != 'Y' && toupper(playerResponse2) != 'N')
            {
                cerr << "\nError, Invalid response. ";
                cin >> playerResponse2;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
            }
    
    
            if(toupper(playerResponse1) == 'Y' && toupper(playerResponse2) == 'Y')
            {
                cout << "\n\nCreating a new board to play on. " << endl;
                refresh(); // refreshes the board.
            }
            else
                cout << "\n\nThanks for playing! " << endl << endl << endl;
    }
    
    
    void Board::scoreBoard(int &playerWins1, int &PlayerLose1, int &playerWins2, int &playerLose2, int &tie)
    {
        cout << "\t\t\tScoreBoard " << endl << endl;
        cout << "--------------------------------------------------------------" << endl;
        cout << "                \t\tWins\t\tLosses " << endl << endl;
    
    
        cout << "player 1: " << "\t\t\t" << playerWins1 << "\t\t" << PlayerLose1 << endl;
    
    
        cout << "player 2: " << "\t\t\t" << playerWins2 << "\t\t" << playerLose2 << endl << endl;
    
    
        cout << "Number of tie games: " << tie << endl;
        cout << "--------------------------------------------------------------" << endl;
    }

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: Creating a good computer AI for a tic tac toe game.

    Quote Originally Posted by Freddy Kreuger View Post
    how I can go about creating a computer AI given the way that I structured my code.
    What do you mean by "good computer AI" and how does this property depend on code structure?

    There's very little structure in your code actually. Except from the Board class you haven't defined a single own type. Your code is more C than C++.

    Splitting a C++ classes into .h and .cpp files is not how you give structure to code in C++. You do it by defining your own classes (and structs).

    Forget about the .h / .cpp split. It's of no importance really. Put everything in .h and let the compiler do the rest.
    Last edited by razzle; December 26th, 2014 at 06:04 PM.

  3. #3
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Re: Creating a good computer AI for a tic tac toe game.

    1.) What I mean by a good computer AI is an AI that won't let the user win by randomly placing on X's or O's on the board(it would place an X or an O right when the user is about to win).

    2.) What I'm saying when I say "code structure", I'm asking how can incorporate a computer ai without overhauling or changing the structure of my program.(I', talking about the way the program looks and all the functions and variable that are inside the program).

    3.) In what way is my program more c oriented than c++ oriented? Is the functions or libraries that I'm using that make more c than anything? Please explain.

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: Creating a good computer AI for a tic tac toe game.

    Quote Originally Posted by Freddy Kreuger View Post
    1.) What I mean by a good computer AI is an AI that won't let the user win by randomly placing on X's or O's on the board(it would place an X or an O right when the user is about to win).

    2.) What I'm saying when I say "code structure", I'm asking how can incorporate a computer ai without overhauling or changing the structure of my program.(I', talking about the way the program looks and all the functions and variable that are inside the program).

    3.) In what way is my program more c oriented than c++ oriented? Is the functions or libraries that I'm using that make more c than anything? Please explain.
    Your program is mostly C because you're not defining own classes.

    Okay you have the Board class but one swallow doesn't make a summer as they say.

    And avoid plaititues like "good computer AI". It's unprofessional.

  5. #5
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Re: Creating a good computer AI for a tic tac toe game.

    Ok, if you may, can you offer a mini example of how you would've effectively defined my class and properly implement it(I've done most of the work. All I'm asking for is an example so that I can learn.)?

  6. #6
    Join Date
    Jul 2013
    Posts
    576

    Re: Creating a good computer AI for a tic tac toe game.

    Quote Originally Posted by Freddy Kreuger View Post
    Ok, if you may, can you offer a mini example of how you would've effectively defined my class and properly implement it(I've done most of the work. All I'm asking for is an example so that I can learn.)?
    You'll have to be a little patient.

    In due time your teacher will introduce what makes C++ more than C.

    Later you may even be asked to rewrite this very first of your programs to reflect what you've learned later in the course. Then you will have graduated from the procedural to the OO and functional paradigms of programing.

    Good luck.
    Last edited by razzle; December 26th, 2014 at 07:26 PM.

  7. #7
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Re: Creating a good computer AI for a tic tac toe game.

    That's all well and good, but the main reason I made this thread was to ask how I can incorporate a computer AI in my program. o you have any pointers on that.

  8. #8
    Join Date
    Jul 2013
    Posts
    576

    Re: Creating a good computer AI for a tic tac toe game.

    Quote Originally Posted by Freddy Kreuger View Post
    That's all well and good, but the main reason I made this thread was to ask how I can incorporate a computer AI in my program. o you have any pointers on that.
    So you want this to be solved,

    "1.) What I mean by a good computer AI is an AI that won't let the user win by randomly placing on X's or O's on the board(it would place an X or an O right when the user is about to win)."

    What's the problem then? Your program's just a few lines. What's preventing you from just doing what you want?

    What kind of help do you want? A kick in the butt to take the plunge and get going?

  9. #9
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Re: Creating a good computer AI for a tic tac toe game.

    A general structure on how you usually incorporate a computer AI would be a good start.

  10. #10
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Re: Creating a good computer AI for a tic tac toe game.

    What's also getting to me is that I'm using commands(string like commands to be exact) to dictate where the X or O will be placed(If you go back to my board.cpp, you would see that I'm using commands like UL, UC, UR, that represents the location of the tile) and I don't know how to program the computer to know that the tile has been taken(I'm pretty sure by now you see can that I have never done this before).

  11. #11
    Join Date
    Jul 2013
    Posts
    576

    Re: Creating a good computer AI for a tic tac toe game.

    I'm sorry but I haven't the faintest idea of what you're asking.

    You're talking like someone who hasn't written this program but wants to modify it without any knowledge of the programming process.

    I'm out.

  12. #12
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Re: Creating a good computer AI for a tic tac toe game.

    ok then, instead of dealing with the AI, I was thinking of cleaning the up all the if/else if statements. The pseudocode that I thought is this

    for (int i = 0; i < rowSize; i++)
    {
    if(if the tile is blank)
    {
    place letter
    }
    else
    output-This spot has been taken Try again.

    Ask the user to pick another command(like UL, UC etc).
    }

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating a good computer AI for a tic tac toe game.

    Code:
    while(toupper(response1) != 'N' || toupper(response2) != 'N')
    This is your first issue with this code.

    If you are trying to go down the ai route, then the program 'learns' from its mistakes so that next time it doesn't make that 'mistake' and tries a different solution.

    If you have a class called Board, then this class would be expected to deal with displaying the board, registering a move etc. I would'nt expect it to deal with play again (better in main()) etc. Another class say TicTacToe could be derived from class Board that deals with checking validity of moves, making a move etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Dec 2014
    Location
    Miami, Florida
    Posts
    9

    Re: Creating a good computer AI for a tic tac toe game.

    Well I don't really see that code is an issue. The reason why is because I want the user to have the option of playing with a computer of a real person. That is why I have two responses that prompt each user if they wish to play again. If one of them doesn't want to play again the game ends. Now, I simply can use another game loop that is for AI's only and then I'll have two game loops that are for real players and a computer AI.

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating a good computer AI for a tic tac toe game.

    That code is an issue because for the first time around the while loop, response1 and response2 haven't been initialised and could contain any value!

    Code:
    char response1 = 'Y';          // A variable to gather input from player1 if they want to quit the game or not
    char response2 = 'Y';          // A variable to gather input from player2 if they want to quit the game or not
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured