You are on page 1of 13

2048 of the Report

Hadi #1 Ting #2

Abstract
This report summarizes the “2048” game-making programming using C# in Visual
Studio 2022. The objectives of this program are to make simple games, easy to
understand game, and fun to play for long terms. The objective of this game is to get
the highest score from making high-value of block. The methodology for this program
is the designing, implementation, and testing stages. The key findings for the 2048
game encompass its successful implementation, user-friendly interface, integration of
sound effects, and also identification of occasional errors in declaring the game over.
In conclusion, this program is a user friendly game because it simpleness to play.

1. Introduction

This report introduces the creation of a "2048" game through C# in Visual Studio
2022, aiming to provide a straightforward, comprehensible, and enjoyable gaming
experience while striving for the highest score through block combinations. The
significance lies in crafting an interface that is user-friendly and addressing occasional
errors in determining game over. The report's structure includes an introduction to the
project's purpose, the chosen object-oriented programming undertaking, and its
importance.

In the subsequent sections, the report explores the methodology used, with a focus on
the design, implementation, and testing stages. Key findings emphasize the successful
implementation of the game, its user-friendly interface, incorporation of sound
effects, and the identification of occasional game-over errors. The report concludes by
underscoring the program's user-friendly nature, attributed to its simplicity and ease
of play.
2. Project Overview

The primary goals of the project are to create a "2048" game using C# in Visual
Studio 2022, with the overarching objective of developing a simple, easy-to-
understand, and enjoyable gaming experience that promotes long-term engagement.
Specifically, the goal within the game is to achieve the highest score through the
combination of blocks to form high-value ones. The requirements for the project
include designing, implementing, and testing the game, placing significant emphasis
on ensuring a user-friendly interface and addressing occasional errors related to
declaring game over.

The problem domain revolves around users' desire for a simple, easy-to-understand,
and engaging game experience that can be played for extended periods. Typically,
users also want features such as scoring and best score tracking to add challenges to
the gameplay. The solution involves creating a 2048 game controlled simply by arrow
buttons, ensuring the game has no elements other than the game over condition, and
implementing a scoring system that records and saves the achieved scores as the best
score.

3. System Architecture

The architecture of the provided code is a simple event-driven design based on the
Windows Forms framework. Here's a concise breakdown:

1. User Interface (UI) Layer:

 Form1 Class: The main class representing the UI and game logic.

PictureBox and Label Controls: Used for rendering the game grid, tiles, and
scores.
 Buttons: UI elements for user interaction (e.g., "Refresh" and "Again").
2. Game Logic Layer:
 Tile Generation and Movement: Methods like GenerateNewPic, SlideLeft,
moveLeft, etc., handle the core game logic.
 Scoring: Tracks and updates the game score.
 Color Updates: ChangeColor method updates tile colors based on their values.
 Game Over Check: IsGameOver method checks for game over conditions.
3. Sound Layer:
SoundPlayer Class: Manages sound effects during tile merging and game over.
4. Randomization Layer:
Random Class: Generates random positions for new tiles.
5. Key Event Handling:

OnKeyboardPressed Method: Handles keyboard input events to trigger tile


movements.
6. System State Layer:

Game Grid State: Maintains the state of the game grid, scores, and game over
status.

4. Implementation

1. Game Logic :
The main class representing the form of the application, handling UI and game logic.
Key Methods:
CreateMap(): Initializes the game grid, creating PictureBox controls for each cell.
GenerateNewPic(): Generates a new tile with the value "2" at a random empty
position.
ChangeColor(): Updates the colors of the tiles based on their values.
OnKeyboardPressed(): Handles keyboard input events to move tiles and trigger game
actions.
Slide(), SlideLeft(), swap(), mirrorupdown(), transpose(): Implement the core logic
for moving and merging tiles in different directions.
moveLeft(), moveRight(), moveUp(), moveDown(): Coordinate the tile movement in
respective directions.
IsGameOver(): Checks if the game is over by verifying if the grid is full and no more
valid moves are possible.
btnRefresh_Click(), Again_Click(): Event handlers for the "Refresh" and "Again"
buttons.
2. Tile Color and Value Mapping:
This event handler make the color of each value different. this method is to
help the user to recognize different value of block.

3. Tile Movement and Merging:


this event handler make the tile move according to the arrow key. also, make
the two same value block merging when meet each other in the same direction.

4. Movement and Game Over Logic:


moveLeft(), moveRight(), moveUp(), moveDown() Methods:
Coordinate tile movements in different directions by using transposition and
mirroring.
IsGameOver() Method:
Checks if the game is over by verifying if the grid is full and no more valid moves are
possible

5. UI Interaction and Button Click Events:


btnRefresh_Click() and Again_Click() Methods:
Event handlers for the "Refresh" and "Again" buttons, resetting the game state.

5. Key Features
The 2048 game implementation incorporates various key features that contribute to its
functionality and user experience. Object-oriented principles have been applied to
enhance code modularity, reusability, and maintainability.

1. Grid Initialization and User Interface:


Feature: The game initializes a 4x4 grid of tiles represented by PictureBox controls in
the Windows Forms interface.
Object-Oriented Application: The grid is represented using a 2D array of PictureBox
controls (pic[,]). Labels (labels[,]) are used for displaying the numerical values on the
tiles.

2. Tile Movement and Merging:


Feature: Users can move tiles in four directions (up, down, left, right) using arrow
keys.
Object-Oriented Application: Methods like Slide(), SlideLeft(), moveUp(),
moveDown(), etc., encapsulate the logic for tile movement and merging. This follows
the principles of encapsulation and abstraction.

3. Tile Value and Color Mapping:


Feature: Tiles dynamically change color based on their numerical values.
Object-Oriented Application: The ChangeColor() method encapsulates the logic for
mapping tile values to colors. This allows for easy modification and extension,
promoting code maintainability.

4. Random Tile Generation:


Feature: New tiles with the value "2" are randomly generated on the grid after each
move.
Object-Oriented Application: The GenerateNewPic() method handles the random
generation of tiles, contributing to code modularity.

5. Score Tracking:
Feature: The game keeps track of the player's score, updating it when tiles are
merged.
Object-Oriented Application: Score-related logic is encapsulated within methods and
properties, adhering to encapsulation principles.

6. Game Over Detection:


Feature: The system detects and displays a "Game Over" message when there are no
more valid moves.
Object-Oriented Application: The IsGameOver() method encapsulates the logic for
determining whether the game is over, promoting code readability.

7. Sound Effects:
Feature: The game includes sound effects during tile merging and upon reaching a
game over state.
Object-Oriented Application: SoundPlayer objects are encapsulated within the Form1
class, providing a modular approach for managing sound effects.

8. Restart Functionality:
Feature: Users can restart the game by clicking the "Again" button.
Object-Oriented Application: The restart functionality is encapsulated within the
Again_Click() method, promoting code modularity and reusability.

9. Best Score Tracking:


Feature: The game keeps track of the best score achieved.
Object-Oriented Application: Best score-related logic is handled within the
btnRefresh_Click() and Again_Click() methods, encapsulating the functionality.

10. Object-Oriented Principles:


Encapsulation: Methods, properties, and controls are encapsulated within the Form1
class, promoting information hiding and reducing complexity.
Abstraction: Complex game logic, such as tile movement and merging, is abstracted
into methods, enhancing code readability and maintainability.
Modularity and Reusability: The code is organized into methods, promoting
modularity and reusability of components like tile movement and color mapping.

6. Testing and Validation

1. Unit testing
 Block Movement Function:
Test Case: Ensure that the function responsible for moving blocks in
response to user input works correctly.
Result: Passed. The block movement function accurately adjusts block
positions based on user input.
 Score Calculation:
Test Case: Verify that the scoring mechanism calculates the score
correctly for different block combinations.
Result: Passed. The scoring function consistently provides accurate
scores based on block combinations.
 Random Block Generation.
Test Case: Test the randomness of the block generation function to
ensure a fair distribution across the grid.
Result: Passed. The random block generation function creates blocks in
different grid positions, maintaining fairness.
 Game Over Condition:
Test Case: Check if the game over condition is appropriately triggered
when there are no possible moves left.
Result: Passed. The game over condition is accurately identified when
there are no valid moves left.

2. Integration testing
 User Input and Block Movements:
Test Description: Verify that user input (arrow key presses) results in
appropriate block movements on the grid.
Results: Successful. User input consistently corresponds to the
expected movements of blocks, demonstrating a smooth interaction.
 Scoring Mechanism Integration:
Test Description: Ensure that the scoring mechanism integrates
correctly with block combinations, providing accurate scores.
Results: Successful. The scoring system accurately calculates and
updates the score based on combined block values.
 Sound Effects Integration:
Test Description: Confirm that sound effects are appropriately
triggered during key events, such as block combination or game over.
Results: Successful. Sound effects are integrated seamlessly,
enhancing the gaming experience without causing disruptions.
 Game Over Declaration:
Test Description: Test the conditions for declaring a game over,
ensuring it accurately identifies when no further moves are possible.
Results: Partially Successful. Occasionally, the game over condition is
triggered prematurely, even when there are still possible moves. This
aspect requires further investigation.
 Interface and Color Changes:
Test Description: Validate that interface elements and color changes
occur consistently with game events (e.g., block combination leading
to color change).
Results: Successful. Interface elements and color changes align with
the expected outcomes based on game events.

User Acceptance Testing

 Test Scenario: Users interact with the game interface to ensure it is


intuitive and responsive to their actions.
Result: Passed. Users find the interface easy to navigate, and the game
responds promptly to their commands.
 Gameplay Experience:
Test Scenario: Users play the game to assess its overall entertainment
value, ease of understanding, and engagement.
Result: Passed. Users report a positive gameplay experience, finding
the game simple, understandable, and enjoyable for extended periods.
 Scoring and Best Score Tracking
 Test Scenario: Users play the game to evaluate the scoring system's
accuracy and the tracking of the best score.
Result: Passed. Users observe that the scoring system functions
correctly, and the best score is accurately recorded.
 Sound Effects Feedback:
Test Scenario: Users experience the game's sound effects during key
events to assess their appropriateness and impact.
Result: Passed. Users appreciate the integration of sound effects,
enhancing the overall gaming experience without being intrusive.
 Game Over Handling:
Test Scenario: Users play the game to evaluate how accurately and
appropriately the game handles the declaration of a game over.
Result: Partially Passed. Users note occasional premature game over
declarations, suggesting the need for improvement in this aspect.

7. Challenges Faced

Technical hurdles
translate the javascript language:Find the same way to achieve a certain
function
button focus can’t be removed:Put buttons in panel.

Game Logic Complexity:

Managing the complexity of game logic for tile movement, merging, and
game over conditions. Breaking down the logic into modular functions, using
abstraction to simplify complex operations, and ensuring each method has a
single responsibility.

Unexpected obstacles

Language barrier between us make hard to communicate efficiently.

8. Results and Achievements

Successful Game Logic:


The implementation of the game logic has been a significant success. Users can
seamlessly merge numerical blocks, and the core mechanics of the 2048 game are
faithfully reproduced. The logic ensures a challenging and entertaining gaming
experience.

Immersive Sound Effects:


The incorporation of sound effects, including background music and tile merging
sounds, adds a layer of immersion to the game. The auditory elements contribute to
the overall ambiance, enhancing the user experience and making gameplay more
engaging.

Score Recording and Best Score Tracking:


The system accurately records scores during gameplay, providing users with a real-
time reflection of their performance. Additionally, the best score is tracked and
updated when a new high score is achieved. This feature encourages players to strive
for improvement and beat their own records.

Restart Button Functionality:


The inclusion of a restart button allows users to reset the game, providing a
convenient way to start a new round without having to reload the entire application.
This feature contributes to a seamless and user-friendly gaming experience.

Game Over Appearance:


The game over appearance is triggered when there are no more possible moves,
signaling the end of the game. This visual cue informs players of the conclusion of
their current session and prompts them to either restart or take other actions.
These achievements collectively demonstrate the successful development of a fully
functional and enjoyable 2048 game. The combination of accurate game logic,
immersive sound effects, score recording, a restart button, and a clear game over
appearance contributes to a satisfying user experience.

9. Lessons Learned

Time Management Impact:


The project emphasized the critical role of time management in the development
process. Inadequate time control not only led to challenges in meeting project
deadlines but also had a ripple effect on other commitments, especially after school
hours. This underscores the importance of setting realistic timelines and priorities to
ensure a balanced workload.

Implementation Efficiency:
Identifying areas for improving implementation efficiency became a key takeaway.
This involved a closer examination of algorithms, code structure, and overall system
performance. The need for continuous optimization was underscored, prompting a
commitment to ongoing learning and exploration of best practices in coding and
software development.

10. Future Work

Background Music with User Choice:


Enhance the gaming experience by allowing users to choose their preferred
background music from a selection of tracks. This customization can add a personal
touch to the gameplay and cater to diverse user preferences.

Dynamic Grid Sizes:


Implement multiple grid size options, such as 4x4, 5x5, and 6x6. This expansion
offers players varying levels of difficulty and strategy, accommodating both beginners
and seasoned players. Each grid size could present unique challenges, providing a
more versatile gaming experience.

Time Record Feature:


Introduce a time recording feature to track the duration of each gaming session. This
could be displayed as a record or timer, allowing players to challenge themselves to
achieve faster completion times. Additionally, the time record feature adds an element
of competition for users striving to improve their gameplay efficiency.

Achievement System:
Implement an achievement system to recognize and reward players for specific
accomplishments during the game. Achievements could include reaching high scores,
completing games within certain time limits, or successfully merging tiles to create
rare combinations. This system encourages players to explore different aspects of the
game and adds an element of progression.

User Profiles and Statistics:


Create user profiles that store individual gaming statistics and achievements. This
feature allows players to track their progress over time, compare performance with
friends, and set personal goals. User profiles contribute to a more personalized and
engaging gaming experience.

Interactive Tutorials:
Develop interactive tutorials or hints to guide new players through the game
mechanics and strategies. This ensures a smoother onboarding process and helps users
grasp the rules and objectives quickly, enhancing the accessibility of the game.

11. Conclusion
Our objective was to replicate the enjoyable experience of the 2048 game, providing
users with the satisfaction of merging numerical blocks. However, this project has
also served as a valuable learning experience. We understand the significance of
effective time management and recognize areas for improving implementation
efficiency. Looking ahead, the addition of background music is a potential feature to
enhance the gaming ambiance. Despite challenges, we hope that users will find joy in
playing our 2048 replica, and we encourage everyone to have fun with the game.

12. References
Game 2048 in 5 mins - JavaScript - 5minsofcode.com
2048 GAME IN C# WITH SOURCE CODE

You might also like