xxxxxxxxxx
#include <iostream>
#include <vector>
enum class Player { None, X, O };
class TicTacToe {
public:
TicTacToe() : board(9, Player::None), currentPlayer(Player::X), movesLeft(9) {}
void play() {
while (movesLeft > 0) {
displayBoard();
int position;
std::cout << "Player " << playerToString(currentPlayer) << ", enter your move (1-9): ";
std::cin >> position;
if (isValidMove(position)) {
makeMove(position);
if (checkWin(currentPlayer)) {
displayBoard();
std::cout << "Player " << playerToString(currentPlayer) << " wins!" << std::endl;
return;
}
currentPlayer = (currentPlayer == Player::X) ? Player::O : Player::X;
movesLeft--;
} else {
std::cout << "Invalid move. Please try again." << std::endl;
}
}
displayBoard();
std::cout << "It's a draw!" << std::endl;
}
private:
std::vector<Player> board;
Player currentPlayer;
int movesLeft;
void displayBoard() const {
std::cout << "-------------" << std::endl;
for (int i = 0; i < 9; i += 3) {
std::cout << "| " << playerToString(board[i]) << " | " << playerToString(board[i + 1]) << " | "
<< playerToString(board[i + 2]) << " |" << std::endl;
std::cout << "-------------" << std::endl;
}
}
std::string playerToString(Player player) const {
if (player == Player::X) {
return "X";
} else if (player == Player::O) {
return "O";
} else {
return " ";
}
}
bool isValidMove(int position) const {
return (position >= 1 && position <= 9 && board[position - 1] == Player::None);
}
void makeMove(int position) {
board[position - 1] = currentPlayer;
}
bool checkWin(Player player) const {
for (int i = 0; i < 9; i += 3) {
if (board[i] == player && board[i + 1] == player && board[i + 2] == player) {
return true;
}
}
for (int i = 0; i < 3; ++i) {
if (board[i] == player && board[i + 3] == player && board[i + 6] == player) {
return true;
}
}
if (board[0] == player && board[4] == player && board[8] == player) {
return true;
}
if (board[2] == player && board[4] == player && board[6] == player) {
return true;
}
return false;
}
};
int main() {
TicTacToe game;
game.play();
return 0;
}
xxxxxxxxxx
#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 << "\t\t\t\tTic Toc Toe Game\n" << endl;
cout << "\t\t\t\tplayer<1>: X\n";
cout << "\t\t\t\tplayer<2>: O\n";
cout << "\n\n";
do
{
Draw();
player_1();
Draw();
if (X_Wins())
{
cout << "\t\t\t\tPlayer<1> Wins!" << endl;
n = 1;
break;
}
cout << "\n\n\n";
player_2();
Draw();
if (O_Wins())
{
cout << "\t\t\t\tPlayer<2> Wins!" << endl;
n = 1;
break;
}
cout << "\n\n\n";
} while (n == -1);
return 0;
}
//Draw
void Draw()
{
cout <<"\t\t\t\t"<< ch[0] << " | " << ch[1] << " | " << ch[2] << " | " << "\n";
cout << "\t\t\t\t----------------" << endl;
cout << "\t\t\t\t"<<ch[3] << " | " << ch[4] << " | " << ch[5] << " | " << "\n";
cout << "\t\t\t\t----------------" << endl;
cout << "\t\t\t\t"<<ch[6] << " | " << ch[7] << " | " << ch[8] << " | " << "\n\n\n";
}
//player 1
void player_1()
{
cout << "\t\t\t\tPlayer<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 << "\t\t\t\t\nplease, enter number from <1> to <9>!\n\n" << endl;
player_1();
break;
}
}
//player 2
void player_2()
{
cout << "\t\t\t\tPlayer<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 << "\t\t\t\t\nplease, enter number from <1> to <9>!\n\n" << 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;
}
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp
xxxxxxxxxx
// github link: https://github.com/AhmedSamir2000/Tic-Toc-Toe-Game-C-/blob/main/Tic%20Toc%20Toe%20Game%20In%20C%2B%2B/Source.cpp