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

0% found this document useful (0 votes)
7 views10 pages

Project Report

This document outlines a project to develop a console-based version of the 2048 game using C++ and object-oriented programming principles. It details the game's mechanics, objectives, class structure, and OOP concepts utilized, such as encapsulation and inheritance. The project aims to create an engaging user experience while ensuring a modular and scalable codebase.

Uploaded by

tahsin2209016
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)
7 views10 pages

Project Report

This document outlines a project to develop a console-based version of the 2048 game using C++ and object-oriented programming principles. It details the game's mechanics, objectives, class structure, and OOP concepts utilized, such as encapsulation and inheritance. The project aims to create an engaging user experience while ensuring a modular and scalable codebase.

Uploaded by

tahsin2209016
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/ 10

Open Ended Lab

On

2048 GAME
using C++ Programming Language

Course No: CSE 2100


Course Name: Object Oriented Programming Laboratory

Submitted By-
Fariha Tahsin
Roll: 2209016

8th August, 2025

Department of Electronics and Communication Engineering


Khulna University of Engineering & Technology
Khulna-9203, Bangladesh.
Introduction
Project Overview
This project aims to develop a console-based implementation of the 2048 game using C++
and object-oriented programming (OOP) principles.
Game Description
2048 is a sliding tile puzzle game that challenges players to combine numbered tiles into larger
values. The objective is to create the 2048 tile by merging tiles of the same value. Each move
shifts tiles in one direction (Up, Down, Left, or Right), merging adjacent tiles of the same value
and adding a new tile to the grid.
Key gameplay mechanics:
1. Sliding tiles in one of four directions (Up, Down, Left, Right).
2. Merging tiles with the same value during a slide.
3. Randomly adding new tiles (2, 4, 8, 16 or 32) to the grid after each move.
4. Ending the game when no valid moves remain or when the player achieves the 2048
tile.
Objectives
1. To implement the basic mechanics of 2048, including tile sliding, merging, and grid
updates.
2. To provide clear feedback for win or game-over states.
3. To use OOP principles (class, object, encapsulation, inheritance, abstraction and
polymorphism) to ensure a modular and reusable codebase.
4. To display the grid in a visually appealing format and provide intuitive controls (W,
A, S, D for movement, Q to quit).
5. To load saved game from file.

Features
1. Dynamic Grid Updates:
oA 4x4 grid updates in real-time after each move.
oNew tiles (2, 4, 8, 16 or 32) are generated dynamically based on gameplay
progression. For example: First there will be 2 and 4 tile. After five moves,
2, 4 and 8 will be generated. Then after ten moves, 2, 4, 8 and 16 will be
generated randomly.
2. Win and Game-Over Conditions:
o Players win upon creating the 2048 tile.
o The game ends if no valid moves are available.

2
3. Tile Sliding and Merging:
o Will be implemented as modular strategies for each direction (Move_Up,
Move_Down, Move_Left, Move_Right).
4. Interactive Console Interface:
o A clean and intuitive console interface will display the grid, with clear
prompts for user input.

Here’s a simple chart that represents the flow of the 2048 game implementation.

3
Class Structure
1. Grid Class:
o Managing the grid state (a 2D array).
o Initializing the grid.
o Tile Generation: Randomly adding a new tile (2, 4, 8, 16 or 32) to an empty
position after each move.
o Win/ Loss Detection: Detecting when the 2048 tile is created or when no
valid moves are available.
o Grid Display: Formatting and display the grid for a clear user interface.
o Asks player to restart or quit.
o Lets the player quit anytime and resume later.

2. Move Classes (Move_Up, Move_Down, Move_Left, Move_Right):


o Implementing the specific behaviors for moving and merging tiles in each
direction.

3. Game_2048 Class:
o Managing the game loop and interactions with the Grid object.
o Accepting user input (W (up), A (left), S (down), D (right), Q(quit)).
o Managing the game state and triggering win/loss conditions.

Inheritance used in this project


1. Single Inheritance: Base_Move only inherits from one parent class
(Move_Strategy), this is single inheritance.
2. Hierarchical Inheritance: Move_Up, Move_Down, Move_Left, Move_Right all
inherit from Base_Move. Since multiple classes inherit from the same base class,
this is hierarchical inheritance.

4
5
2048 GAME -

MAIN FUNCTION
PRINT "W for up, A for left, S for down, D for right"
CREATE game new Game2048
CALL game.play()

CLASS: Game2048
CONTAINS:
- grid : Grid
METHOD play():
SEED random number generator
IF user wants to load saved game:
IF load fails:
PRINT "No saved game. Starting new."
CALL grid.initialize()
ELSE:
CALL grid.initialize()
LOOP forever:
CALL grid.display()
IF grid.hasWon():
PRINT "You won!"
IF user wants to restart:
CALL grid.reset()
ELSE:
BREAK
IF grid.hasNoMoves():
PRINT "Game Over!"
IF user wants to restart:
CALL grid.reset()

6
ELSE:
BREAK
ASK user for move (W/A/S/D/E/Q)
SWITCH input:
CASE W → strategy ← MoveUp
CASE A → strategy ← MoveLeft
CASE S → strategy ← MoveDown
CASE D → strategy ← MoveRight
CASE E → CALL grid.saveToFile()
PRINT "Game saved"
CONTINUE
CASE Q → PRINT "Thanks for playing!"
BREAK
DEFAULT → PRINT "Invalid input"
CONTINUE
CALL grid.move(strategy)

ABSTRACT CLASS: MoveStrategy


ABSTRACT METHOD:
execute(grid[4][4])

CLASS: BaseMove (inherits MoveStrategy)


METHOD slide(array):
SHIFT all non-zero numbers to the left
METHOD merge(array):
MERGE adjacent same numbers (a + a → 2a)
METHOD slideAndMerge(array):
CALL slide()
CALL merge()
CALL slide()

7
SUBCLASSES: MoveUp, MoveDown, MoveLeft, MoveRight
Each class overrides execute(grid):
FOR each row/column:
EXTRACT into temp[]
IF direction is Down or Right reverse temp[]
CALL slideAndMerge(temp)
IF direction is Down or Right reverse temp[] back
PUT temp[] back into grid

OOP Concept How it’s used

Encapsulation Grid hides all board logic inside one class

The game only tells the grid to "move" — it doesn’t know


Abstraction
the internals

MoveUp, MoveDown inherit from BaseMove and


Inheritance
MoveStrategy

Grid calls strategy->execute() — and actual movement


Polymorphism
depends on which subclass was passed

8
Sample output:

9
Conclusion
This project is a practical implementation of the popular 2048 game using C++ and object-
oriented programming (OOP) principles. The game demonstrates the successful use of key
OOP concepts such as encapsulation, inheritance, polymorphism, and abstraction to create a
clean, modular, and scalable codebase. The console-based design ensures that players enjoy an
interactive and engaging experience, while the modular architecture allows for easy future
enhancements like scoring, undo functionality, or even a graphical interface. This project
highlights how OOP can be effectively used to build robust and maintainable software
solutions.

10

You might also like