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);
}
}
}
}