Unity Command Pattern 2048 with Obstacle
This is an implementation of the Command Pattern on 2048 game using Unity. I first replicated the original 2048 game on Unity, then utilized the Command Pattern for the undo and redo features. Additionally, I added an Obstacle mode that generates a moving obstacle at random places.
Specs | Date | Role | Team Composition |
---|---|---|---|
Unity PC Command Pattern | 04/2024 | Game Programmer | Programmer * 1 |
Details
I have tried my best to replicate the original game including on the sprites, colors, and animations. The game turned out to feel very similar to the original game.
The Command Pattern:
Assets\Scripts\ICommand.cs:
This is the base class of command.
Assets\Scripts\MoveCommand.cs:
I only made one MoveCommand as all the four directions can be taken in as parameters.
It also needs a reference to the board it operates on. (There is only one board, but this is a practice to avoid using the Singleton Pattern)
For undoing, it stores the state of the game before performing the movement. It stores the current score and the numbers on each cell (0 if there is no tile on that cell) as a two-dimensional integer array.
Assets\Scripts\CommandManager.cs:
The CommandManager has two constructors. The difference is just one of them can set the capacity of commands for undoing.
It has two linked lists for storing commands. Whenever the player makes a command, it will be stored to the commands linked list. If the commands linked list has reached its capacity, the oldest command in the linked list will be removed. As for the redoCommands, it will only be filled while undoing. Whenever the player undoes, the undid command will be removed from the commands linked list and be added into the redoCommands linked list. And it will be cleared whenever the player makes a new command.
Assets\Scripts\CommandManager.cs: