Please write a tic tac toe game using c++ language and 1D arrays, thank you!
The following solution is suggested to handle the subject “Please write a tic tac toe game using c++ language and 1D arrays, thank you!“. Let’s keep an eye on the content below!
Question “Please write a tic tac toe game using c++ language and 1D arrays, thank you!”
Please write a tic tac toe game using c++ language and 1D
arrays, thank you!
Answer
Answer 1
====================================================
Program
====================================================
#include <iostream>
#include <string>
using namespace std;
int checkwin(char square[]);
void board(string player1Name,string player2Name,char
square[]);
bool validateUseInput(char input[]);
bool validatePlayerName(string playerName);
int main()
{
char userChoice;
do
{
int player = 1,i,choice;
char square[9] =
{’0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8’};
string player1Name;
string player2Name;
cout<<“Enter Player 1
Name:\n”;
while(true)
{
cin>>player1Name;
bool
valid = validatePlayerName(player1Name);
if(valid)
break;
else
{
cout<<“–Invalid Player Name , Must be
characters only –\n”;
cout<<“–Please Re-Enter–\n”;
}
}
cout<<“Enter Player 2
Name:\n”;
while(true)
{
cin>>player2Name;
bool
valid = validatePlayerName(player2Name);
if(valid)
break;
else
{
cout<<“–Invalid Player Name , Must be
characters only –\n”;
cout<<“–Please Re-Enter–\n”;
}
}
char mark;
do
{
board(player1Name,player2Name,square);
player=(player%2)?1:2;
if(player==1)
cout << “Player ” << player1Name
<< “, enter a number: “;
else
if(player==2)
cout << “Player ” << player2Name
<< “, enter a number: “;
char
input[10];
while(true)
{
cin >> input;
bool valid = validateUseInput(input);
if(valid==true)
break;
}
choice =
input[0]-48;
mark=(player ==
1) ? ‘X’ : ‘O’;
if (choice == 0
&& square[0] == ‘0’)
square[0] = mark;
else if (choice
== 1 && square[1] == ‘1’)
square[1] = mark;
else if (choice
== 2 && square[2] == ‘2’)
square[2] = mark;
else if (choice
== 3 && square[3] == ‘3’)
square[3] = mark;
else if (choice
== 4 && square[4] == ‘4’)
square[4] = mark;
else if (choice
== 5 && square[5] == ‘5’)
square[5] = mark;
else if (choice
== 6 && square[6] == ‘6’)
square[6] = mark;
else if (choice
== 7 && square[7] == ‘7’)
square[7] = mark;
else if (choice
== 8 && square[8] == ‘8’)
square[8] = mark;
else
{
cout<<“Invalid move “;
player–;
}
i=checkwin(square);
player++;
}while(i==-1);
board(player1Name,player2Name,square);
if(i==1)
{
int
playerNumber= –player;
if(playerNumber==1)
cout<<“==>\aPlayer
“<<player1Name<<” win “;
else
if(playerNumber==2)
cout<<“==>\aPlayer
“<<player2Name<<” win “;
}
else
cout<<“==>\aGame draw”;
cout<<endl;
cout<<“Do you want to
continue press y or Y :”;
cin>>userChoice;
}while(userChoice==’y’||userChoice==’Y’);
cout<<“Bye\n”;
return 0;
}
/*********************************************
FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH RESULT
-1 FOR GAME IS IN PROGRESS
O GAME IS OVER AND NO RESULT
**********************************************/
int checkwin(char square[])
{
if (square[0] == square[1] && square[1] ==
square[2])
return 1;
else if (square[3] == square[4] && square[4]
== square[5])
return 1;
else if (square[6] == square[7] && square[7]
== square[8])
return 1;
else if (square[0] == square[3] && square[3]
== square[6])
return 1;
else if (square[1] == square[4] && square[4]
== square[7])
return 1;
else if (square[2] == square[5] && square[5]
== square[8])
return 1;
else if (square[0] == square[4] && square[4]
== square[8])
return 1;
else if (square[2] == square[4] && square[4]
== square[6])
return 1;
else if (square[0] != ‘0’ && square[1] != ‘1’
&& square[2] != ‘2’ && square[3] != ‘3’
&& square[4] != ‘4’ && square[5] != ‘5’ &&
square[6] != ‘6’
&& square[7] != ‘7’ && square[8] != ‘8’)
return 0;
else
return -1;
}
/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/
void board(string player1Name,string player2Name,char
square[])
{
cout << “\n\n\tTic Tac Toe\n\n”;
cout << “Player “<<player1Name<<”
(X) – Player “<<player2Name<<” (O)” << endl
<< endl;
cout << endl;
cout << ” | | ” << endl;
cout << ” ” << square[0] << ” | ”
<< square[1] << ” | ” << square[2] <<
endl;
cout << “_____|_____|_____” <<
endl;
cout << ” | | ” << endl;
cout << ” ” << square[3] << ” | ”
<< square[4] << ” | ” << square[5] <<
endl;
cout << “_____|_____|_____” <<
endl;
cout << ” | | ” << endl;
cout << ” ” << square[6] << ” | ”
<< square[7] << ” | ” << square[8] <<
endl;
cout << ” | | ” << endl <<
endl;
}
bool validateUseInput(char input[])
{
int inputLength = strlen(input);
if(inputLength>1)
{
cout<<“—-Invalid input
Please enter valid input—-\n”;
return false;
}
else if(input[0]>=48 &&
input[0]<57)
{
return true;
}else{
cout<<“—-Invalid input
Please enter valid input—-\n”;
return false;
}
}
bool validatePlayerName(string playerName)
{
bool isValid=true;
for(int i=0;i<playerName.length();i++)
{
if(!((playerName[i]>=65
&& playerName[i]<=90) || (playerName[i]>=97
&& playerName[i]<=122)))
{
isValid=false;
break;
}
}
return isValid;
}
Answer 2
{void get_input (char []); void showBoard (char [], int, int); void winning (char [], int, int); int main () void get_input void winBoard (char[], int. int]); int main() playerX(0), PlayerO(0); char board[] = 1,2, 3, 4, 5, 6, 7, 8, 9, 9; cout "This game of tic-tac toe. You will choose your selection by" << " entering the corresponding number (1-9) on the gameboard.\n\n"; showBoard (gameboard, playerX, playerO); cout << "\nPlayer X will select first.\n"; get_input (gameboard); winning (gameboard, playerX, playerO); return 0; void get_input (char gameboard[]) { for (int i = 1; i <= 9; i++) You can choose which player you want to play by entering the number (1-9) on your gameboard. Cin >> playerX; gameboard, playerX, and playerO; cout "What's your choice player O?" "; cin >> playerO; showBoard (gameboard, playerX, playerO); } } void showBoard (char a[], int playerX, int playerO) cout << '1' << " " << '2' << " " << '3' << endl; cout << '4' << " " << '5' << " " << '6' << endl; cout << '7' << " " << '8' << " " << '9' << endl; void winning (char gameboard[], int playerX, int playerO) { cout << "You won player X! "; }
Conclusion
Above is the solution for "Please write a tic tac toe game using c++ language and 1D arrays, thank you!". We hope that you find a good answer and gain the knowledge about this topic of engineering.