Write a C++ program that plays a simple game of TicTacToe against a user. The us
ID: 3853607 • Letter: W
Question
Write a C++ program that plays a simple game of TicTacToe against a user. The user will input the "X"s and the computer will counter with "O"s. The program will update the display after each user input and each computer input. The program will end after someone wins the game (all "X"s or all "O"s in a row) or all 9 spaces are played.
** Below is a screenshot of my main function. I already have this program 99% finished. I am just trying to add User Input Validation. I was thinking about changing my while loop to a do while loop possibly, somewhere between lines 39-41, or maybe using a "try" statement or For Loop? I just need some help tweaking this just a little. The Input Validation should display an error message if the user enters a value other than 0-8 to make a move on the Tic Tac Toe Display, and then re-prompt them to enter a number 0-8. It should say something like: "That Is Not A Valid Move. Please Enter A Number [0-8]. "
domanip> E#include 10 11 #include #include #include using namespace std; 14 16 void Display(int]) 17 void compMove(int) 18 char Winner(intl); 19 20 int main) bool play«Game true; const int spaces9; int num[spaces] = { -1, -1, -1, -1, -1, -1, .1, -1, -1 }; int x; int numMoves; char input; 24 25 28 while (playGame) 36 31 32 i++) for (int i a; i num[i-1: spaces; while (1) 34 35 36 37 38 39 Display (num) coutExplanation / Answer
Most probably you can change while into do while.
Please find the modified loop alone below.
do{
for(int i=0;i<space;i++){
num[i] = -1;
}
while(1){
Display(num);
cout<< " Please enter a posistion number:";
cin>>x;
cout<<endl;
num[x] =1;
Display(num);
compMove(num);
cout<<" The computer Has chosen. "<<endl;
if(Winner(num) == 'X'){
cout<<" Congratulations,you Won! "<<endl;
break;
}
else if(Winner(num) =='0'){
cout<<"Sorry, The Computer Wins. " <<endl;
break;
}
else if(Winner(num) =='T){
cout<<"Sorry, It's a Draw! " <<endl;
break;
}
}
cout<<" Do you want to Play Again?[y or n] ";
cin.ignore();
cin>>input;
cout<<endl;
if(input =='y' || input == 'Y')
playGame = true;
else
playGame = false;
}while(playGame);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.