Tic tac toe game C++ programing using classes, objects, arays
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
#include <iostream>
#include <iomanip>
using namespace std;
class ticTacToe{
char b[3][3]; //two dimenstional array
public:
ticTacToe(); //constructor
void board(); //prints the game board
void Input_p1(int); //takes the input from the user
bool Checker(); //checks the winner
};
int main()
{
ticTacToe game; //creating object
game.board(); //display the game board
for (int i = 1; i <= 9; i++) //takes the game input from the
user
{
game.Input_p1(i);
if (game.Checker())
break;
if (i + 1 == 10)
{
cout << "NO one WUNS\n";
break;
}
i++;
game.Input_p1(i);
if (game.Checker())
break;
if (i+1 == 10)
cout << "NO one WUNS\n";
}
system("pause");
}
ticTacToe::ticTacToe() //constructor to intialize the 3by3
array
{
b[0][0] = '1';
b[0][1] = '2';
b[0][2] = '3';
b[1][0] = '4';
b[1][1] = '5';
b[1][2] = '6';
b[2][0] = '7';
b[2][1] = '8';
b[2][2] = '9';
}
void ticTacToe::board()//function to create the game board
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
cout << " "<<b[i][j];
if (j <= 1)
cout << " |";
else
cout << endl;
if (j == 2&&i<2)
cout << "-----------" << endl;
}
}
void ticTacToe::Input_p1(int a) //function that takes input from
the user
{
int n, temp = 0;
char c;
if (a == 1 || a == 3 || a == 5 || a == 7 || a == 9)
{
cout << "Player 'X' Turn" << endl;
c = 'X';
}
else
{
cout << "Player 'O' Turn" << endl;
c = 'O';
}
cout << "Enter the Box no :";
do{ // loop to take the box number AGAIN if user enters in correct
number
if (temp == 1)
cout << " Again Enter the box no:";
while (!(cin >> n))
{
cin.clear();
cin.ignore(900, '\n');
cout << " Invalid!\n Again Enter the box no :";
}
if (n < 0 || n>9)
temp = 1;
switch (n)
{
case 1:
if (b[0][0] == 'X'||b[0][0]=='O')
temp = 1;
else
{
b[0][0] = c;
temp = 0;
}
break;
case 2:
if (b[0][1] == 'X' || b[0][1] == 'O')
temp = 1;
else
{
b[0][1] = c;
temp = 0;
}
break;
case 3:
if (b[0][2] == 'X' || b[0][2] == 'O')
temp = 1;
else
{
b[0][2] = c;
temp = 0;
}
break;
case 4:
if (b[1][0] == 'X' || b[1][0] == 'O')
temp = 1;
else
{
b[1][0] = c;
temp = 0;
}
break;
case 5:
if (b[1][1] == 'X' || b[1][1] == 'O')
temp = 1;
else
{
b[1][1] = c;
temp = 0;
}
break;
case 6:
if (b[1][2] == 'X' || b[1][2] == 'O')
temp = 1;
else
{
b[1][2] = c;
temp = 0;
}
break;
case 7:
if (b[2][0] == 'X' || b[2][0] == 'O')
temp = 1;
else
{
b[2][0] = c;
temp = 0;
}
break;
case 8:
if (b[2][1] == 'X' || b[2][1] == 'O')
temp = 1;
else
{
b[2][1] = c;
temp = 0;
}
break;
case 9:
if (b[2][2] == 'X' || b[2][2] == 'O')
temp = 1;
else
{
b[2][2] = c;
temp = 0;
}
break;
}
}while(temp != 0);
board();
}
bool ticTacToe::Checker() //checks the winner
{
if ((b[0][0] == 'X'&& b[0][1] == 'X'&&b[0][2] ==
'X') ||
(b[1][0] == 'X'&& b[1][1] == 'X'&&b[1][2] == 'X')
||
(b[2][0] == 'X'&& b[2][1] == 'X'&&b[2][2] == 'X')
||
(b[0][0] == 'X'&& b[1][0] == 'X'&&b[2][0] == 'X')
||
(b[0][1] == 'X'&& b[1][1] == 'X'&&b[2][1] == 'X')
||
(b[0][1] == 'X'&& b[1][1] == 'X'&&b[2][1] == 'X')
||
(b[0][2] == 'X'&& b[1][1] == 'X'&&b[2][0] ==
'X')||
(b[0][0] == 'X'&& b[1][1] == 'X'&&b[2][2] ==
'X'))
{
cout << "Player 'X' WINS" << endl;
return 1;
}
else if ((b[0][0] == 'O'&& b[0][1] == 'O'&&b[0][2]
== 'O') ||
(b[1][0] == 'O'&& b[1][1] == 'O'&&b[1][2] == 'O')
||
(b[2][0] == 'O'&& b[2][1] == 'O'&&b[2][2] == 'O')
||
(b[0][0] == 'O'&& b[1][0] == 'O'&&b[2][0] == 'O')
||
(b[0][1] == 'O'&& b[1][1] == 'O'&&b[2][1] == 'O')
||
(b[0][1] == 'O'&& b[1][1] == 'O'&&b[2][1] == 'O')
||
(b[0][2] == 'O'&& b[1][1] == 'O'&&b[2][0] == 'O')
||
(b[0][0] == 'O'&& b[1][1] == 'O'&&b[2][2] ==
'O'))
cout << "Player 'O' WINS" << endl;
else
return 0;
}
Kindly revert for any queries
Thanks.
Chess game c++ programing using classes, objects, arays
(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...
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
So I have to make a tic tac toe game in C++. and the problem is asking me to design, implement and test classea to reperesnt the game board(3x3 sqaure), and the x and o markers. the problem is also asking me to provide suitable observor and mutator methods for modifying the game board and displaying game status. I have to use clases to create a game that prompts for player x and player O to place markers at specified...
Please write a tic tac toe game using c++ language and 1D arrays, thank you!
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...
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) ...
what’s the code of game Tic-tac-toe by using the Alpha Beta Turing algorithm ? The code in language JavaScript?
tic-tac-toe game. Add functionality to the program so when the button is clicked for the AI to take a turn, a heuristic is applied for each of the possible moves, a possible move is selected and the game state and GUI are properly updated. Once complete, the program should be able to play a single game of tic-tac-toe with the user. C#
Create a very basic tic tac toe game written in C code. This C code should be able to create a GBA file from using devkit to run in visual boy advance.