I'm thinking about an assignment based around the game 2048. This would be towards the end of a first programming course and assumes the students have been introduced to at least two dimensional arrays, loops, conditionals and methods. The purpose is to have the students practice these skills.
I would represent the board
as a two dimensional array of int
.
I would require a method (e.g. merge(int[] row)
) which does the work on an individual row of the puzzle when the move is to slide left. The idea is to encapsulate the work of a slide in a single, testable method.
What are your opinions on the following ideas:
Should a single move in the game be implemented as (
move(direction)
) or as individual methods (up() down() left() right()
)?One idea for completing the game involves rotating the board by multiples of 90 degrees. Then a move can be implemented as a three step process:
- Rotate board to one orientation
- Slide all rows left (
merge(board[row])
for eachrow
- Rotate board back to original orientation
Another idea is to show them that move right can be implemented by the process
- reverse a row
- slide it left (
merge(board[row]
) - reverse the row again
- For columns, copy the values from a column into a one dimensional temp array, then perform the
merge(temp)
and then copy back to the board. This may also require reverses, depending on the up/down selection.
Require a method to produce a list of empty cells for use in picking the location of the next "insertion". This could be a list of
int
to demonstrate how a single index can map to a row/col usingdiv
andmod
operations. Alternatively, it could be a list ofpairs
of row/column.The last wrinkle is to decide what to actually stored in each board location. The simple answer is just the value. However, an interesting variation might be to store the
log2
of the value. By doing this, you get an index that can be used to select, for example, the background color of each square of the board. This changes the logic ofmerge()
, so that combining adjacent cells ups the value by 1, rather than adding two values together.
Am I trying to put too many different concepts into this assignment? Your opinions and suggestions are appreciated.
Thanks