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!
#include
#include
using namespace std;
@brief Draws the provided tictactoe board to the screen
void drawBoardconst vector& gameBoard
for int i ; i ; i
cout gameBoard.ati gameBoard.ati gameBoard.ati endl;
if i
cout endl;
cout endl;
@brief Fills vector with characters starting at lower case a
void initVectorvector& v
char c a;
for int i ; i vsize; i
vi c;
C;
@brief Converts a character representing a cell to associated vector index
int convertPositionchar boardPosition
return boardPosition a;
@brief Checks if a spot in the board is available
bool validPlacementconst vector& gameBoard, int positionIndex
return positionIndex && positionIndex &&
gameBoardpositionIndexx && gameBoardpositionIndex;
@brief Acquires a play from the user as to where to put their mark
int getPlayconst 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 convertPositionboardPosition;
Check if the position is valid not already occupied
if validPlacementgameBoard 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 gameNonconst vectorechar& ganeBoard
Check rows
for int i ; i ; i
if gameBoardi gameBoardi l && gameBoardi ganeBoardi
return true;
Check colunns
for int i ; i ; i
if gameBoardi gameBoardi && gameBoardi ganeBoardi
return true;
Check diagonals
if gameBoard ganeBoard && ganeBoard ganeBoard
gameBoard ganeBoard && ganeBoard ganeBoard
return true;
return false;
gbrief Checks if the board is full
bool boardFullconst vectorechar& ganeBoard
for char cell : gameBoard
if cell x && cell o
return false;
return true;
Global constants for player representation
const int PLAYER;
const int PLAYER;
Main function
int main
vectorechar ganeBoard;
int curPlay;
int whosTurn PLAYERl; Player always goes first and is X
Initialize board to enpty state
initVectorganeBoard;
Display the empty board
drawBoardganeBoard;
Play until gane is over
while ganelongameBoard && boardFullganeBoard
Get a play
curPlay getPlayganeBoard whosTurn;
Set the play on the board
ganeBoardcurPlaywhosTurn PLAYERlx : o;
Switch the turn to the other player
whosTurn whosTurn PLAYER PLAYER : PLAYER;
Output the updated board
dravBoardganeBoard;
Deternine winner and output appropriate nessage
if ganellongameBoard
int winner whosTurn PLAYER PLAYER : PLAYER;
cout "Player winner 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 wins!!
pass
Tebt
Input:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
