0% found this document useful (0 votes)
26 views9 pages

Data Strucure and Algorithem Proj.

Uploaded by

nawrashamdone
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)
26 views9 pages

Data Strucure and Algorithem Proj.

Uploaded by

nawrashamdone
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/ 9

DATA STRUCURE AND ALGORITHEM

TIC TAC TOE GAME PROJECT

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.

The goal of this project :


The main goal is to build a game in c++ using data structure and oop features
To learn more about data structure and aloso having fun building a game using
programming

Our code functions:


1. Definition of the TicTacToe class:
- The TicTacToe class is used to represent the game Tic Tac Toe. The Variety
has the following variants:
- player: determines the current Player (X or O).
- board: represents the playing board, which is a two-dimensional matrix.
- winner: determines the winner of the game.

2. Stack class definition:


- The Stack class is used to implement the "stack" data structure. The Variety
has the following variants:
- MAX_SIZE: specifies the maximum stack size.
- items: a matrix for storing stack elements.
- top: determines the current position of the top element in the stack.
3. The Ticktactoe()function:
- A constructor is defined for the Ticktactoe class that initializes the game. It is
done in this constructor:
- Set the current player to X and initialize other variables.
- Initialize the gameboard with numbers from 1 to 9.

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.

9. Stack class functions:


- The functions push (), pop (), isEmpty (), showWins() and countWins () are
used to perform addition and deletion operations, check for emptiness,
display items and count the win for each player in the stack structure.
10. The main()function:
- The main function of the program.
- Contains a list of options to choose between playing, win display, and output
results.
- A loop is used to repeat operations until the exit option is chosen.

Our implementation code for the game :


#include <iostream>
using namespace std;

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;

// Convert position to array indices


int row = (position - 1) / 3;
int col = (position - 1) % 3;

char playerChar = (player == 'X') ? 'X' : 'O';

// Check if the chosen position is valid


if (position < 1 || position > 9 || board[row][col] == 'X' ||
board[row][col] == 'O')
{
cout << "Invalid position! Please choose again." << endl;
inputXO(); // Recursively call inputXO() until a valid position is
entered
return;
}

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

void push(char value)


{
if (top == MAX_SIZE - 1)
{
cout << "Stack overflow!" << endl;
return;
}
items[++top] = value;
}

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 countWins(char player)


{
int count = 0;
for (int i = 0; i <= top; i++)
{
if (items[i] == player)
{
count++;
}
}
return count;
}
};

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

cout << "Do you want to play again? (Y/N): ";


cin >> playAgain;
}
break;
case 2:
cout << "Wins for Player X: " << winStack.countWins('X') << endl;
cout << "Wins for Player O: " << winStack.countWins('O') << endl;
break;
case 3:
char popped = winStack.pop();
if (popped != '-')
{
cout << "Result popped: " << popped << endl;
}
break;
}
}

return 0;
}

The code results:


References:
The all lectures by Dr. Turkan Ahmed Khalil

• Object-Oriented Programming in C++, Fourth Edition Robert Lafore • C++ PROGRAMMING:


FROM PROBLEM ANALYSIS TO PROGRAM DESIGN • FIFTH EDITION D.S. MALIK • Data
Structures and Algorithms in C++ Second Edition Adam Drozdek

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy