Hello,
I am working on my final and I am stuck right near the end of the the program. I am making a Tic-Tac-Toe game and I can't seem to figure out how to make the program ask if you would like to play again, and keep the same names for the players that just played, keep track of the player that wins, and display the number of wins said player has, after accepting to keep playing.
I am wanting the scores to be next to the names of the players after the :
Thanks for the help! Much appreciated.
The variables int player1Score; and int player2Score; were the names I was trying to use. Feel free to change them to what you want.
Here is the code I have so far:
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int winner();
void board();
void clearGame();
int i;
char symbol;
char box[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
int player = 1;
int playersChoice;
int player1Score;
int player2Score;
string player1;
string player2;
int winner()
{
if (box[1] == box[2] && box[2] ==
box[3])
{
return 1;
}
else if (box[4] == box[5] && box[5] ==
box[6])
{
return 1;
}
else if (box[7] == box[8] && box[8] ==
box[9])
{
return 1;
}
else if (box[1] == box[4] && box[4] ==
box[7])
{
return 1;
}
else if (box[2] == box[5] && box[5] ==
box[8])
{
return 1;
}
else if (box[3] == box[6] && box[6] ==
box[9])
{
return 1;
}
else if (box[1] == box[5] && box[5] ==
box[9])
{
return 1;
}
else if (box[3] == box[5] && box[5] ==
box[7])
{
return 1;
}
else if (box[1] != '1' && box[2] != '2'
&& box[3] != '3' && box[4] != '4'
&& box[5] != '5' &&
box[6] != '6' && box[7] != '7' && box[8] !=
'8'
&& box[9] != '9')
{
return 0;
}
else
{
return -1;
}
}
void clearGame()
{
for (int i = 0; i < 10; i++)
box[i] = '0' + i;
}
void board()
{
system("cls");
cout << "\n";
cout << "\t\t\t\tTic Tac Toe";
cout << "\n";
cout << "\t\tPlayer 1: " << player1
<< " (X) : Player 2: " << player2 << " (O) : "
<< endl << endl;
cout << endl;
cout << "\t\t\t _ _ " << endl;
cout << "\t\t\t |_| |_| " << endl;
cout << "\t\t\t " << box[1] << " |_|
" << box[2] << " |_| " << box[3] <<
endl;
cout << "\t\t\t _____|_|_____|_|_____"
<< endl;
cout << "\t\t\t|__|__|_|__|__|_|__|__|" <<
endl;
cout << "\t\t\t |_| |_| " << endl;
cout << "\t\t\t " << box[4] << " |_| " << box[5] << " |_| " << box[6] << endl;
cout << "\t\t\t _____|_|_____|_|_____"
<< endl;
cout << "\t\t\t|__|__|_|__|__|_|__|__|" <<
endl;
cout << "\t\t\t |_| |_| " << endl;
cout << "\t\t\t " << box[7] << " |_| " << box[8] << " |_| " << box[9] << endl;
cout << "\t\t\t |_| |_| " << endl
<< endl;
}
int main()
{
char answer;
cout << "Welcome to Tic Tac Toe! \n\n"
<< endl << endl;
do
{
clearGame();
cout << "Would you like to
play? [Y/N]" << endl << endl;
cin >> answer;
if (answer == 'Y')
{
cout <<
"Enter name for Player 1: ";
cin >>
player1;
cout <<
endl;
cout <<
"\nEnter name for Player 2: ";
cin >>
player2;
cout <<
endl;
cout <<
"Player 1: " << player1 << endl;
cout <<
"Player 2: " << player2 << endl;
do
{
board();
player = (player % 2) ? 1 : 2;
cout << " Player " << player
<< " your turn! Choose where you want to place your
marker.";
cin >> playersChoice;
symbol = (player == 1) ? 'X' : 'O';
if (playersChoice == 1 && box[1] ==
'1')
box[1] = symbol;
else if (playersChoice == 2 && box[2] ==
'2')
box[2] = symbol;
else if (playersChoice == 3 && box[3] ==
'3')
box[3] = symbol;
else if (playersChoice == 4 && box[4] ==
'4')
box[4] = symbol;
else if (playersChoice == 5 && box[5] ==
'5')
box[5] = symbol;
else if (playersChoice == 6 && box[6] ==
'6')
box[6] = symbol;
else if (playersChoice == 7 && box[7] ==
'7')
box[7] = symbol;
else if (playersChoice == 8 && box[8] ==
'8')
box[8] = symbol;
else if (playersChoice == 9 && box[9] ==
'9')
box[9] = symbol;
else
{
cout << "\aINVALID
MOVE";
player--;
cin.ignore();
cin.get();
}
i = winner();
player++;
} while (i ==
-1);
{
board();
if (i == 1)
{
cout << "Player "
<< --player << " wins!\n" << endl;
}
else if (i == 0)
{
cout << " Draw!";
cin.ignore();
cin.get();
return 0;
}
else
{
cout << "\aInvalid
Response";
cout << "Closing..."
<< endl;
return 0;
}
}
}
else if (answer == 'N')
{
exit(0);
}
else if (answer != 'N' ||
'Y')
cout <<
"Invalid input\n" << endl;
cin.ignore();
}
while (answer == 'N' || 'Y');
system("pause");
return 0;
}
Updated code
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int winner();
void board();
void clearGame();
int i;
char symbol;
char box[10] = { '0','1', '2', '3', '4', '5', '6', '7', '8', '9'
};
int player = 1;
int playersChoice;
int player1Score;
int player2Score;
string player1;
string player2;
int winner()
{
if (box[1] == box[2] && box[2] == box[3])
{
return 1;
}
else if (box[4] == box[5] && box[5] == box[6])
{
return 1;
}
else if (box[7] == box[8] && box[8] == box[9])
{
return 1;
}
else if (box[1] == box[4] && box[4] == box[7])
{
return 1;
}
else if (box[2] == box[5] && box[5] == box[8])
{
return 1;
}
else if (box[3] == box[6] && box[6] == box[9])
{
return 1;
}
else if (box[1] == box[5] && box[5] == box[9])
{
return 1;
}
else if (box[3] == box[5] && box[5] == box[7])
{
return 1;
}
else if (box[1] != '1' && box[2] != '2' &&
box[3] != '3' && box[4] != '4'
&& box[5] != '5' && box[6] != '6' && box[7]
!= '7' && box[8] != '8'
&& box[9] != '9')
{
return 0;
}
else
{
return -1;
}
}
void clearGame()
{
for (int i = 0; i < 10; i++)
box[i] = '0' + i;
}
void board()
{
system("cls");
cout << "\n";
cout << "\t\t\t\tTic Tac Toe";
cout << "\n";
cout << "\t\tPlayer 1: " << player1 << " (X) :
Player 2: " << player2 << " (O) : " << endl
<< endl;
cout << "\t\tPlayer 1 Score : " << player1Score
<< " : Player 2 Score: " << player2Score << endl
<< endl;
cout << endl;
cout << "\t\t\t [" << box[1] << "] [" <<
box[2] << "] [" << box[3]
<<"]"<<endl;
cout << "\t\t\t [" << box[4] << "] [" <<
box[5] << "] [" << box[6]
<<"]"<<endl;
cout << "\t\t\t [" << box[7] << "] [" <<
box[8] << "] [" << box[9]
<<"]"<<endl;
cout<<"\n";
}
int main()
{
char answer;
player1Score=0;
player2Score=0;
cout << "Welcome to Tic Tac Toe! \n\n" <<
endl << endl;
cout << "Enter name for Player 1: ";
cin >> player1;
cout << endl;
cout << "Enter name for Player 2: ";
cin >> player2;
cout << endl;
cout << "Player 1: " << player1
<< endl;
cout << "Player 2: " << player2 <<
endl;
do
{
cout << "Would you like to
play? [Y/N]" << endl << endl;
cin >> answer;
if (answer == 'Y')
{
clearGame();
do
{
board();
player = (player % 2) ? 1 : 2;
cout << " Player " << player
<< " your turn! Choose where you want to place your
marker.";
cin >> playersChoice;
symbol = (player == 1) ? 'X' : 'O';
if (playersChoice == 1 && box[1] ==
'1')
box[1] = symbol;
else if (playersChoice == 2 && box[2] ==
'2')
box[2] = symbol;
else if (playersChoice == 3 && box[3] ==
'3')
box[3] = symbol;
else if (playersChoice == 4 && box[4] ==
'4')
box[4] = symbol;
else if (playersChoice == 5 && box[5] ==
'5')
box[5] = symbol;
else if (playersChoice == 6 && box[6] ==
'6')
box[6] = symbol;
else if (playersChoice == 7 && box[7] ==
'7')
box[7] = symbol;
else if (playersChoice == 8 && box[8] ==
'8')
box[8] = symbol;
else if (playersChoice == 9 && box[9] ==
'9')
box[9] = symbol;
else
{
cout << "\aINVALID
MOVE";
player--;
cin.ignore();
cin.get();
}
i = winner();
player++;
} while (i ==
-1);
board();
if (i ==
1)
{
cout << "Player " << --player
<< " wins!\n" << endl;
if(player==1)
player1Score++;
else
player2Score++;
}
else if (i ==
0)
{
cout << " Draw!";
cin.ignore();
cin.get();
return 0;
}
else
{
cout << "\aInvalid Response";
cout << "Closing..." << endl;
return 0;
}
}
else if (answer == 'N')
{
exit(0);
}
else if (answer != 'N' ||
'Y')
cout << "Invalid input\n" << endl;
cin.ignore();
}while (answer == 'N' || 'Y');
cout<<"Player 1 :"<<player1<<"'s
score is: "<<player1Score<<endl;
cout<<"Player 2 :"<<player2<<"'s
score is: "<<player2Score<<endl<<endl;
system("pause");
return 0;
}
output



If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
Hello, I am working on my final and I am stuck right near the end of...
Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...
I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe the missing portion. As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. // TicTacToe.cpp: Follow along with...
Hello, I am working on a project for my C++ class, I have the vast majority of the code figured out but am running into a few issues. Mainly updating each * to an X. The problem is as follows:(Airplane Seating Assignment) Write a program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with six seats in each row. Rows 1 and 2 are first class, rows 3 through 7 are business...
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...
This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...
Can someone help me to convert from c++ to C program int main() { srand(time(0)); int playAgain; int PlayerScore=0; int computerScore= 0; int ties= 0; do { system("CLS"); Firstturn = rand()%(2-1+1)+1;// starting person. ComputerRandomPosition= rand()%(9-1+1)+1;// computer first pick- random gameWin=3; block1= '1'; block2= '2'; block3= '3'; block4= '4'; block5= '5'; block6= '6'; block7= '7'; block8= '8'; block9= '9'; //start of program cout<<"Welcome to Tic Tac Toe Game"<<endl<endl; cout<<"Player:"<<playerScore<<"Computer:"<<computerScore<<"Ties:"<<ties<<endl; if(Firstturn==1)// player first { cout<<"Please Select your a grid to place (X):"<<endl<<endl;...
Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...
Trying to figure out how to complete my fourth case 4 (option exit), write a function that display a “Good Bye” message and exits/log out from user’s account displaying a menu (sign in, balance, withdraw and exit.. #include<iostream> #include <limits> using namespace std; void printstar(char ch , int n); double balance1; int main() { system("color A0"); cout<<"\n\t\t ========================================="<< endl; cout<<"\t\t || VASQUEZ ATM SERVICES ||"<< endl; cout<<"\t\t ========================================\n\n"<< endl; int password; int pincode ; cout<<" USERS \n"; cout<<" [1] -...
My if/else statement wont run the program that I am calling. The menu prints but once I select a number the menu just reprints, the function called wont run. What can I do to fix this probelm? #include <iostream> #include "miltime.h" #include "time.h" #include "productionworker.h" #include "employee.h" #include "numdays.h" #include "circle.h" using namespace std; int main() { int select=1; while (select>0) { cout << "Option 1:Circle Class\n"<< endl; cout << "Option 2:NumDay Class\n" << endl; cout <<"Option 3:Employee and Production...
Am I using the right cin statement to take in values for my array? If I print them out it prints "0x61fea0" with the input 1 2 3 4 5 ///////////////////////////////// #include <iostream> using namespace std; const int ARRAY_LENGTH = 5; int main(){ int numbers[ARRAY_LENGTH]; int threshold; //@todo prompt user to enter array values cout << "Please input 5 numbers: " << endl; for(int i = 0; i < ARRAY_LENGTH; i++){ //@todo add cin statement to read in values for...