Please write a tic tac toe game using c++ language and 1D arrays, thank you!
====================================================
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;
}
void get_input (char []); void showBoard (char [], int, int); void winning (char [], int, int); int main () { int playerX(0), playerO(0); char gameboard[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cout << "This is a 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++) { int playerX(0), playerO(0); cout << "What is your choice player X? "; cin >> playerX; showBoard (gameboard, playerX, playerO); cout << "What is 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!"; }
Please write a tic tac toe game using c++ language and 1D arrays, thank you!
Write a Tic-Tac-Toe game in C language. Your project must contain at least one header file, two C source files.
1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle
Tic tac toe game C++ programing using classes, objects, arays
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
what’s the code of game Tic-tac-toe by using the Alpha Beta Turing algorithm ? The code in language JavaScript?
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
Hey guys, I need help writing a Tic-Tac-Toe game programmed in Java. The game has to be a 1D (One-Dimension) array board NOT 2D. It has to be a human plays against the computer type of Tic-Tac-Toe. Here is the requirements for the program. PLEASE NO COPY AND PASTE ANSWER. Thank you in advance! You will develop a program in which a human plays against the computer. 1. Validate user input at every opportunity. a. Do not allow number entries...
Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...
PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...