Search
 
SCRIPT & CODE EXAMPLE
 

CPP

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic toc toe c++

#include<iostream>
using namespace std;

int num;    //number enter to play.
char ch[9] = { '1','2','3','4','5','6','7','8','9' }; 
char player1 = { 'X' };
char player2 = { 'O' };
void Draw();
void player_1();
void player_2();
bool X_Wins();
bool O_Wins();

int main()
{
	int n = -1;
	cout << "				Tic Toc Toe Game
" << endl;
	cout << "				player<1>: X
";
	cout << "				player<2>: O
";
	cout << "

";
	do
	{
		Draw();
		player_1();
		Draw();
		if (X_Wins())
		{
			cout << "				Player<1> Wins!" << endl;
			n = 1;
			break;
		}
		cout << "


";
		player_2();
		Draw();
		if (O_Wins())
		{
			cout << "				Player<2> Wins!" << endl;
			n = 1;
			break;
		}
		cout << "


";
	} while (n == -1);

	return 0;
}
//Draw
void Draw()
{
	cout <<"				"<< ch[0] << "  |  " << ch[1] << "  |  " << ch[2] << "  |  " << "
";
	cout << "				----------------" << endl;
	cout << "				"<<ch[3] << "  |  " << ch[4] << "  |  " << ch[5] << "  |  " << "
";
	cout << "				----------------" << endl;
	cout << "				"<<ch[6] << "  |  " << ch[7] << "  |  " << ch[8] << "  |  " << "


";
}
//player 1
void player_1()
{
	cout << "				Player<1> Enter number: ";
	cin >> num;
	switch (num)
	{
	case 1:
		ch[0] = player1;
		break;
	case 2:
		ch[1] = player1;
		break;
	case 3:
		ch[2] = player1;
		break;
	case 4:
		ch[3] = player1;
		break;
	case 5:
		ch[4] = player1;
		break;
	case 6:
		ch[5] = player1;
		break;
	case 7:
		ch[6] = player1;
		break;
	case 8:
		ch[7] = player1;
		break;
	case 9:
		ch[8] = player1;
		break;
	default:
		cout << "				
please, enter number from <1> to <9>!

" << endl;
		player_1();
		break;
	}
}
//player 2
void player_2()
{
	cout << "				Player<2> Enter number: ";
	cin >> num;
	switch (num)
	{
	case 1:
		ch[0] = player2;
		break;
	case 2:
		ch[1] = player2;
		break;
	case 3:
		ch[2] = player2;
		break;
	case 4:
		ch[3] = player2;
		break;
	case 5:
		ch[4] = player2;
		break;
	case 6:
		ch[5] = player2;
		break;
	case 7:
		ch[6] = player2;
		break;
	case 8:
		ch[7] = player2;
		break;
	case 9:
		ch[8] = player2;
		break;
	default:
		cout << "				
please, enter number from <1> to <9>!

" << endl;
		player_2();
		break;
	}
}
//player 1 wins.
bool X_Wins()
{
	if (ch[0] == 'X' && ch[1] == 'X' && ch[2] == 'X')
		return true;
	else if (ch[3] == 'X' && ch[4] == 'X' && ch[5] == 'X')
		return true;
	else if (ch[6] == 'X' && ch[7] == 'X' && ch[8] == 'X')
		return true;
	else if (ch[0] == 'X' && ch[3] == 'X' && ch[6] == 'X')
		return true;
	else if (ch[1] == 'X' && ch[4] == 'X' && ch[7] == 'X')
		return true;
	else if (ch[2] == 'X' && ch[5] == 'X' && ch[8] == 'X')
		return true;
	else if (ch[0] == 'X' && ch[4] == 'X' && ch[8] == 'X')
		return true;
	else if (ch[2] == 'X' && ch[4] == 'X' && ch[6] == 'X')
		return true;
	else
		return false;
}
//player 2 wins.
bool O_Wins()
{
	if (ch[0] == 'O' && ch[1] == 'O' && ch[2] == 'O')
		return true;
	else if (ch[3] == 'O' && ch[4] == 'O' && ch[5] == 'O')
		return true;
	else if (ch[6] == 'O' && ch[7] == 'O' && ch[8] == 'O')
		return true;
	else if (ch[0] == 'O' && ch[3] == 'O' && ch[6] == 'O')
		return true;
	else if (ch[1] == 'O' && ch[4] == 'O' && ch[7] == 'O')
		return true;
	else if (ch[2] == 'O' && ch[5] == 'O' && ch[8] == 'O')
		return true;
	else if (ch[0] == 'O' && ch[4] == 'O' && ch[8] == 'O')
		return true;
	else if (ch[2] == 'O' && ch[4] == 'O' && ch[6] == 'O')
		return true;
	else
		return false;
}
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

tic tac toe in cpp

// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to make a n*n 2d dynamic array in c++ 
Cpp :: bit c++ 
Cpp :: C++ generate a random letter 
Cpp :: sort function from bigest to smallest c++ 
Cpp :: c++ product of vector 
Cpp :: c++ shared pointer 
Cpp :: min element c++ 
Cpp :: C++ convert integer to digits, as vector 
Cpp :: opencv rgb to gray c++ 
Cpp :: factorial using recursion cpp 
Cpp :: google pdf iframe viwer 
Cpp :: difference between lower and upper bound 
Cpp :: c++ hours minutes seconds 
Cpp :: array and for loop in c++ 
Cpp :: map in c++ sorted descending order 
Cpp :: copy a part of a vector in another in c++ 
Cpp :: c++ std::sort 
Cpp :: resize 2d vector c++ 
Cpp :: max heap in c++ 
Cpp :: find primes in cpp 
Cpp :: read comma separated text file in c++ 
Cpp :: how to erase a certain value from a vector in C++ 
Cpp :: coordinate in 1d array 
Cpp :: c++ how to read from a file 
Cpp :: c++ Sum of all the factors of a number 
Cpp :: what is c++ standard library 
Cpp :: string to upper c++ 
Cpp :: c++ rand include 
Cpp :: C++ Vector Operation Add Element 
Cpp :: lua table contains 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =