Today I had a day off work, and what better way to spend it than by programming an Excel spreadsheet 'port' of the web game 2048? So that is exactly what I did!
This is a beta version of the game: I'm not entirely convinced that I've got every aspect of the gameplay quite right (my random weightings for the spawning of 2s and 4s may be a little out), and there's always the small possibility of the odd formula error somewhere along the line causing tiles to misbehave. Anything like that can be easily ironed out. The point is that it works!
[Edit - I spotted one problem that needed fixing: new tiles shouldn't spawn when all adjacents in a direction are doubled and shunted. Tut. I've added a new line of sums to monitor and block spawning in this situation, and things are working a lot better as a consequence.]
[Edit - I spotted one problem that needed fixing: new tiles shouldn't spawn when all adjacents in a direction are doubled and shunted. Tut. I've added a new line of sums to monitor and block spawning in this situation, and things are working a lot better as a consequence.]
The game was designed in Excel 2007, and requires macros to be enabled. Right click here to download yourself a copy at a tidy 32K. If your browser tries to save the file as a .zip, be sure to correct it and to save it as a.xlsm file.
The gubbins that works the game can be found to the right of the sheet, explored and amended to suit your tastes. Basically, the five buttons (four cursors and a "New Game" reset) copy the current grid to a space below it (or in the case of the reset, copy a blank grid), and then the gubbins does the rest.
Each move follows a three-stage process: shunt the tiles together to skip the blanks; double up adjacent duplicates; shunt again. Calculations are made for all four directions. This was the trickiest bit to work out, and I'm thankful that there are only four rows to work with at a time. There was a fair bit of trial and error beyond the initial idea of what should happen.
The random tile was another tough thing to implement. It is calculated by totting up the number of empty spaces, then allocating each one a number (using =COUNTIF), and picking one of the spaces at random using the =RANDBETWEEN function. I'm quite proud of this, though like I say, I'm not sure I've got the weighting right as I'm clocking in some high scores.
Scoring is achieved by totting up the difference between the shunted grid and the doubled grid, and applying a square/root to knock off any negatives.
Anyway, feel free to have a trawl of the cells and let me know if you find any glaring errors. If you'd like me to make a step-by-step work-through I'll give it a go.
