0% found this document useful (0 votes)
14 views

Source Code

Uploaded by

Shivam choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Source Code

Uploaded by

Shivam choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

SOURCE CODE

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;

class tictactoe
{
public:
void clear_screen()
{
system("cls");
}

void display_board(char board[3][3])


{
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

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 HORIZONTAL LINES


for (int i = 0; i < 3; ++i)
{
if ((board[i][0] == symbol) && (board[i][1] == symbol) && (board[i][2] == symbol))
{
cout << str << " won" << endl;
exit(0);
}
}

// CHECKING VERTICAL LINES


for (int i = 0; i < 3; ++i)
{
if ((board[0][i] == symbol) && (board[1][i] == symbol) && (board[2][i] == symbol))
{
cout << str << " won" << endl;
exit(0);
}
}

// 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;

for (int moves = 1; moves <= 9; moves++)


{
if ((moves % 2) != 0)
{
int x, y;
label_player1:
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;
}
board[x][y] = 'X';
display_board(board);
checkwin(board, u, 'X');
}
else
{
int p, q;
label_player2:
cout << v << ", enter the position: ";
cin >> p >> q;
try
{
if ((p > 2) || (q > 2) || (p < 0) || (q < 0) || (board[p][q] != ' '))
throw 2;
}
catch (int b)
{
cout << "You have entered an invalid position. Please enter a correct one" <<
endl;
goto label_player2;
}
board[p][q] = '0';
display_board(board);
checkwin(board, v, '0');
}
}

cout << "It's a draw" << endl;


}
void PvCEasy()
{
char board[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}};

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;

for (int moves = 1; moves <= 9; moves++)


{
if ((moves % 2) != 0)
{
int x, y;
label_player1_easy:
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_easy;
}
board[x][y] = 'X';
display_board(board);
cout << endl;
checkwin(board, u, 'X');
}
else
{
int p, q;
label_computer_easy:
p = rand() % 3;
q = rand() % 3;
try
{
if ((p > 2) || (q > 2) || (p < 0) || (q < 0) || (board[p][q] != ' '))
throw 2;
}
catch (int b)
{
cout << "You have entered an invalid position. Please enter a correct one" <<
endl;
goto label_computer_easy;
}
board[p][q] = '0';
display_board(board);
checkwin(board, "Computer", '0');
}
}
cout << "It's a draw" << endl;
}
void PvCHard()
{
char board[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}};

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;

for (int moves = 1; moves <= 9; moves++)


{
if ((moves % 2) != 0)
{
// Computer's turn for difficult level
// Winning moves for computer
for (int i = 0; i < 3; i++)
{
if (board[i][0] == '0' && board[i][1] == '0' && board[i][2] == ' ')
{
board[i][2] = '0';
}
else if (board[i][0] == '0' && board[i][2] == '0' && board[i][1] == ' ')
{
board[i][1] = '0';
}
else if (board[i][1] == '0' && board[i][2] == '0' && board[i][0] == ' ')
{
board[i][0] = '0';
}
}

for (int i = 0; i < 3; i++)


{
if (board[0][i] == '0' && board[1][i] == '0' && board[2][i] == ' ')
{
board[2][i] = '0';
}
else if (board[0][i] == '0' && board[2][i] == '0' && board[1][i] == ' ')
{
board[1][i] = '0';
}
else if (board[1][i] == '0' && board[2][i] == '0' && board[0][i] == ' ')
{
board[0][i] = '0';
}
}

if (board[0][0] == '0' && board[1][1] == '0' && board[2][2] == ' ')


{
board[2][2] = '0';
}
else if (board[0][0] == '0' && board[2][2] == '0' && board[1][1] == ' ')
{
board[1][1] = '0';
}
else if (board[1][1] == '0' && board[2][2] == '0' && board[0][0] == ' ')
{
board[0][0] = '0';
}

if (board[0][2] == '0' && board[1][1] == '0' && board[2][0] == ' ')


{
board[2][0] = '0';
}
else if (board[0][2] == '0' && board[2][0] == '0' && board[1][1] == ' ')
{
board[1][1] = '0';
}
else if (board[1][1] == '0' && board[2][0] == '0' && board[0][2] == ' ')
{
board[0][2] = '0';
}

// Blocking moves for player


for (int i = 0; i < 3; i++)
{
if (board[i][0] == 'X' && board[i][1] == 'X' && board[i][2] == ' ')
{
board[i][2] = '0';
}
else if (board[i][0] == 'X' && board[i][2] == 'X' && board[i][1] == ' ')
{
board[i][1] = '0';
}
else if (board[i][1] == 'X' && board[i][2] == 'X' && board[i][0] == ' ')
{
board[i][0] = '0';
}
}

for (int i = 0; i < 3; i++)


{
if (board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == ' ')
{
board[2][i] = '0';
}
else if (board[0][i] == 'X' && board[2][i] == 'X' && board[1][i] == ' ')
{
board[1][i] = '0';
}
else if (board[1][i] == 'X' && board[2][i] == 'X' && board[0][i] == ' ')
{
board[0][i] = '0';
}
}

if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == ' ')


{
board[2][2] = '0';
}
else if (board[0][0] == 'X' && board[2][2] == 'X' && board[1][1] == ' ')
{
board[1][1] = '0';
}
else if (board[1][1] == 'X' && board[2][2] == 'X' && board[0][0] == ' ')
{
board[0][0] = '0';
}

if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == ' ')


{
board[2][0] = '0';
}
else if (board[0][2] == 'X' && board[2][0] == 'X' && board[1][1] == ' ')
{
board[1][1] = '0';
}
else if (board[1][1] == 'X' && board[2][0] == 'X' && board[0][2] == ' ')
{
board[0][2] = '0';
}
else if (board[1][1] == ' ')
{
board[1][1] = '0';
}
else if (board[0][0] == ' ')
{
board[0][0] = '0';
}
else if (board[0][2] == ' ')
{
board[0][2] = '0';
}
else if (board[2][0] == ' ')
{
board[2][0] = '0';
}
else if (board[2][2] == ' ')
{
board[2][2] = '0';
}

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;
}
}

// Function to clear the previous position of a player on the board


void clear_previous_position(string board1[10][10], int pos)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (board1[i][j] == "P1" || board1[i][j] == "P2")
{
board1[i][j] = to_string(pos);
}
}
}
}

// Function to check if a player has reached a ladder


void checking_ladder(string board1[10][10], int &pos, string pers)
{

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();
}
}

// Function to check and update the position of a player on the board


void checking_position(string board1[10][10], int pos, string pers)
{

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 isValidMove(int col)


{
return col >= 0 && col < COLS && board[0][col] == ' ';
}
void makeMove(int col)
{
for (int row = ROWS - 1; row >= 0; --row)
{
if (board[row][col] == ' ')
{
board[row][col] = currentPlayer;
break;
}
}
}

bool checkDirection(int row, int col, int dRow, int dCol)


{
int count = 0;
for (int i = 0; i < 4; ++i)
{
int r = row + i * dRow;
int c = col + i * dCol;
if (r >= 0 && r < ROWS && c >= 0 && c < COLS && board[r][c] == currentPlayer)
count++;
else
break;
}
return count == 4;
}

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 shuffle(vector<char> &symbols)


{
for (int i = symbols.size() - 1; i > 0; --i)
{
int j = rand() % (i + 1);
swap(symbols[i], symbols[j]);
}
}
void initializeBoard()
{
vector<char> symbols = {
'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D',
'E', 'E', 'F', 'F', 'G', 'G', 'H', 'H'};
shuffle(symbols);
int k = 0;
for (int i = 0; i < SIZE; ++i)
for (int j = 0; j < SIZE; ++j)
board[i][j] = {symbols[k++], false};
}

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";
}
}

bool isValid(int x, int y)


{
return x >= 0 && x < SIZE && y >= 0 && y < SIZE && !board[x][y].revealed;
}

public:
MemoryGame() : board(SIZE, vector<Card>(SIZE)), matches(0)
{
initializeBoard();
}

void play()
{
while (matches < SIZE * SIZE / 2)
{
printBoard();
int x1, y1, x2, y2;

cout << "\nEnter first card (row column): ";


cin >> x1 >> y1;
if (!isValid(x1, y1))
{
cout << "Invalid input. Try again.\n";
continue;
}
board[x1][y1].revealed = true;
printBoard();

cout << "Enter second card (row column): ";


cin >> x2 >> y2;
if (!isValid(x2, y2) || (x1 == x2 && y1 == y2))
{
cout << "Invalid input. Try again.\n";
board[x1][y1].revealed = false;
continue;
}
board[x2][y2].revealed = true;
printBoard();

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();
}

cout << "\nCongratulations! You matched all the cards!\n";


}
};
class Sudoku
{
private:
vector<vector<int>> board;

const vector<vector<int>> easySeed = {


{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};

const vector<vector<int>> mediumSeed = {


{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};

const vector<vector<int>> hardSeed = {


{0, 0, 0, 4, 0, 0, 0, 8, 0},
{0, 0, 3, 0, 0, 0, 0, 7, 0},
{0, 2, 0, 0, 0, 8, 4, 1, 0},
{3, 0, 0, 0, 2, 0, 5, 0, 0},
{0, 8, 4, 0, 0, 0, 7, 0, 0},
{0, 1, 5, 0, 0, 0, 0, 0, 6},
{0, 4, 1, 2, 0, 0, 0, 0, 3},
{0, 7, 0, 0, 0, 0, 1, 0, 0},
{9, 0, 0, 0, 0, 5, 6, 0, 0}};

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;
}

for (int j = 0; j < 9; ++j)


{
if (j % 3 == 0 && j != 0)
{
cout << "| ";
}

if (board[i][j] == 0)
{
cout << ". ";
}
else
{
cout << board[i][j] << " ";
}
}
cout << endl;
}
}

bool isValidMove(int row, int col, int num)


{
for (int i = 0; i < 9; ++i)
{
if (board[row][i] == num)
return false;
}

for (int i = 0; i < 9; ++i)


{
if (board[i][col] == num)
return false;
}

int startRow = (row / 3) * 3;


int startCol = (col / 3) * 3;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (board[startRow + i][startCol + j] == num)
{
return false;
}
}
}

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;

cout << "Starting Sudoku Game" << endl;


while (true)
{
printBoard();
cout << "Enter row (0-8), column (0-8), and number (1-9) to place (0 to quit): ";
cin >> row >> col >> num;

if (row == 0 && col == 0 && num == 0)


{
break;
}

if (row < 0 || row >= 9 || col < 0 || col >= 9 || num < 1 || num > 9)
{
cout << "Invalid input. Please try again." << endl;
continue;
}

if (isValidMove(row, col, num))


{
board[row][col] = num;
}
else
{
cout << "Invalid move. Try again." << endl;
}
}

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 merge(int &a, int &b)


{
if (a == b && a != 0)
{
a *= 2;
b = 0;
}
}

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;

if (move == 'q' || move == 'Q')


{
cout << "You quit the game!" << endl;
break;
}

bool validMove = false;

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()
{

cout << " Welcome to TIC-TAC-TOE" <<


endl
<< endl;
cout << "Here are the rules:" << endl;
cout << "1)Objective: The game is played on a 3x3 grid. The objective is to be the first player to
get three of their symbols (traditionally X or O) in a row, column, or diagonal." << endl;
cout << "2)Players: The game is typically played by two players." << endl;
cout << "3)Board: The board consists of a 3x3 grid. Players take turns placing their symbol (X or
O) in an empty cell of the grid." << endl;
cout << "4)Turns: Players take turns to make their moves. Player X makes the first move, followed
by Player O, and so on." << endl;
cout << "5)Winning: A player wins the game if they have three of their symbols in a row, column,
or diagonal. The game ends immediately when a player achieves this, and they are declared the winner."
<< endl;
cout << "6)Draw: If all cells of the grid are filled with symbols and neither player has achieved
a winning combination, the game is a draw." << endl;
cout << "7)Strategy: While Tic Tac Toe is a simple game, it still involves some strategy. Players
aim to either create their own winning combinations or block their opponent from achieving theirs." <<
endl;
cout << "8)Fairness: If both players play optimally, the game should end in a draw. However,
mistakes or suboptimal play can lead to one player winning or a draw." << endl
<< endl
<< endl;
cout << "Here is your Tic Tac Toe board" << endl;

tictactoe t;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
if (j != 2)
{
cout << " |";
}
}
cout << endl;
if (i != 2)
{
cout << "---------" << endl;
}
}

cout << endl;


cout << "Choose an option:" << endl;
cout << "1. Player vs player" << endl;
cout << "2. Player vs computer (Easy)" << endl;
cout << "3. Player vs computer (Difficult)" << endl;

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 << " SNAKE AND LADDER" << endl


<< endl;
cout << "Here are the rules:" << endl;
cout << "1)Objective: The objective of the game is to be the first player to reach the final
square on the game board, traditionally marked as 100." << endl
<< "2)Game Board: The game is played on a square board divided into a grid of cells. Each
cell is numbered sequentially from 1 to 100." << endl
<< "3)Players: The game is typically played by two or more players. Each player takes turns
to roll a die and move their token (representing their position) on the board." << endl
<< "4)Rolling the Die: On their turn, a player rolls a standard six-sided die. The number
rolled determines the number of squares the player advances on the board." << endl
<< "5)Ladders: Some squares on the board have a ladder symbol connecting them to a higher-
numbered square. If a player lands on a square at the bottom of a ladder, they immediately move their
token to the square at the top of the ladder." << endl
<< "6)Snakes: Similarly, some squares on the board have a snake symbol connecting them to a
lower-numbered square. If a player lands on a square at the head of a snake, they immediately move
their token to the square at the tail of the snake." << endl
<< "7)Winning: The first player to reach or exceed the final square (100) on the board is
declared the winner." << endl
<< "8)Extra Rolls: If a player rolls a number that would move them beyond square 100, they do
not move forward on that turn. However, if they roll the exact number needed to reach square 100, they
win the game." << endl
<< "9)Turns: Players take turns rolling the die and moving their tokens in a clockwise manner
around the board until one player wins." << endl
<< "10)Optional Rules: Some variations of the game may include additional rules or features,
such as penalties for landing on certain squares or special rules for landing on the first and last
squares." << endl
<< endl;

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);

string p1, p2;


cout << "Enter the name of 1st player: ";
cin.ignore();
getline(cin, p1);
cout << "Enter the name of 2nd player: ";
getline(cin, p2);
cout << endl;

int posp1 = 0, posp2 = 0;


int moves = 1;
while ((posp1 <= 100) && (posp2 <= 100))
{
if (moves % 2 != 0)
{
cout << p1 << ", press enter to roll a die" << endl;
getchar();
int p;
p = 1 + rand() % 6;
cout << p1 << ", you rolled a " << p << endl;
if (posp1 + p <= 100)
{
posp1 = posp1 + p;
}
else if (posp1 + p > 100)
{
cout << "Better luck next time" << endl;
}

s.checking_ladder(board1, posp1, "P1");

s.checking_snake(board1, posp1, "P1");

s.checking_position(board1, posp1, "P1");

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.checking_ladder(board1, posp2, "P2");

s.checking_snake(board1, posp2, "P2");

s.checking_position(board1, posp2, "P2");

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;

cout << " Welcome to Sudoku!" <<


endl;

cout << "\nHere are the rules:" << endl;


cout << "1) Objective: The game is played on a 9x9 grid. The objective is to fill the grid such
that each row, each column, and each 3x3 subgrid contain all the digits from 1 to 9." << endl;
cout << "2) Players: The game is played by a single player." << endl;
cout << "3) Board: The board consists of a 9x9 grid. Some cells will already be filled with
numbers, and the player must fill in the rest of the cells." << endl;
cout << "4) Valid Move: A player can place a number in a cell if the number is not already in the
same row, column, or 3x3 subgrid." << endl;
cout << "5) Winning: The game ends when the board is completely filled, and the player has
successfully placed all numbers in a valid configuration." << endl;
cout << "6) Strategy: Sudoku involves logical thinking and strategy. Players need to use deduction
to figure out where each number goes." << endl;
cout << "7) Fairness: Sudoku puzzles are designed to have one unique solution. The difficulty
level affects how many numbers are pre-filled and how difficult the puzzle is to solve." << endl;

cout << "\nSelect difficulty level (1 = Easy, 2 = Medium, 3 = Hard): ";


cin >> 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;

cout << "Press Enter to start the game..." << endl;


cin.ignore();
cin.get();

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;
}

You might also like