From 24aeb15f1ca91d4f079277ec829f24daeb2a8814 Mon Sep 17 00:00:00 2001 From: Jordan Suchow Date: Sun, 30 Sep 2018 12:36:32 -0400 Subject: [PATCH] Improve callback, serialization, and stopping rule --- README.md | 7 ++----- acsg.js | 19 ++++++++++++++----- demo.js | 12 ++++++------ 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 0f55c2b..b1a2dcb 100755 --- a/README.md +++ b/README.md @@ -23,10 +23,9 @@ game = ACSG({ BLOCK_SIZE: 12, BLOCK_PADDING: 1 SEED: '19145822646', -}, -function () { console.log('Finished.') }) +}) -game.run() +game.run(function () { console.log(game.serialize()) }) ``` @@ -48,8 +47,6 @@ To update bundle.js: $ yarn run bundle -Note: Updates to the bundle should be committed to version control. - ## Managing dependencies To add a new dependency, use the `yarn add` command: diff --git a/acsg.js b/acsg.js index 770ad67..471e5d3 100755 --- a/acsg.js +++ b/acsg.js @@ -52,6 +52,7 @@ function ACSG (opts) { players = [] scheduledHumanMoves = [] self.events = [] + gameOver = false var data = [] var background = [] @@ -72,6 +73,13 @@ function ACSG (opts) { formatted: true }) + this.serialize = function () { + return JSON.stringify({ + 'events': this.events, + 'config': opts + }) + } + function randomPosition () { empty = false while (!empty) { @@ -226,7 +234,7 @@ function ACSG (opts) { document.getElementById('score').innerHTML = players[0].score } - this.run = function () { + this.run = function (callback) { start = Date.now() // Pregenerate bot motion events, sans direction. @@ -308,10 +316,11 @@ function ACSG (opts) { } } - if (lastIdx <= whichBotMoves.length) { + if (lastIdx < whichBotMoves.length - 1) { pixels.update(data) - } else { - return 'd' + } else if (!gameOver) { + gameOver = true + callback() } }) } @@ -324,7 +333,7 @@ function ACSG (opts) { lock = false directions.forEach(function (direction) { Mousetrap.bind(direction, function () { - if (!lock) { + if (!lock && !gameOver) { scheduledHumanMoves.push(direction) } lock = true diff --git a/demo.js b/demo.js index f8a2765..5a3ae2b 100644 --- a/demo.js +++ b/demo.js @@ -1,18 +1,18 @@ var ACSG = require('./acsg') game = ACSG({ + NUM_PLAYERS: 9, + DURATION: 6, INCLUDE_HUMAN: true, - NUM_PLAYERS: 3, - DURATION: 60, + BOT_STRATEGY: 'random', ROWS: 25, COLUMNS: 25, NUM_FOOD: 8, - VISIBILITY: 500, + VISIBILITY: 50, BOT_MOTION_RATE: 4, BLOCK_SIZE: 12, BLOCK_PADDING: 1, SEED: '19145822646' -}, -function () { console.log('Finished.') }) +}) -game.run() +game.run(function () { console.log(game.serialize()) })