Course:CPSC312-2024W2/2048

From UBC Wiki

What is the problem?

We will be implementing the 2048 game in Haskell. We took inspiration from the game 2048 and a project from the previous term (https://wiki.ubc.ca/Course:CPSC312-2021/2048). The game involves a 4x4 grid, with tiles that have numbers that are all powers of two. The player can combine two tiles with the same number. The player can choose to move the blocks up, down, left, or right and tiles will combine in that direction. After each move, a new tile will spawn in a random row. A player wins the game by getting the 2048 tile and loses if the grid is filled with tiles and no moves are available.

In 2048, it is important to be able to see the current game board in order to determine your next move. Our goal with this project is to investigate implementing games in Haskell that rely heavily on the visual aspects in order to play.

Something extra:

We have implemented a new game mode based on the basic game where an AI will randomly make a move for the player for every forth round. The move is unpredictable, and could mess up the player's current game board. However, the player has the option to save the game board and load back to a previous game board if needed. This uses Haskell's IORef to temporarily store the board and score.

What did we learn from doing this?

This project was good practice in applying some of the concepts we learned in lecture to our own problems. Our project allowed us to better understand working with let...in, where, creating and using new data types, more complex list functions applications as well as more conceptual problem solving like being able to effectively break big problem into small pieces. We also learned about using Haskell IO functions like IORef.

Specifically in terms of creating games, functional programming is definitely well suited for this task. Haskell's wide variety of well documented libraries allows for more complex games then we were able to implement in this time. It seems very feasible to make a wide variety of games in Haskell, as seen in lecture and through our own research making this project. Implementing games with proper graphics seems much more difficult, however. We originally had planned to create a GUI where we would display the game board, but the Haskell libraries it would require to do so were very complex. Implementing a full game with graphics in Haskell is possible, but it does not seem like Haskell is as well suited for this. Taking this into account, we decided it would be better to display the game board in the command line where Haskell is much easier to work with. This ended up working well, and it is clear and easy to see the current state of the board.

Work division

Dusty: Set up base of the game (starting types and functions), lose function, functions to update the game board including moving up and down, updating the score, worked with Michael on the addTile function and the play.hs file setup for the main game.

Michael: move left, right function, menu function, addTile function, play function, go function, main function, play function (Dusty helped me optimize many of those functions)

Joseph: Implemented the display function, nightmare mode and the save and load for the nightmare mode


https://github.students.cs.ubc.ca/dustyf/2048