@derhuerst/2048

1.0.1 • Public • Published

2048

This is a fork of Gabriele Cirulli's 2048 project with some changes around deterministic play and unlimited undos.

Live here: https://andrewmorris.io/2048.

The Challenge

As you start playing, you'll notice that your moves are displayed in the url hash. This makes your game state and its history shareable (usually). Your mission is to produce a url with the highest score you can find.

Headless Version

Since you are a developer, you will likely desire to separate your algorithmic number crunching domination from the fancy schmancy graphical frontend I have provided. Fear not my friend, simply add "voltrevo-2048": "voltrevo/2048" to your package.json and access the Board factory via:

'use strict';

const Board = require('voltrevo-2048/src/Board.js');

const b = Board();

b.up();
b.down();
b.left();
b.right();

console.log(b.prettyString()); /*
  +------------+
  | 0  0  0  4 |
  | 0  0  2  0 |
  | 0  0  0  0 |
  | 0  0  4  2 |
  +------------+
*/

You can also experiment with this in the developer console as window.Board. Boards also provide .getCells() and .clone(), and you can specify the game seed and starting state like this:

Board({
  gameSeed: 'foobar',
  cells: [
    [0, 0, 0, 4],
    [0, 0, 2, 0],
    [0, 0, 0, 0],
    [0, 0, 4, 2],
  ],
})

The Suggestion Engine

Speaking of the headless version, you may have noticed that the game displays move suggestions for you. These suggestions are generated by trivialGetSuggestion.js which looks like this:

module.exports = (board) => {
  const movePriority = ['right', 'down', 'up', 'left'];

  for (let i = 0; i !== movePriority.length; i++) {
    const move = movePriority[i];

    if (board[move]()) {
      return move;
    }
  }

  // Nowhere to go, but still need to return something
  return 'right';
};

These suggestions are very weak, as they simply attempt right, down, up, left, in that order. If you create a headless AI, you might want to fork this project and incorporate it here to interact with and see what your AI moves look like. You might even use this to team up with your AI to produce a better outcome together.

This is also accessible to you in the console of the official version by overwriting window.gameManager.getSuggestion.

URL Hash Format

If your move sequence becomes very long, it may become abbreviated using a fake url that is not shareable. The format looks like this:

https://andrewmorris.io/2048/#<game-seed>;<move-sequence-url>;<undo-count-after-url>;<raw-moves>

So if your move-sequence-url is something like 2048://wwxzcbyyzo, that url will only work inside the page that generated it. To fix this, you can print your raw moves in the developer console with this command:

gameManager.moveStore.resolve().then(moves => console.log(moves));

Then, I suggest creating a url for those moves using a github gist (use the raw button), and then shortening it using goo.gl, and then you can create a shareable url like this:

https://andrewmorris.io/2048/#<game-seed>,<url-you-created>

Note: <game-seed> might be an empty string. That's ok.

Run Your Own Instance

git clone git@github.com:voltrevo/2048
cd 2048
npm install
npm start
# see localhost:8080

Note: You will need git and node.

License

2048 is licensed under the MIT license.

Package Sidebar

Install

npm i @derhuerst/2048

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • derhuerst