Need Help with Tic Tac Toe Game

My code isn't out putting when someone wins even though they get 3 in a row. I think there is something wrong with my function bool hasWon(string boardPos[]);. I'm new to c++, so i would appericate if someone would help with this.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;



bool hasWon(string boardPos[]);
void turn(int player, string boardPos[]);
void printGame(string boardPos[], int size, string name1, string name2);
int main()
{
string boardPos[9]= {"1", "2", "3", "4", "5", "6", "7", "8" ,"9"}; //initialize array with preset values
int size = 9; //initialize size of array
int count= 0;
string player1, player2; //initializing variables

cout<<"Enter your player names."<<endl; //prompts users for player names
cout<<"Player 1: ";
cin>>player1; //user input
cout<<"Player 2: ";
cin>>player2;
cout<<"Choose a position on the board from 1-9"<<endl; //prompts user for position on board
system("cls"); //clears previous clutter
printGame(boardPos, size, player1, player2); //prints a blank board

while((count<=9) && !hasWon(boardPos)) //while board is not full or a player has not won
{
cout<<player1<<": ";
turn(1, boardPos);
cout<<endl;
system("cls");
printGame(boardPos, size, player1, player2);
count++;

cout<<player2<<": ";
turn(2, boardPos);
cout<<endl;
system("cls");
printGame(boardPos, size, player1, player2);
count++;
}

if(count<=9)
{
cout<<"Cat's Game! No one wins"<<endl;
}
system("pause");
return 0;
}

void turn(int player, string boardPos[])
{
bool turnOver = false;
int spot;
while(!turnOver)
{
cin>>spot;
if(player == 1)
{
if(boardPos[spot-1] == "X" || boardPos[spot-1] == "O")
{
cout<<"Invalid Move, try again."<<endl;
}
else if(spot > 9)
{
cout<<"Invalid Move, try again."<<endl;
}
else
{
boardPos[spot-1] = "X";
turnOver = true;
}
}
if(player == 2)
{
if(boardPos[spot-1] == "X" || boardPos[spot-1] == "O")
{
cout<<"Invalid Move, try again."<<endl;
}
else if(spot > 9)
{
cout<<"Invalid Move, try again."<<endl;
}
else
{
boardPos[spot-1] = "O";
turnOver = true;
}
}
}
}

void printGame(string boardPos[], int size, string name1, string name2)
{
cout<<"Player 1: "<<name1<<endl;
cout<<"Player 2: "<<name2<<endl;
cout<<endl;
cout<<boardPos[6]<<setw(8)<<boardPos[7]<<setw(8)<<boardPos[8]<<endl;
cout<<endl;
cout<<boardPos[3]<<setw(8)<<boardPos[4]<<setw(8)<<boardPos[5]<<endl;
cout<<endl;
cout<<boardPos[0]<<setw(8)<<boardPos[1]<<setw(8)<<boardPos[2]<<endl;



}
bool hasWon(string boardPos[])
{
if((boardPos[0] == "X") && (boardPos[1] == "X") && (boardPos[2] == "X") ||
(boardPos[3] == "X") && (boardPos[4] == "X") && (boardPos[5] == "X") ||
(boardPos[6] == "X") && (boardPos[7] == "X") && (boardPos[8] == "X") ||
(boardPos[0] == "X") && (boardPos[3] == "X") && (boardPos[6] == "X") ||
(boardPos[1] == "X") && (boardPos[4] == "X") && (boardPos[7] == "X") ||
(boardPos[2] == "X") && (boardPos[5] == "X") && (boardPos[8] == "X") ||
(boardPos[2] == "X") && (boardPos[4] == "X") && (boardPos[6] == "X") ||
(boardPos[0] == "X") && (boardPos[4] == "X") && (boardPos[8] == "X"))
return true;
else if((boardPos[0] == "O") && (boardPos[1] == "O") && (boardPos[2] == "O") ||
(boardPos[3] == "O") && (boardPos[4] == "O") && (boardPos[5] == "O") ||
(boardPos[6] == "O") && (boardPos[7] == "O") && (boardPos[8] == "O") ||
(boardPos[0] == "O") && (boardPos[3] == "O") && (boardPos[6] == "O") ||
(boardPos[1] == "O") && (boardPos[4] == "O") && (boardPos[7] == "O") ||
(boardPos[2] == "O") && (boardPos[5] == "O") && (boardPos[8] == "O") ||
(boardPos[2] == "O") && (boardPos[4] == "O") && (boardPos[6] == "O") ||
(boardPos[0] == "O") && (boardPos[4] == "O") && (boardPos[8] == "O"))
return true;
else
return false;
}
Last edited on
Topic archived. No new replies allowed.