An implementation of the 8-puzzle game and an AI for solving it.
Demo: link
- Node.JS
- Yarn 1.x
- Clone the repo
- Open a terminal in the cloned folder and run
yarn
- Start the application by running
yarn dev
. - The application can now be accessed on
http://localhost:1234
Each AI solver must implement the AI
interface (src/ai/AI.ts
).
To test the AI, add it to the object in src/sketch.ts
, with an appropiate name.
export const solvers: { [key: string]: () => AI } = {
'a*': () => new AStarAI(),
'yourOwnAI': => () => new YourOwnAI(),
};
Afterward, select the AI which should be used for solving in the constructor of the EightPuzzle
...
p.setup = () => {
game = new EightPuzzle(p, 3, 'yourOwnAI');
...
It is also possible to change the size of the board if you feel like a 3x3 grid is too easy.
It is expected that all moves are computed up-front and returned as a list of moves.
Since this can be rather slow, the game allows the AI to give progress reports while moves are being generated.
This can be done by sending the ai_progress
message to the main thread, of type Status
as seen in EightPuzzle
:
Note this only works when the AI runs in a web worker thread, which is why it is advised to use the isRunningAsWorker
utility function as a condition to send the update.
if(isRunningAsWorker()) {
self.postMessage({
cmd: 'ai_progress',
percent, // The current progress in percent between 0-100 (required)
memory, // The current memory usage, in bytes (optional)
status, // The current status message (optional)
} as Status);
}