Final Project Documentation
Final Project Documentation
Session 2023-2027
Submitted By:
Faiza Kiran Safdar 2023-CS-189
Ayesha Batool 2023-CS-171
Submitted To:
Sir Nazeef ul Haq
Overview of 2048--------------------------------------------------------------------------------------4
Summary------------------------------------------------------------------------------------------------5
Motivation----------------------------------------------------------------------------------------------5
Objectives:----------------------------------------------------------------------------------------------5
Target Audience:--------------------------------------------------------------------------------------6
Stakeholders:-------------------------------------------------------------------------------------------6
Operational Details:----------------------------------------------------------------------------------7
Use Cases:----------------------------------------------------------------------------------------------7
Wireframes---------------------------------------------------------------------------------------------8
Future Enhancements--------------------------------------------------------------------------------8
AI Implementation------------------------------------------------------------------------------------9
Debugging Techniques----------------------------------------------------------------------10
Performance Optimization------------------------------------------------------------------11
How to play-------------------------------------------------------------------------------------------11
Implementation--------------------------------------------------------------------------------------11
Initial Setup------------------------------------------------------------------------------------12
Board Initialization---------------------------------------------------------------------------12
Game Design-----------------------------------------------------------------------------------------12
Game Rules------------------------------------------------------------------------------------12
Technical Specifications---------------------------------------------------------------------------13
Development Environment-----------------------------------------------------------------13
Additional Features----------------------------------------------------------------------------13
AI Bot----------------------------------------------------------------------------------------14
Undo Functionality------------------------------------------------------------------------14
Game Mechanics---------------------------------------------------------------------------------14
Scoring System------------------------------------------------------------------------------14
User Interactions----------------------------------------------------------------------------15
Code Breakdown:--------------------------------------------------------------------------------15
Event Handling--------------------------------------------------------------------------------15
Function Descriptions-----------------------------------------------------------------------16
Summary
The 2048 game is a single-player sliding block puzzle game that involves combining
numbered tiles on a 4x4 grid to create a tile with the value of 2048. The game is
simple yet addictive, requiring strategic thinking and planning. It has gained immense
popularity due to its challenging nature and the satisfaction of achieving high
scores.The 2048 game is a single-player sliding block puzzle game that challenges
players' strategic thinking and problem-solving skills. Developed using Python and
the Pygame library, this project incorporates various data structures such as lists,
dictionaries, sets, stacks, queues, graphs, and linked lists to enhance gameplay and
functionality. The goal is to demonstrate the practical applications of these data
structures in a real-world scenario while providing an engaging and educational
experience for players and developers alike.
Motivation
The primary motivation behind this project is to explore the practical applications of
data structures in game development. By integrating different data structures into the
Objectives:
Develop a fully functional 2048 game:
Create a working version of the 2048 game using Python and Pygame.
Utilize lists, dictionaries, sets, stacks, queues, graphs, and linked lists to manage
game logic, AI, and additional features.
Showcase how different data structures can be effectively used in game development
to manage complexity and improve performance.
Target Audience:
Students and educators:
Game developers:
Gain insights into integrating data structures into game logic and enhancing user
experience.
Enthusiasts:
Stakeholders:
Students:
Educators:
Use the project as a teaching tool to explain the importance of data structures in real-
world applications.
Game developers:
Learn how to incorporate data structures into game logic and design to create a more
engaging user experience.
Enthusiasts:
Operational Details:
Development Environment:
The game will be developed using Python 3.x and the Pygame library. Pygame
provides the necessary tools for handling graphics, sounds, and user input.
Code Repository:
A version control system like GitHub will be used to manage the codebase and
collaborate with other developers.
Background music and sound effects will be added using Pygame's mixer module.
The music will be sourced from the "dsa" folder and will play continuously during the
game. The music will stop when the game quits.
Game Assets:
Custom graphics for tiles, buttons, and backgrounds will be created to enhance the
visual appeal of the game.
Use Cases:
Start Game:
The player initiates a new game by clicking the "New Game" button, which resets the
board and score.
Move Tiles:
Undo Move:
The player can revert to the previous game state by clicking the "Undo" button.
AI Move: The player can ask the AI to make an optimal move by clicking the "AI
Move" button.
Game Over:
When no more moves are possible, a game over screen is displayed with 45 blessings
or wishes and a "Congratulations" message.
Wireframes
Figure3:Game Playing
Figure7:Score Board
Dictionaries
Sets
Ensures that the AI does not revisit the same state, optimizing move selection.
Stacks
Queues
Used in the AI implementation for breadth-first search (BFS) to find the optimal
moves.
Graphs
Represent possible moves and paths in the AI to determine the best move sequence.
Linked Lists
Used to store and display the move history, allowing players to track their actions.
Algorithms implemented for features like sorting scores or searching through the
move history for specific actions.
AI Implementation
AI Algorithm
Tile Colors: Vary based on the tile value, with higher values having more intense
colors.
Fonts: Default Pygame font with a font size of 55 for tiles and 36 for buttons,
ensuring readability and clarity.
Background music is played continuously during the game to enhance the player's
experience. The music is sourced from the "dsa" folder and is managed using
Pygame's mixer module
Sound Effects
Sound effects are played for actions such as merging tiles, starting a new game, and
achieving a new high score.
Various test cases are designed to ensure the game functions correctly. These include
tests for tile movements, merging behavior, score calculation, and game over
Includes tests for tile movements, merging, score calculation, and AI moves.
Debugging Techniques
Debugging tools and techniques are used to identify and fix issues in the code.
Performance Optimization
How to play
This is a tile-sliding game. It means, it has is a board-game which has numbers on
tiles. We slide the tiles and merge the numbers. We can only slide the tiles to Up,
Down, Left, and Right directions. Choosing a direction will mean every element on
the board will move to that direction.
Only two same numbers will merge; for example, we can merge 2 and 2, but we
cannot merge 2 and 4. The goal is to merge the numbers until we get 2048. Merging
numbers to 2048 will mean we have won the game. Otherwise, if there are no empty
tiles left and we cannot merge any more numbers, the game is over and we lose.
At the beginning of the game, two 2's are randomly generated to start the game. As
we swipe from left to right or any other direction, with every move a random number
2 or 4 is added to board. This addition of random number happens whether the other
tiles are merged or not.
Implementation
We will be explaining the steps as we implement the code. For this game, we have
chosen a 4X4 matrix; that would be a square board with 4 rows and 4 columns.
"Frame" widget in Tkinter keeps other widgets organized and working together. The
colors for the board and the tiles are randomly picked and methods are defined for
every function of the game.
Initial Setup
The initial setup involves importing necessary libraries, defining constants for colors,
fonts, and tile sizes, and initializing the Pygame environment. The game window is
created with specified dimensions, and fonts are loaded for rendering text.
Board Initialization
The game board is represented as a 2D list (4x4 grid), initialized with zeros. Two
random tiles with values of 2 or 4 are placed on the board to start the game.
Tiles can be moved in four directions: left, right, up, and down. Functions are
implemented to handle tile sliding and merging. When two tiles of the same value
collide, they merge into a single tile with the combined value.
After each move, a new tile with a value of 2 or 4 is placed in an empty spot on the
board. The place_random_tile() function ensures that the new tile appears in a
random empty cell.
Game Design
Game Rules
Each turn, a new tile (with a value of 2 or 4) appears in an empty spot on the board.
Tiles slide as far as possible in the chosen direction until they are stopped by another
tile or the edge of the grid.
If two tiles of the same number collide while moving, they merge into a tile with the
total value of the two tiles that collided.
The game is won when a tile with the value of 2048 appears on the board. Players can
continue playing to reach higher scores.
Technical Specifications
Development Environment
The 2048 game is developed using Python 3.x and the Pygame library, a set of Python
modules designed for writing video games.
Programming Language
Python: Chosen for its simplicity, readability, and extensive library support. Python
allows for rapid development and prototyping, making it an ideal choice for game
development.
Additional Features
AI Bot
Uses algorithms like BFS to evaluate possible moves and select the best one.
Undo Functionality
Background music sourced from the "dsa" folder plays continuously during the game.
Sound effects are added for tile movements and merges to enhance the gaming
experience.
Game Mechanics
Scoring System
The game is won when a tile with the value of 2048 appears on the board.
User Interactions
Buttons are provided for starting a new game, undoing the last move, and invoking
the AI to make a move.
Visual and audio feedback enhances user interactions and provides a satisfying
gaming experience.
Code Breakdown:
The main game loop handles the game's logic, updating the game state, and rendering
the game board.
Event Handling
Function Descriptions
Each function is documented with a description of its purpose, parameters, and return
values.
Clear and concise descriptions help in understanding the code and its functionality.
Examples demonstrate how different data structures and algorithms are implemented
in the game.
Future Enhancements
Multiplayer Mode:
Implementing a multiplayer mode where players can compete against each other on
separate boards or on the same board in turn-based gameplay.
Introducing power-ups that provide special abilities, such as merging multiple tiles at
once or doubling the value of a tile.
Leaderboards:
Adding online leaderboards to display high scores and allow players to compete
globally.
Customizable Themes:
Allowing players to customize the appearance of the game, including tile colors,
backgrounds, and fonts.