-1

So, this bug has something to do with converting the player choice 1-9 to displaying it on the board with a X or O. It displays correctly it's just they can over loop each other buy just putting in the same input and I'm not quite sure how to fix this. I've tried moving the convert() around the play game() in the for loop to see if that would somehow fix it by trial and error but no it seems its a coding issue in the convert() function I just don't know where or what that issue is. I'm still learning C++ I'm just testing myself I saw a few people make a tik tac toe game online and I wanted to give it a shot, so any advice is appreciated.

#include "functions.h"
#include <iostream>
#include <vector>
using namespace std;


vector<string> player_icons = { "X", "O" };
vector<string> grid = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
int player1 = 0;
int player2 = 0;
string who_Won1 = "Player 1";
string who_Won2 = "Player 2";

void intro() {//The intro sequence to game
    

    cout << "==================\n";
    cout << " Tic Tac Toe Game \n";
    cout << "==================\n";
    cout << "\n";

    cout << "Instructions: This game will require 2 players\n";
    cout << "To win you need to match 3 in a row of the same\n";
    cout << "\n";

    cout << "Player 1: X\n";
    cout << "Player 2: O\n";
    cout << "\n";

    cout << "     |     |       \n";
    cout << "  1  |  2  |  3    \n";
    cout << "_____|_____|_____  \n";
    cout << "     |     |       \n";
    cout << "  4  |  5  |  6    \n";
    cout << "_____|_____|_____  \n";
    cout << "     |     |       \n";
    cout << "  7  |  8  |  9    \n";
    cout << "     |     |       \n";
    cout << "\n";

    cout << "Above is the example of what the grid is going to look like when you play\n";
    cout << "Each player must select a number 1-9 to put there X or O there\n";

}

void board_make() {//outputs to the terminal the actual tic tac toe board

    std::cout << "     |     |      \n";

    std::cout << "  " << grid[0] << "  |  " << grid[1] << "  |  " << grid[2] << "\n";

    std::cout << "_____|_____|_____ \n";
    std::cout << "     |     |      \n";

    std::cout << "  " << grid[3] << "  |  " << grid[4] << "  |  " << grid[5] << "\n";

    std::cout << "_____|_____|_____ \n";
    std::cout << "     |     |      \n";

    std::cout << "  " << grid[6] << "  |  " << grid[7] << "  |  " << grid[8] << "\n";
    std::cout << "     |     |      \n";


}

bool win_condition() {//its in the name it checks for wins 

    bool winner = false;

    //rows
    if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
        winner = true;
    }
    else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
        winner = true;
    }
    else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
        winner = true;
    }

    //columns
    if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
        winner = true;
    }
    else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
        winner = true;
    }
    else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
        winner = true;
    }

    //across
    if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
        winner = true;
    }
    else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
        winner = true;
    }

    return winner;
}


void game_start() {
    
    
    for (int i = 0; i < 5; i ++) {//iterates through both players turns till after the 
        //Player 1's turn
        cout << "\n";
        cout << "Player 1 its your turn please enter 1-9 to select your choice: "; cin >> player1; convert(); cout << "\n";
        board_make();
        win();
        cout << "\n";
        //Player 2's turn
        cout << "\n";
        cout << "Player 2 its now your turn select your choice 1-9: "; cin >> player2; convert();  cout << "\n";
        board_make();
        win();
        cout << "\n";


    }

    





}


void convert() {
    for (int i = 0; i < 8; i++) {
        //Player 1 checking
        if (player1 == i + 1 && player2 != i + 1) {
            grid.at(i) = "X";

        }
        if (player2 == i + 1 && player1 != i + 1) {
            grid.at(i) = "O";

        }
    }
    

}

void win() {
    
    if (win_condition()) {
        if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
            
            if (grid[0] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[0] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }

        }
        else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
            
            if (grid[3] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[3] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }

        }
        else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
            
            if (grid[6] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[6] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }

        }

        //columns
        if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
            
            if (grid[0] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[0] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }

        }
        else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
            
            if (grid[1] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[1] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }

        }
        else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
            
            if (grid[2] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[2] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }

        }

        //across
        if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
            
            if (grid[0] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[0] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }

        }
        else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
            
            if (grid[2] == "X") {
                cout << who_Won1 << " Won\n";
                exit(0);
            }
            else if (grid[2] == "O") {
                cout << who_Won2 << " Won\n";
                exit(0);
            }
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shractic
  • 1
  • 1
  • 2
    Refer to [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – Jason Nov 22 '22 at 02:56
  • The `convert` function doesn't appear to check if a square is occupied except by the last move before overwriting the value. It was hard to figure out where you were even calling it due to multiple statements on one line. You should break that habit early. Focus on readability over saving a line or two. You're going to need some sort of validation and probably a loop to reject invalid moves. – Retired Ninja Nov 22 '22 at 03:08
  • 2
    Some advice: there is a huge amount of similar code here, that could be made much simpler with a for-loop. Improving the readability of the code could help you debug. – nathaniel Nov 22 '22 at 03:18
  • I edited the code to add a for loop instead of the bunch of if statements i know it wont fix the problem but is it more readable at least the convert() function? – shractic Nov 22 '22 at 14:36

2 Answers2

1

I think your problem is that player1 and player2 variables only store where that players last move was. So once one player makes their second move the other player can overwrite the first move.

You could fix this by checking to see if the grid contains " " before making the move.

Best of luck!

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
w o h
  • 23
  • 1
  • 4
0

Update: I made more of my code readable or tried to, but I fixed the bug I wanted to fix here is the updated code

            #include "functions.h"
            #include <iostream>
            #include <vector>
            using namespace std;


            vector<string> player_icons = { "X", "O" };
            vector<string> grid = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
            int player1 = 0;
            int player2 = 0;
            string who_Won1 = "Player 1";
            string who_Won2 = "Player 2";
            bool filled = false;

            void intro() {//The intro sequence to game


                cout << "==================\n";
                cout << " Tic Tac Toe Game \n";
                cout << "==================\n";
                cout << "\n";

                cout << "Instructions: This game will require 2 players\n";
                cout << "To win you need to match 3 in a row of the same\n";
                cout << "\n";

                cout << "Player 1: X\n";
                cout << "Player 2: O\n";
                cout << "\n";

                cout << "     |     |       \n";
                cout << "  1  |  2  |  3    \n";
                cout << "_____|_____|_____  \n";
                cout << "     |     |       \n";
                cout << "  4  |  5  |  6    \n";
                cout << "_____|_____|_____  \n";
                cout << "     |     |       \n";
                cout << "  7  |  8  |  9    \n";
                cout << "     |     |       \n";
                cout << "\n";

                cout << "Above is the example of what the grid is going to look like when you play\n";
                cout << "Each player must select a numer 1-9 to put there X or O there\n";

            }

            void board_make() {

                std::cout << "     |     |      \n";

                std::cout << "  " << grid[0] << "  |  " << grid[1] << "  |  " << grid[2] << "\n";

                std::cout << "_____|_____|_____ \n";
                std::cout << "     |     |      \n";

                std::cout << "  " << grid[3] << "  |  " << grid[4] << "  |  " << grid[5] << "\n";

                std::cout << "_____|_____|_____ \n";
                std::cout << "     |     |      \n";

                std::cout << "  " << grid[6] << "  |  " << grid[7] << "  |  " << grid[8] << "\n";
                std::cout << "     |     |      \n";


            }

            void win_condition() {//its in the name it checks for wins 



                //rows
                if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
                    win(0);
    
                }
                else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
                    win(3);
                }
                else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
                    win(6);
                }

                // columns
                if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
                    win(0);
                }
                else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
                    win(1);
                }
                else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
                    win(2);
                }

                //across
                if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
                    win(0);
                }
                else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
                    win(2);
                }
            }

            void game_start() {

                for (int i = 0; i < 5; i ++) {//iterates through both players turns till after the 
                    //Player 1's turn
                    cout << "\n";
                    cout << "Player 1 its your turn please enter 1-9 to select your choice: "; cin >> player1; convert(); cout << "\n";
                    board_make();
                    win_condition();
                    cout << "\n";
                    //Player 2's turn
                    cout << "\n";
                    cout << "Player 2 its now your turn select your choice 1-9: "; cin >> player2; convert();  cout << "\n";
                    board_make();
                    win_condition();
                    cout << "\n";
            }
        }

        void convert() {
                for (int i = 0; i < 8; i++) {
                    //checks if the spot is filled if it is filled it sets filled to true
                    if ((grid[i] == "X" || grid[i] == "O") && grid[i] != " ") {
                        filled = true;
                    }
                    else if (grid[i] == " "){
                        filled = false;
                    }

                    if (filled == true && player1 == i + 1 && grid[i] != " ") {//if filled is true and player choice is a option that was filled then it outputs whats below
                        cout << "Looks like that spot is already taken, please select another space\n";
                    }
    
                    if (filled == true && player2 == i + 1 && grid[i] != " ") {//if filled is true and player choice is a option that was filled then it outputs whats below
                        cout << "Looks like that spot is already taken, please select another space\n";
                    }

                    //converting the player choice 1-9 to a X or O
                    if (filled == false && player1 == i + 1 && player2 != i + 1) {
                        grid.at(i) = "X";
                    }

                    if (filled == false && player2 == i + 1 && player1 != i + 1) {
                        grid.at(i) = "O";
                    }
                }
            }

            void win(int num) {
                if (grid[num] == "X") {
                    cout << who_Won1 << " Won\n";
                    exit(0);
                }
                else if (grid[num] == "O") {
                    cout << who_Won2 << " Won\n";
                    exit(0);
                }
            }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shractic
  • 1
  • 1