Data Strucure and Algorithem Proj.
Data Strucure and Algorithem Proj.
BY
Sarmad Dakhel
Safar attallah
supervised by:
Dr. Turkan Ahmed Khalil
Tic Tac Toe Game:
This project involves applying Data Structure and Algorithm concepts to create
a console-based version of the classic Tic Tac Toe game. The project typically
includes the implementation of game logic, user input handling, and checking
for win/lose conditions. Using C++ programming language allows for efficient
representation of the game board, player moves, and decision-making
algorithms. This project provides a practical application of data structures and
algorithms in the context of a well-known and widely played game.
4. Draw () function:
- Used to draw the current gameboard in the window.
- Displays the board in the form of a 3x3 grid and displays the values of the cells.
5. The inputXO()function:
- It asks the player to enter his movement into the game.
- Convert the entered position of the player to Matrix indicators.
- Check the correctness of the input location and update the chipboard with
the appropriate value.
- You switch the current player after each move.
6. CheckWinner()function:
- Used to check if there is a winner in the game.
- Scan rows, columns and diagonals to find three points in a row for the same
player.
7. CheckDraw()function:
- Used to check the draw of the game.
- Check if all cells are full without a winner.
8. GetWinner () function:
- Used to retrieve the winning player in the game.
class TicTacToe
{
private:
char player;
char board[3][3];
char winner;
public:
TicTacToe()
{
player = 'X';
winner = '-';
// Initialize the board with numbers 1 to 9
char num = '1';
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = num++;
}
}
}
void draw()
{
cout << "------------ \n";
for (int r = 0; r < 3; r++)
{
cout << "| ";
for (int c = 0; c < 3; c++)
{
cout << board[r][c] << " ";
}
cout << "|";
cout << endl;
}
cout << "------------ \n";
}
void inputXO()
{
int position;
cout << "ENTER YOUR POSITION (" << player << ") \n";
cin >> position;
board[row][col] = playerChar;
if (player == 'X')
player = 'O';
else
player = 'X';
}
bool checkWinner()
{
// Check rows
for (int r = 0; r < 3; r++)
{
if (board[r][0] == board[r][1] && board[r][1] == board[r][2])
{
winner = board[r][0];
return true;
}
}
// Check columns
for (int c = 0; c < 3; c++)
{
if (board[0][c] == board[1][c] && board[1][c] == board[2][c])
{
winner = board[0][c];
return true;
}
}
// Check diagonals
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
{
winner = board[1][1];
return true;
}
return false;
}
bool checkDraw()
{
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
if (board[r][c] != 'X' && board[r][c] != 'O')
{
return false; // If any cell is empty, game is not draw
}
}
}
return true; // All cells are filled, game is draw
}
char getWinner()
{
return winner;
}
};
class Stack
{
private:
const int MAX_SIZE = 100;
char items[100];
int top;
public:
Stack()
{
top = -1;
}
char pop()
{
if (top == -1)
{
cout << "Stack underflow!" << endl;
return '-';
}
return items[top--];
}
bool isEmpty()
{
return top == -1;
}
void showWins()
{
cout << "Wins: ";
for (int i = 0; i <= top; i++)
{
cout << items[i] << " ";
}
cout << endl;
}
int main()
{
char d;
Stack winStack;
char playAgain = 'Y';
for (;;)
{
cout << "\nMenu:\n";
cout << "1. Play \n";
cout << "2. Show Wins\n";
cout << "3. Pop Result\n";
cout << "Choose an option: ";
int option;
cin >> option;
switch (option)
{
case 1:
cout << "Do you want to play? (Y/N): ";
cin >> playAgain;
cout << endl;
while (playAgain == 'Y' || playAgain == 'y')
{
TicTacToe game;
while (true)
{
game.draw();
game.inputXO();
if (game.checkWinner())
{
winStack.push(game.getWinner());
cout << "Player " << game.getWinner() << " wins!" <<
endl;
winStack.showWins();
cout << endl;
break;
}
else if (game.checkDraw())
{
cout << "The game ends in a draw!" << endl;
break;
}
}
return 0;
}