Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views21 pages

DSA1

The document is a project report on the development of a digital Tic-Tac-Toe game as part of a Data Structures course. It details the game's functionality, including single-player and two-player modes, user interface, game logic, and AI implementation. The report concludes with an overview of the project's outcomes and potential areas for future enhancement.

Uploaded by

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

DSA1

The document is a project report on the development of a digital Tic-Tac-Toe game as part of a Data Structures course. It details the game's functionality, including single-player and two-player modes, user interface, game logic, and AI implementation. The report concludes with an overview of the project's outcomes and potential areas for future enhancement.

Uploaded by

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

TIC TAC TOE GAME

Project Based Learning (PBL) Report

for the course

Data Structures – 20CS21001

BACHELOR OF TECHNOLOGY

IN

COMPUTER SCIENCE AND ENGINEERING

By

D.SUSHANTH 24R15A0509

E.UDAY BHARGAV 24R15A0510

M.SAI VIVEK 23R11A0581


Under the guidance of

V. Sravanthi

Department of Computer Science and Engineering

Accredited by NBA

Geethanjali College of Engineering and Technology


(UGC Autonomous)

(Affiliated to J.N.T.U.H, Approved by AICTE, New Delhi)

Cheeryal (V), Keesara (M), Medchal.Dist.-501 301.

December -2024

TABLE OF CONTENTS:

S.No. Contents Page No

1 Introduction

2 System overview
3 DATA FLOW

4 Output

5 CONCLUSION

6 REFERENCES

INTRODUCTION
The Tic-Tac-Toe game is a classic two-player board game that is simple to
understand but offers an engaging experience. The game is played on a 3x3
grid, where two players take turns marking their respective symbols (X and O)
on the grid, with the objective of being the first to align three of their symbols
horizontally, vertically, or diagonally.

In this project, we have developed a digital version of the Tic-Tac-Toe game


that can be played on a computer or mobile device. The game supports both
single-player (against the computer) and two-player modes. It incorporates
basic game logic to check for winning conditions, handle invalid moves, and
allow players to restart the game once a winner is determined or the game
ends in a draw.

OUTCOMES

 Fully Functional Game: A working version of Tic-Tac-Toe that can be


played by two players or one player against an AI (computer opponent).
 User Interface: A simple, intuitive interface that allows users to easily play
the game, make moves, and view the results of the game (win, lose, or draw).

 Game Logic: Implementation of the core game mechanics, including turn-


taking, checking for winning conditions, and preventing invalid moves.

 Interactive Experience: Engaging gameplay with options for players to


restart the game or exit as needed.

1. Code Structure: Clean and organized code, demonstrating good software


development practices and understanding of core programming principles.

OBJECTIVE

 To Develop Basic Game Functionality: Create a working version of


the Tic-Tac-Toe game with features such as move validation, win conditions, and
player interaction.

 To Practice Core Programming Concepts: Utilize programming


fundamentals such as arrays, loops, conditionals, and functions to design and
implement the game.

 To Implement Game Logic: Implement the rules of the game, including


how to check for winning conditions (horizontal, vertical, diagonal) and handle a
draw.

 To Create an AI Player: For single-player mode, design a basic AI that


can challenge the player. The AI will make random moves or follow simple
strategies.

To Improve User Interface: Build a user-friendly interface that enhances


the user experience and provides clear game states (e.g., whose turn it is, whether
someone has won, or if the game is a draw).

 Interactive Interface: Provide a clear, interactive menu-driven interface


for ease of use.

SYSTEM ARCHITECTURE
1. User Interface (UI) Layer

The UI layer is responsible for interacting with the user. It handles input and
output between the player and the game system.

 Main Menu: Allows the player to choose between different modes


(Single Player, Two Player, Restart, Exit).
 Game Grid: Displays a 3x3 grid to represent the game board. The grid
updates based on player moves and displays results (win/lose/draw).
 Player Status: Displays information about the current player (X or O),
whose turn it is, and the game status (win/loss/draw).
 Messages: Displays messages such as "Player X Wins", "Player O
Wins", "It's a Draw", etc.

2. Game Logic Layer

The Game Logic Layer is responsible for handling the rules and conditions of
the game. It processes the player's moves, checks for winning conditions, and
manages game state transitions.

 Turn Management: Controls the alternating turns between Player 1


and Player 2, or Player 1 and the AI.
 Move Validation: Ensures that players can only mark empty spaces
on the grid. Invalid moves (e.g., trying to overwrite an already marked cell)
are rejected.
 Winning Condition Check: Continuously checks for winning
conditions (horizontal, vertical, or diagonal lines) after each move.
 Game State: Keeps track of the overall game state (e.g., ongoing, win,
draw) and triggers the appropriate actions when the game ends.

3. AI Player (Optional)

For single-player mode, the AI Player generates valid moves for the computer.

 AI Move Generation: The AI makes random moves or uses a simple


algorithm to decide its move.
 Difficulty Level: If implemented, the AI may offer multiple difficulty
levels, such as easy (random moves) or hard (strategic moves).

4. Data Layer

The Data Layer is responsible for managing and storing the game data, such
as the game board and the current game state.

 Game Board Representation: A 3x3 array or grid structure that


stores the current state of the board (e.g., which cells are marked by 'X',
'O', or are empty).
 Game History: Stores previous moves for replay or undo functionality,
if applicable.
 Game Results: Tracks the result of the game (win, loss, draw) and
keeps the game logic informed.

5. Controller Layer

The Controller Layer acts as the intermediary between the UI, Game Logic,
and Data layers. It manages user inputs and coordinates the flow of the
game.

 Player Input Handling: Receives input from the user (clicks or


keyboard input) and updates the game state accordingly.
 State Transitions: When a player makes a move, the controller
updates the game board, triggers the win condition check, and determines
whether the game is over or continues.
 Result Display: After a game concludes, the controller will update the
UI to reflect the winner or if the game ended in a draw.
Data Flow
1. Player Action: A player inputs their move via the UI (clicking on an
empty cell).
2. Input Validation: The Controller Layer validates the move to ensure
it’s within the rules of the game.
3. Game State Update: Once the move is validated, the Game Logic
Layer updates the board.
4. Winning Condition Check: After each move, the Game Logic checks
for a winner (or a draw).
5. AI Move (if applicable): In Single Player mode, once the player
completes their move, the AI makes its move (if it's the computer's turn).
6. Endgame: If a winner is found or the game results in a draw, the system
updates the UI to display the result.

+-------------------+ +--------------------+ +-----------------------+

| User Interface | <-----> | Controller Layer | <-----> | Game Logic Layer |

+-------------------+ +--------------------+ +-----------------------+

^ ^ ^

| | |

| | |

+-------------------+ +--------------------+ +-----------------------+

| Data Layer | <-----> | AI Player (if any)| | Game Board (3x3) |

+-------------------+ +--------------------+ +-----------------------+

IMPLEMENTATIONS

Source code:

#include <stdbool.h>

#include <stdio.h>
#include <stdlib.h>

#include <time.h>

#define COMPUTER 1

#define HUMAN 2

#define SIDE 3

#define COMPUTERMOVE 'O'

#define HUMANMOVE 'X'

// Node structure for linked list

struct Node {

int row, col; // Coordinates of the cell

char value; // 'X', 'O', or ' '

struct Node *next; // Pointer to the next node

};

// Structure for the board

struct Board {

struct Node *head; // Head of the linked list

};

// Create a new node

struct Node* createNode(int row, int col, char value) {

struct Node* newNode = (struct


Node*)malloc(sizeof(struct Node));

newNode->row = row;

newNode->col = col;

newNode->value = value;
newNode->next = NULL;

return newNode;

// Initialize the board as a linked list

struct Board* initializeBoard() {

struct Board* board = (struct Board*)malloc(sizeof(struct


Board));

board->head = NULL;

struct Node *current = NULL;

// Create a 3x3 board

for (int i = 0; i < SIDE; i++) {

for (int j = 0; j < SIDE; j++) {

struct Node* newNode = createNode(i, j, ' ');

if (board->head == NULL) {

board->head = newNode;

current = board->head;

} else {

current->next = newNode;

current = newNode;

return board;

// Get the node at a specific position


struct Node* getNode(struct Board* board, int row, int col) {

struct Node* temp = board->head;

while (temp) {

if (temp->row == row && temp->col == col)

return temp;

temp = temp->next;

return NULL; // Node not found

// Display the board

void showBoard(struct Board* board) {

struct Node* temp = board->head;

printf("\n\n");

for (int i = 0; i < SIDE; i++) {

for (int j = 0; j < SIDE; j++) {

struct Node* node = getNode(board, i, j);

printf(" %c ", node->value);

if (j < SIDE - 1) printf("|");

printf("\n");

if (i < SIDE - 1) printf("---|---|---\n");

printf("\n");

// Check if the board is full


bool isBoardFull(struct Board* board) {

struct Node* temp = board->head;

while (temp) {

if (temp->value == ' ')

return false;

temp = temp->next;

return true;

// Check if there's a winner

bool checkWinner(struct Board* board, char move) {

// Check rows

for (int i = 0; i < SIDE; i++) {

bool rowWin = true;

for (int j = 0; j < SIDE; j++) {

struct Node* node = getNode(board, i, j);

if (node->value != move) {

rowWin = false;

break;

if (rowWin) return true;

// Check columns

for (int j = 0; j < SIDE; j++) {


bool colWin = true;

for (int i = 0; i < SIDE; i++) {

struct Node* node = getNode(board, i, j);

if (node->value != move) {

colWin = false;

break;

if (colWin) return true;

// Check diagonals

bool diagWin1 = true, diagWin2 = true;

for (int i = 0; i < SIDE; i++) {

struct Node* node1 = getNode(board, i, i);

struct Node* node2 = getNode(board, i, SIDE - i - 1);

if (node1->value != move) diagWin1 = false;

if (node2->value != move) diagWin2 = false;

return diagWin1 || diagWin2;

// Declare the winner

void declareWinner(int whoseTurn) {

if (whoseTurn == COMPUTER)

printf("COMPUTER has won\n");

else
printf("HUMAN has won\n");

// Play Tic-Tac-Toe

void playTicTacToe(int whoseTurn) {

struct Board* board = initializeBoard();

int moveIndex = 0, row, col;

printf("Tic-Tac-Toe Game\n");

showBoard(board);

while (!isBoardFull(board)) {

if (whoseTurn == COMPUTER) {

// Simple AI for computer: Choose the first empty


spot

struct Node* temp = board->head;

while (temp) {

if (temp->value == ' ') {

temp->value = COMPUTERMOVE;

printf("COMPUTER chose: (%d, %d)\n", temp-


>row + 1, temp->col + 1);

break;

temp = temp->next;

showBoard(board);

if (checkWinner(board, COMPUTERMOVE)) {

declareWinner(COMPUTER);
return;

whoseTurn = HUMAN;

} else {

printf("Enter your move (row and column): ");

scanf("%d %d", &row, &col);

row--;

col--;

struct Node* node = getNode(board, row, col);

if (node && node->value == ' ') {

node->value = HUMANMOVE;

showBoard(board);

if (checkWinner(board, HUMANMOVE)) {

declareWinner(HUMAN);

return;

whoseTurn = COMPUTER;

} else {

printf("Invalid move! Try again.\n");

printf("It's a draw!\n");

// Driver function
int main() {

playTicTacToe(COMPUTER);

return 0;

14

OUTPUTS:
Tic-Tac-Toe Game

| |

---|---|---

| |

---|---|---

| |

COMPUTER chose: (1, 1)

X| |

---|---|---

| |

---|---|---

| |

Enter your move (row and column): 2 2

X| |

---|---|---
|X|

---|---|---

| |

COMPUTER chose: (1, 3)

X| |O

---|---|---

|X|

---|---|---

| |

Enter your move (row and column): 3 1

X| |O

---|---|---

|X|

---|---|---

X| |

COMPUTER chose: (2, 1)

X|O|O

---|---|---

X| |

---|---|---

X| |
COMPUTER has won

CONCLUSION
In conclusion, our Tic-Tac-Toe game project successfully demonstrates
the application of fundamental programming concepts such as linked lists,
loops, conditionals, and user input handling. The project involves both the
development of basic game mechanics and user interaction, providing an
interactive platform for users to play the classic Tic-Tac-Toe game either
against a computer or another player.

Throughout the project, we implemented the following key features:

1. Game Logic: The core functionality, including turn-taking, move


validation, win condition checks, and handling draws, was carefully
implemented to ensure a smooth and error-free gaming experience.
2. Data Representation: We used a linked list to represent the game
board, allowing us to effectively store and manipulate the grid values while
simulating a 3x3 board structure.
3. AI Implementation: A simple AI was developed for the single-player
mode, which chooses moves based on the first available empty space,
providing a basic level of challenge for the player.
4. User Interface: A text-based interface was designed to allow easy
interaction between the player and the game, showing the current state of
the board and prompting for user input during the game.

While the project effectively showcases core programming skills and the
implementation of a classic game, it also has areas for potential
enhancement. Future improvements could involve refining the AI to make
smarter decisions, introducing a graphical user interface (GUI), or expanding
the game to allow more complex game modes.

Overall, this project provided a hands-on opportunity to apply key software


development principles while producing a fun and interactive game. The
project successfully met its objectives and serves as a valuable learning tool
for understanding game design and fundamental programming techniques.
18

REFERENCES

1.Tic-Tac-Toe Algorithm Explanation

https://www.geeksforgeeks.org/c-programming-language/.

2.ChatGPT: OpenAI's language model

https://chat.openai.com.
19

You might also like