Question: My tictactoe.cpp program was able to pass the first test as shown in picture, but not the second test. Please help. Thank you! 1 #include

My tictactoe.cpp program was able to pass the first test as shown in picture, but not the second test. Please help. Thank you!
1 #include
#include
using namespace std;
/// @brief Draws the provided tic-tac-toe board to the screen
void drawBoard(const vector& gameBoard){
for (int i =0; i 9; i +=3){
cout " gameBoard.at(i)|" gameBoard.at(i +1)"|" gameBoard.at(i +2) endl;
if (i6){
}
cout "-----|-----|-----" endl;
}
cout endl;
}
/// @brief Fills vector with characters starting at lower case a.
void initVector(vector& v){
char c ='a';
for (int i =0; i v.size(); ++i){
v[i]= c;
C++;
}
}
/// @brief Converts a character representing a cell to associated vector index
int convertPosition(char boardPosition){
return boardPosition -'a';
}
/// @brief Checks if a spot in the board is available
bool validPlacement(const vector& gameBoard, int positionIndex){
return positionIndex >=0&& positionIndex 9 &&
gameBoard[positionIndex]!='x' && gameBoard[positionIndex]!='0';
}
/// @brief Acquires a play from the user as to where to put their mark
int getPlay(const vector& gameBoard, int player){
char boardPosition;
int positionIndex;
while (true){
// Prompt for input
cout "Please choose a position: ";
cin >> boardPosition;
// Validate input range
if (boardPosition 'a'|| boardPosition >'i'){
}
continue;
// Convert position to index
positionIndex = convertPosition(boardPosition);
// Check if the position is valid (not already occupied)
if (!validPlacement(gameBoard, positionIndex)){
cout "Please choose a position." endl;
continue;
}
// Input is valid, break the loop
break;
// Input is valid, break the loop
}
break;
}
return positionIndex;
/// gbrief Checks if the game has been won
bool gameNon(const vectorechar`& ganeBoard){
// Check rows
for (int i =0; i 9; i +=3){
if (gameBoard[i]= gameBoard[i + l] && gameBoard[i +1]== ganeBoard[i +2]){
return true;
}
}
// Check colunns
for (int i =0; i 3; ++i){
if (gameBoard[i]= gameBoard[i +3] && gameBoard[i +3]== ganeBoard[i +6]){
return true;
}
}
// Check diagonals
if ((gameBoard[0]== ganeBoard[4] && ganeBoard[4]= ganeBoard[8])||
(gameBoard[2]= ganeBoard[4] && ganeBoard[4]= ganeBoard[6])){
}
return true;
return false;
}
/// gbrief Checks if the board is full
bool boardFull(const vectorechar`& ganeBoard){
for (char cell : gameBoard){
if (cell !='x' && cell !='o'){
return false;
}
}
}
return true;
// Global constants for player representation
const int PLAYER1=0;
const int PLAYER2=1;
/// Main function
int main(){
vectorechar> ganeBoard(9);
int curPlay;
int whosTurn = PLAYERl; // Player 1 always goes first and is 'X'
// Initialize board to enpty state
initVector(ganeBoard);
// Display the empty board
drawBoard(ganeBoard);
// Play until gane is over
while (!ganelon(gameBoard) && !boardFull(ganeBoard)){
// Get a play
curPlay = getPlay(ganeBoard, whosTurn);
// Set the play on the board
ganeBoard[curPlay]=(whosTurn = PLAYERl)?'x' : 'o';
// Switch the turn to the other player
whosTurn =(whosTurn = PLAYER1)? PLAYER2 : PLAYER1;
// Output the updated board
}
dravBoard(ganeBoard);
// Deternine winner and output appropriate nessage
if (ganellon(gameBoard)){
int winner =(whosTurn = PLAYER1)? PLAYER2 : PLAYER1;
cout "Player " winner +1" wins!!" endl;
}
else {
cout "No one wins." endl;
}
return \theta;
}
Running tictactoe.cpp
Please choose a position: a
Please choose a position: b
Please choose a position: c
Please choose a position: d
Please choose a position: e
Please choose a position: f
Please choose a position: g
Player 1 wins!!
pass
Tebt 2
Input:
My tictactoe.cpp program was able to pass the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!