Skip to content

Commit

Permalink
Improve callback, serialization, and stopping rule
Browse files Browse the repository at this point in the history
  • Loading branch information
suchow committed Sep 30, 2018
1 parent 6631570 commit 24aeb15
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()) })
```

Expand All @@ -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:
Expand Down
19 changes: 14 additions & 5 deletions acsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function ACSG (opts) {
players = []
scheduledHumanMoves = []
self.events = []
gameOver = false

var data = []
var background = []
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
})
}
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions demo.js
Original file line number Diff line number Diff line change
@@ -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()) })

0 comments on commit 24aeb15

Please sign in to comment.