Source Code
Source Code
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
class tictactoe
{
public:
void clear_screen()
{
system("cls");
}
clear_screen();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
// Apply color to 'X'
if (board[i][j] == 'X')
{
cout << RED << board[i][j] << RESET;
}
// Apply color to '0'
else if (board[i][j] == '0')
{
cout << BLUE << board[i][j] << RESET;
}
// No color for empty cells
else
{
cout << board[i][j];
}
if (j != 2)
{
cout << " |";
}
}
cout << endl;
if (i != 2)
{
cout << "--------" << endl;
}
}
}
void checkwin(char board[3][3], string str, char symbol)
{
// CHECKING DIAGONALS
if ((board[0][0] == symbol) && (board[1][1] == symbol) && (board[2][2] == symbol))
{
cout << str << " won" << endl;
exit(0);
}
if ((board[0][2] == symbol) && (board[1][1] == symbol) && (board[2][0] == symbol))
{
cout << str << " won" << endl;
exit(0);
}
}
void PvP()
{
char board[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}};
string u, v;
cout << "Enter the name of the first player: ";
cin.ignore();
getline(cin, u);
cout << "Welcome " << u << ", your symbol is 'X'" << endl;
cout << "Enter the name of the second player: ";
getline(cin, v);
cout << "Welcome " << v << ", your symbol is 'O'" << endl;
string u;
cout << "Enter name of the player: ";
cin >> u;
cout << "Welcome " << u << ", your symbol is X" << endl;
cout << "Computer is the second player with symbol 0" << endl;
string u;
cout << "Enter name of the player: ";
cin >> u;
cout << "Welcome " << u << ", your symbol is X" << endl;
cout << "Computer is the second player with symbol 0" << endl;
else
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (board[i][j] == ' ')
{
board[i][j] = '0';
i = j = 3; // Break out of the loops
}
}
}
}
display_board(board);
checkwin(board, "Computer", '0');
}
else
{
int x, y;
label_player1_difficult:
cout << u << "Enter the position: ";
cin >> x >> y;
try
{
if ((x > 2) || (y > 2) || (x < 0) || (y < 0) || (board[x][y] != ' '))
throw 5;
}
catch (int a)
{
cout << "You have entered an invalid position. Please enter a correct one" <<
endl;
goto label_player1_difficult;
}
board[x][y] = 'X';
display_board(board);
cout << endl;
checkwin(board, u, 'X');
}
}
cout << "It's a draw" << endl;
}
};
class snakeandladder
{
public:
/*void display_board(string board1[10][10]);
void clear_previous_position(string board1[10][10], int pos);
void checking_ladder(string board1[10][10], int &pos, string pers);
void checking_snake(string board1[10][10], int &pos, string pers);
void checking_position(string board1[10][10], int pos, string pers);*/
void clear_screen()
{
system("cls");
}
void display_board(string board1[10][10])
{
cout << "--------------------------------------------------" << endl;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (board1[i][j] == "P1")
{
cout << "\x1B[31m"; // Set red color for P1
}
else if (board1[i][j] == "P2")
{
cout << "\x1B[34m"; // Set blue color for P2
}
else
{
cout << "\x1B[33m"; // Set default color to green for the rest of the board
}
cout << board1[i][j] << " \x1B[32m |";
}
cout << endl;
cout << "\x1B[37m--------------------------------------------------" << endl;
}
}
if (pos == 4)
{
cout << "You have reached a ladder!" << endl;
Beep(750, 300);
cout << "Your position is now at 14" << endl;
pos = 14;
board1[8][6] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 9)
{
cout << "You have reached a ladder!" << endl;
Beep(750, 300);
cout << "Your position is now at 31" << endl;
pos = 31;
board1[6][9] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 21)
{
cout << "You have reached a ladder!" << endl;
Beep(750, 300);
cout << "Your position is now at 42" << endl;
pos = 42;
board1[5][1] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 28)
{
cout << "You have reached a ladder!" << endl;
Beep(750, 300);
cout << "Your position is now at 84" << endl;
pos = 84;
board1[2][3] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 51)
{
cout << "You have reached a ladder!" << endl;
Beep(750, 300);
cout << "Your position is now at 67" << endl;
pos = 67;
board1[3][6] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 72)
{
cout << "You have reached a ladder!" << endl;
Beep(750, 300);
cout << "Your position is now at 91" << endl;
pos = 91;
board1[0][9] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 80)
{
cout << "You have reached a ladder!" << endl;
Beep(750, 300);
cout << "Your position is now at 99" << endl;
pos = 99;
board1[0][2] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
}
// Function to check if a player has reached a snake
void checking_snake(string board1[10][10], int &pos, string pers)
{
if (pos == 17)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 7" << endl;
pos = 7;
board1[9][6] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 62)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 19" << endl;
pos = 19;
board1[8][1] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 54)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 34" << endl;
pos = 34;
board1[6][6] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 87)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 36" << endl;
pos = 36;
board1[6][4] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 64)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 60" << endl;
pos = 60;
board1[4][0] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 93)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 73" << endl;
pos = 73;
board1[2][7] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 95)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 75" << endl;
pos = 75;
board1[2][5] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
else if (pos == 98)
{
cout << "OOPS!!You have reached at the mouth of a snake!" << endl;
Beep(750, 300);
cout << "Your position is now at 79" << endl;
pos = 79;
board1[2][1] = pers;
display_board(board1);
Sleep(2000);
clear_screen();
}
}
if ((pos != 4) && (pos != 9) && (pos != 21) && (pos != 28) && (pos != 51) && (pos != 72) &&
(pos != 80) && (pos != 17) && (pos != 62) && (pos != 54) && (pos != 87) && (pos != 64) && (pos != 93)
&& (pos != 95) && (pos != 98) && (pos != 20) && (pos != 40) && (pos != 60) && (pos != 80))
{
cout << "You are at position " << pos << endl;
int row, col;
row = 9 - (pos - 1) / 10;
if (row % 2 != 0)
{
col = (pos - 1) % 10;
}
else
{
col = (9 - (pos % 10)) + 1;
}
board1[row][col] = pers;
}
else if (pos == 20)
{
cout << "You are at position " << pos << endl;
board1[8][0] = pers;
}
else if (pos == 40)
{
cout << "You are at position " << pos << endl;
board1[6][0] = pers;
}
else if (pos == 60)
{
cout << "You are at position " << pos << endl;
board1[4][0] = pers;
}
else if (pos == 80)
{
cout << "You are at position " << pos << endl;
board1[2][0] = pers;
}
display_board(board1);
Sleep(2000);
clear_screen();
cout << endl;
}
};
class connectfour
{
private:
static const int ROWS = 6;
static const int COLS = 7;
vector<vector<char>> board;
char currentPlayer;
string p1, p2;
void printBoard()
{
const string RED = "\033[1;31m"; // Red color for 'X'
const string BLUE = "\033[1;34m"; // Blue color for '0'
const string RESET = "\033[0m"; // Reset color to default
cout << "\n 0 1 2 3 4 5 6\n";
for (int row = 0; row < ROWS; ++row)
{
cout << "|";
for (int col = 0; col < COLS; ++col)
{
if (board[row][col] == 'X')
{
cout << RED << board[row][col] << RESET << "|";
}
else if (board[row][col] == 'O')
{
cout << BLUE << board[row][col] << RESET << "|";
}
else
{
cout << board[row][col] << "|";
}
}
cout << "\n";
}
cout << "---------------\n";
}
bool checkWin()
{
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
if (board[row][col] == currentPlayer)
{
if (checkDirection(row, col, 0, 1) || // Horizontal
checkDirection(row, col, 1, 0) || // Vertical
checkDirection(row, col, 1, 1) || // Diagonal down-right
checkDirection(row, col, 1, -1)) // Diagonal down-left
return true;
}
}
}
return false;
}
bool isBoardFull()
{
for (int col = 0; col < COLS; ++col)
if (board[0][col] == ' ')
return false;
return true;
}
void switchPlayer()
{
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
public:
connectfour()
{
board = vector<vector<char>>(ROWS, vector<char>(COLS, ' '));
currentPlayer = 'X';
}
void play()
{
int m = 1;
cout << "Enter name of player 1: ";
cin.ignore();
getline(cin, p1);
cout << "Enter name of player 2: ";
getline(cin, p2);
cout << "Let's begin! Here's your Connect Four board:" << endl;
cout << "=== Connect 4 ===\n";
printBoard();
while (true)
{
int move;
if (m % 2 != 0)
{
cout << p1 << ", enter column (0-6): ";
cin >> move;
}
else if (m % 2 == 0)
{
cout << p2 << ", enter column (0-6): ";
cin >> move;
}
if (!isValidMove(move))
{
cout << "Invalid move. Try again.\n";
continue;
}
m++;
makeMove(move);
printBoard();
if (checkWin())
{
cout << "Player " << currentPlayer << " wins!\n";
break;
}
else if (isBoardFull())
{
cout << "It's a draw!\n";
break;
}
switchPlayer();
}
}
};
struct Card
{
char symbol;
bool revealed;
};
class MemoryGame
{
private:
const int SIZE = 4;
vector<vector<Card>> board;
int matches;
void printBoard()
{
cout << "\n ";
for (int j = 0; j < SIZE; ++j)
cout << j << " ";
cout << "\n";
for (int i = 0; i < SIZE; ++i)
{
cout << i << " ";
for (int j = 0; j < SIZE; ++j)
{
if (board[i][j].revealed)
cout << board[i][j].symbol << " ";
else
cout << "* ";
}
cout << "\n";
}
}
public:
MemoryGame() : board(SIZE, vector<Card>(SIZE)), matches(0)
{
initializeBoard();
}
void play()
{
while (matches < SIZE * SIZE / 2)
{
printBoard();
int x1, y1, x2, y2;
if (board[x1][y1].symbol == board[x2][y2].symbol)
{
cout << "Match found!\n";
matches++;
}
else
{
cout << "Not a match. Try again.\n";
board[x1][y1].revealed = false;
board[x2][y2].revealed = false;
}
cin.ignore();
cout << "Press Enter to continue...\n";
cin.get();
}
public:
Sudoku(int difficulty)
{
if (difficulty == 1)
{
board = easySeed;
}
else if (difficulty == 2)
{
board = mediumSeed;
}
else if (difficulty == 3)
{
board = hardSeed;
}
else
{
cout << "Invalid difficulty level. Defaulting to easy." << endl;
board = easySeed;
}
}
void printBoard()
{
for (int i = 0; i < 9; ++i)
{
if (i % 3 == 0 && i != 0)
{
cout << "---------------------" << endl;
}
if (board[i][j] == 0)
{
cout << ". ";
}
else
{
cout << board[i][j] << " ";
}
}
cout << endl;
}
}
return true;
}
bool solve()
{
for (int row = 0; row < 9; ++row)
{
for (int col = 0; col < 9; ++col)
{
if (board[row][col] == 0)
{
for (int num = 1; num <= 9; ++num)
{
if (isValidMove(row, col, num))
{
board[row][col] = num;
if (solve())
{
return true;
}
board[row][col] = 0;
}
}
return false;
}
}
}
return true;
}
void play()
{
int row, col, num;
if (row < 0 || row >= 9 || col < 0 || col >= 9 || num < 1 || num > 9)
{
cout << "Invalid input. Please try again." << endl;
continue;
}
if (solve())
{
cout << "You solved the puzzle!" << endl;
printBoard();
}
else
{
cout << "No solution exists." << endl;
}
}
};
class Game2048
{
private:
const int SIZE = 4;
int board[4][4] = {0};
bool game_over = false;
public:
void initGame()
{
for (int i = 0; i < 2; ++i)
{
int x = rand() % SIZE;
int y = rand() % SIZE;
while (board[x][y] != 0)
{
x = rand() % SIZE;
y = rand() % SIZE;
}
board[x][y] = (rand() % 2 + 1) * 2;
}
}
void printBoard()
{
system("cls");
cout << "2048 Game\n";
for (int i = 0; i < SIZE; ++i)
{
for (int j = 0; j < SIZE; ++j)
{
if (board[i][j] == 0)
cout << setw(5) << ".";
else
cout << setw(5) << board[i][j];
}
cout << endl;
}
cout << endl;
}
void moveLeft()
{
for (int i = 0; i < SIZE; ++i)
{
int row[SIZE];
int idx = 0;
for (int j = 0; j < SIZE; ++j)
{
if (board[i][j] != 0)
row[idx++] = board[i][j];
}
for (int j = 0; j < idx - 1; ++j)
{
merge(row[j], row[j + 1]);
}
for (int j = 0; j < SIZE; ++j)
{
if (j < idx)
board[i][j] = row[j];
else
board[i][j] = 0;
}
}
}
void moveRight()
{
for (int i = 0; i < SIZE; ++i)
{
int row[SIZE];
int idx = 0;
for (int j = SIZE - 1; j >= 0; --j)
{
if (board[i][j] != 0)
row[idx++] = board[i][j];
}
for (int j = 0; j < idx - 1; ++j)
{
merge(row[j], row[j + 1]);
}
for (int j = 0; j < SIZE; ++j)
{
if (j < idx)
board[i][SIZE - 1 - j] = row[j];
else
board[i][SIZE - 1 - j] = 0;
}
}
}
void moveUp()
{
for (int j = 0; j < SIZE; ++j)
{
int col[SIZE];
int idx = 0;
for (int i = 0; i < SIZE; ++i)
{
if (board[i][j] != 0)
col[idx++] = board[i][j];
}
for (int i = 0; i < idx - 1; ++i)
{
merge(col[i], col[i + 1]);
}
for (int i = 0; i < SIZE; ++i)
{
if (i < idx)
board[i][j] = col[i];
else
board[i][j] = 0;
}
}
}
void moveDown()
{
for (int j = 0; j < SIZE; ++j)
{
int col[SIZE];
int idx = 0;
for (int i = SIZE - 1; i >= 0; --i)
{
if (board[i][j] != 0)
col[idx++] = board[i][j];
}
for (int i = 0; i < idx - 1; ++i)
{
merge(col[i], col[i + 1]);
}
for (int i = 0; i < SIZE; ++i)
{
if (i < idx)
board[SIZE - 1 - i][j] = col[i];
else
board[SIZE - 1 - i][j] = 0;
}
}
}
void addRandomTile()
{
int x, y;
do
{
x = rand() % SIZE;
y = rand() % SIZE;
} while (board[x][y] != 0);
board[x][y] = (rand() % 2 + 1) * 2;
}
bool isGameOver()
{
for (int i = 0; i < SIZE; ++i)
{
for (int j = 0; j < SIZE; ++j)
{
if (board[i][j] == 0)
return false;
}
}
for (int i = 0; i < SIZE - 1; ++i)
{
for (int j = 0; j < SIZE - 1; ++j)
{
if (board[i][j] == board[i + 1][j] || board[i][j] == board[i][j + 1])
return false;
}
}
return true;
}
void runGame()
{
initGame();
while (!game_over)
{
printBoard();
cout << "Enter move (W = up, A = left, S = down, D = right, Q = quit): ";
char move;
cin >> move;
switch (move)
{
case 'w':
case 'W':
moveUp();
validMove = true;
break;
case 'a':
case 'A':
moveLeft();
validMove = true;
break;
case 's':
case 'S':
moveDown();
validMove = true;
break;
case 'd':
case 'D':
moveRight();
validMove = true;
break;
default:
cout << "Invalid move!" << endl;
break;
}
if (validMove)
{
addRandomTile();
if (isGameOver())
{
printBoard();
cout << "Game Over!" << endl;
game_over = true;
}
}
}
}
};
void playTTT()
{
tictactoe t;
int choice;
cin >> choice;
if (choice == 1)
{
t.PvP();
}
else if (choice == 2)
{
t.PvCEasy();
}
else if (choice == 3)
{
t.PvCHard();
}
}
void playSandL()
{
snakeandladder s;
string board1[10][10] = {
{"100", "99", "98", "97", "96", "95", "94", "93", "92", "91"},
{"81", "82", "83", "84", "85", "86", "87", "88", "89", "90"},
{"80", "79", "78", "77", "76", "75", "74", "73", "72", "71"},
{"61", "62", "63", "64", "65", "66", "67", "68", "69", "70"},
{"60", "59", "58", "57", "56", "55", "54", "53", "52", "51"},
{"41", "42", "43", "44", "45", "46", "47", "48", "49", "50"},
{"40", "39", "38", "37", "36", "35", "34", "33", "32", "31"},
{"21", "22", "23", "24", "25", "26", "27", "28", "29", "30"},
{"20", "19", "18", "17", "16", "15", "14", "13", "12", "11"},
{" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10"}};
cout << "Here is your snake and ladder board(ladder is at 4-14, 9-31, 21-42, 28-84, 51-67, 72-91,
80-99 and snake is at 17-7, 62-19, 54-34, 87-36, 64-60,93-73, 95-75,98-79)" << endl
<< endl;
s.display_board(board1);
s.clear_previous_position(board1, posp1);
if (posp1 == 100)
{
cout << p1 << "You have won the game!" << endl;
exit(0);
}
}
else
{
cout << p2 << ", press enter to roll a die" << endl;
getchar();
int q;
q = 1 + rand() % 6;
cout << p2 << ", you rolled a " << q << endl;
if (posp2 + q <= 100)
{
posp2 = posp2 + q;
}
else if (posp2 + q > 100)
{
cout << "Better luck next time" << endl;
}
s.clear_previous_position(board1, posp2);
if (posp2 == 100)
{
cout << p2 << "You have won the game!" << endl;
exit(0);
}
}
moves++;
}
}
void playconnectfour()
{
cout << " Welcome to CONNECT FOUR" <<
endl
<< endl;
cout << "Here are the rules:" << endl;
cout << "1) Objective: The goal is to connect four of your pieces in a row-vertically,
horizontally, or diagonally-before your opponent does." << endl;
cout << "2) Players: Two players take turns. Player 1 is 'X' and Player 2 is 'O'." << endl;
cout << "3) Board: The game is played on a 7-column by 6-row grid." << endl;
cout << "4) Turns: Players take turns dropping one of their discs into a column." << endl;
cout << "5) Gravity: Discs fall straight down and occupy the lowest available spot in the chosen
column." << endl;
cout << "6) Winning: The first player to connect four of their discs in a row (horizontally,
vertically, or diagonally) wins." << endl;
cout << "7) Draw: If the board is filled completely without any player connecting four, the game
ends in a draw." << endl;
cout << "8) Strategy: Watch your opponent's moves and plan ahead to block and build winning
lines." << endl
<< endl;
connectfour game;
game.play();
}
void playmemorygame()
{
cout << " Welcome to MEMORY MATCHING
GAME" << endl
<< endl;
cout << "Here are the rules:\n";
cout << "1) Objective: Match all pairs of hidden cards on a 4x4 grid.\n";
cout << "2) Board: The board is 4x4 and contains 8 pairs of matching symbols (A-H).\n";
cout << "3) Turn: On each turn, select two cards by entering their row and column indices.\n";
cout << "4) Matching: If the two selected cards match, they remain revealed.\n";
cout << "5) Mismatch: If they don't match, they will be hidden again.\n";
cout << "6) Victory: The game ends when all pairs are successfully matched.\n";
cout << "7) Strategy: Try to remember the positions of cards you've already seen.\n";
cout << "8) Note: Choosing the same card twice or a revealed card is considered invalid.\n";
cout << "\nLet's begin!\n\n";
MemoryGame game;
game.play();
}
void playsudoku()
{
int difficulty;
Sudoku game(difficulty);
game.play();
}
void play2048()
{
cout << " Welcome to 2048!" << endl
<< endl;
cout << "Here are the rules:" << endl;
cout << "1) Objective: The goal of the game is to combine numbers and reach 2048." << endl;
cout << "2) Players: The game is a single-player game." << endl;
cout << "3) Board: The game is played on a 4x4 grid, and the player must swipe the tiles to merge
them." << endl;
cout << "4) Turns: Players move the tiles by pressing W (up), A (left), S (down), or D (right)."
<< endl;
cout << "5) Winning: The player wins when they combine tiles to form the 2048 tile." << endl;
cout << "6) Losing: The game ends when the grid is full, and no more moves are possible." << endl;
cout << "7) Strategy: To win, you need to plan moves carefully to combine tiles and make space for
larger numbers." << endl;
cout << "8) Fairness: The game requires both luck and strategy, and it's about getting the highest
score possible." << endl;
cout << endl
<< endl;
Game2048 game;
game.runGame();
}
int main()
{
cout << "\x1b[33m";
cout << " WELCOME PLAYER" << endl
<< endl;
cout << "THIS IS A SIX GAME PORTAL WHICH INCLUDES THE FOLLOWING GAMES:" << endl << endl;
cout << "1- TIC TAC TOE" << endl;
cout << "2- SNAKES AND LADDERS" << endl;
cout << "3- CONNECT FOUR" << endl;
cout << "4- MEMORY MATCHING GAME" << endl;
cout << "5- SUDOKU" << endl;
cout << "6- 2048" << endl
<< endl;
cout << "\x1b[0m";
cout << "CHOOSE WHICH GAME YOU WANT TO PLAY" << endl;
int a;
cin >> a;
if (a == 1)
{
playTTT();
}
else if (a == 2)
{
playSandL();
}
else if (a == 3)
{
playconnectfour();
}
else if (a == 4)
{
playmemorygame();
}
else if (a == 5)
{
playsudoku();
}
else if (a == 6)
{
play2048();
}
else
{
cout << "THE USER HAS NOT CHOSEN ANY GAME TO PLAY" << endl;
}
return 0;
}