Skip to content

Commit

Permalink
works even better
Browse files Browse the repository at this point in the history
  • Loading branch information
garbados committed Jan 25, 2015
1 parent 1c27101 commit 590abe8
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 47 deletions.
8 changes: 6 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env node

var civ = require('..');
var yargs = require('yargs')
.alias('r', 'report');
var argv = yargs.argv;

civ
.game([
Expand All @@ -9,7 +12,8 @@ civ
civ.ai.basic.discover,
civ.ai.basic.expand,
civ.ai.basic.exchange,
civ.ai.basic.develop
civ.ai.basic.develop,
civ.ai.basic.consent,
])
.play()
.report();
.report(argv.report);
4 changes: 2 additions & 2 deletions lib/ai/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ function basic_ai_init (value) {
};
}

function basic_ai_choice (choices, world, done) {
function basic_ai_choice (choices, i, world, done) {
if (choices.indexOf(this.value) > -1)
done(null, this.value);
else
done(null, choices[0]);
done(null, choices[1] > choices[2] ? choices[1] : choices[2]);
}

function make_basic_ai (value) {
Expand Down
42 changes: 35 additions & 7 deletions lib/ai/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var readline = require('readline');

module.exports = new Player({
init: function () {
this._turn = 0;
this.make_rl = function (choices) {
return readline.createInterface({
input: process.stdin,
Expand All @@ -17,7 +18,6 @@ module.exports = new Player({
};

this.ask = function ask (rl, choices, done) {
rl.setPrompt(['Choose:', choices.join(', '), '\n'].join(' '));
rl.prompt();
rl.on('line', function (choice) {
if (choices.indexOf(choice) !== -1) {
Expand All @@ -29,16 +29,44 @@ module.exports = new Player({
});
};
},
turn: function (choices, world, done) {
turn: function (choices, i, world, done) {
this._turn++;
var society = world.societies[i];
if (society._dead)
return done(null, world);
var rl = this.make_rl(choices);
console.log(JSON.stringify(world, undefined, 2));
console.log('YOUR TURN');
var indent = ' ',
kndent = ' ';
console.log('TURN', this._turn);
console.log(indent, 'yield', world.yield);
console.log(indent, 'feels', JSON.stringify(world.feels));
world.societies.forEach(function (society, i) {
console.log(indent, 'society', i);
console.log(kndent, 'yield', society.nation.yield);
console.log(kndent, 'population', society.nation.population);
console.log(kndent, 'values', JSON.stringify(society.values));
});
console.log('CHOOSE', choices.join(' or '));
this.ask(rl, choices, done);
},
inflection: function (choices, world, done) {
inflection: function (choices, i, world, done) {
var society = world.societies[i];
if (society._dead)
return done(null, world);
this._turn++;
var rl = this.make_rl(choices);
console.log(JSON.stringify(world, undefined, 2));
console.log('INFLECTION TURN');
var indent = ' ',
kndent = ' ';
console.log('TURN', this._turn, '-- INFLECTION');
console.log(indent, 'yield', world.yield);
console.log(indent, 'feels', JSON.stringify(world.feels));
world.societies.forEach(function (society, i) {
console.log(indent, 'society', i);
console.log(kndent, 'yield', society.nation.yield);
console.log(kndent, 'population', society.nation.population);
console.log(kndent, 'values', JSON.stringify(society.values));
});
console.log('CHOOSE', choices.join(' or '));
this.ask(rl, choices, done);
}
});
6 changes: 1 addition & 5 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ function random_choices (n, society) {
}

var values = Object.keys(society.values);
var choices = [];
for (i = 0; i < n; i++) {
choices.push(values.pop());
values = shuffle(values);
}
var choices = shuffle(values).slice(0, n);
return choices;
}

Expand Down
34 changes: 14 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var worlds = require('./worlds');
var play = require('./play');
var async = require('async');
var common = require('./common');
var report = require('./report');
var q = require('q');

function AjveCiv () {
Expand Down Expand Up @@ -39,26 +40,19 @@ AjveCiv.prototype.play = function (opts) {
AjveCiv.prototype.report = function (opts) {
if (!this._playing) throw new Error("Must call `play()` before `report()`");

// TODO custom print options and stuff
this._playing = this._playing.then(function (games) {
var indent = ' ',
jndent = ' ',
kndent = ' ';
games.forEach(function (turns, i) {
console.log('game', i + 1);
turns.forEach(function (world, j) {
if (!((j === 0) || (j === (turns.length - 1)))) return;
console.log(indent, 'turn', j + 1);
console.log(jndent, 'global yield', world.yield);
console.log(jndent, 'global feels', JSON.stringify(world.feels));
world.societies.forEach(function (society, k) {
console.log(jndent, 'society', society.name || k + 1);
console.log(kndent, 'nation', JSON.stringify(society.nation));
console.log(kndent, 'values', JSON.stringify(society.values));
});
});
});
});
if (typeof opts === 'string') {
if (opts === 'json')
this._playing = this._playing.then(report.to_json());
else
// file path
this._playing = this._playing.then(report.to_file(opts));
}
else if (typeof opts === 'function')
// custom function
this._playing = this._playing.then(opts);
else if (opts === undefined)
this._playing = this._playing.then(report.prettyprint);

return this;
};

Expand Down
9 changes: 5 additions & 4 deletions lib/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function play_game (ruleset, teams, done) {
var tasks = players.map(function (player, i) {
return async.apply(get_choice.bind({
world: world,
society: societies[i]
society: societies[i],
i: i
}), n, player);
});
async.parallel(tasks, done);
Expand All @@ -42,7 +43,7 @@ function get_players (teams) {

function get_societies (teams) {
var players = get_players(teams);
return players.map(function (player) {
return players.map(function (player, i) {
return {
values: {
discover: 2,
Expand Down Expand Up @@ -73,9 +74,9 @@ function feels_matrix (players, done) {

function get_choice (turn, player, done) {
if (turn % 10)
player.turn(common.values.random(3, this.society), this.world, done);
player.turn.call(player, common.values.random(3, this.society), this.i, this.world, done);
else
player.inflection(common.values.highest(3, this.society), this.world, done);
player.inflection.call(player, common.values.highest(3, this.society), this.i, this.world, done);
}

function update_world (turn, choices, done) {
Expand Down
54 changes: 53 additions & 1 deletion lib/report.js
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
// TODO
function prettyprint (games) {
var indent = ' ',
jndent = ' ',
kndent = ' ';
games.forEach(function (turns, i) {
console.log('game', i + 1);
turns.forEach(function (world, j) {
if (!((j === 0) || (j === (turns.length - 1)))) return;
console.log(indent, 'turn', j + 1);
console.log(jndent, 'global yield', world.yield);
console.log(jndent, 'global feels', JSON.stringify(world.feels));
world.societies.forEach(function (society, k) {
console.log(jndent, 'society', society.name || k + 1);
console.log(kndent, 'nation', JSON.stringify(society.nation));
console.log(kndent, 'values', JSON.stringify(society.values));
});
});
// BUT WHO WON?
var last_turn = turns.slice(-1)[0];
console.log(indent, 'final scores');
var scores = last_turn.societies.map(function (society) {
return society.nation.yield + society.nation.population;
});
var high_score = Math.max.apply(Math, scores);
scores.forEach(function (score, i) {
var WINNER = (score === high_score) && 'WINNER';
var DEAD = (last_turn.societies[i]._dead) && 'DEAD';
console.log(jndent, i + 1, score, DEAD || WINNER || '');
});
var total = scores.reduce(function (a, b) {
return a + b;
}, 0);
console.log(indent, 'total score', total);
});
}

function to_file (path) {
return function (games) {
fs.writeFileSync(path, JSON.stringify(games));
};
}

function to_json () {
return function (games) {
console.log(JSON.stringify(games));
};
}

module.exports = {
prettyprint: prettyprint,
to_file: to_file,
to_json: to_json
};
14 changes: 8 additions & 6 deletions test/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ describe('ai', function () {
var players = choices.map(function (value) { return ai.basic[value]; });
var world = {};

async.map(players, function (player, done) {
player.turn(choices, world, done);
}, function (err, players_choices) {
var tasks = players.map(function (player, i) {
return player.turn.bind(player, choices, i, world);
});
async.parallel(tasks, function (err, players_choices) {
chai.expect(players_choices).to.deep.equal(choices);
done();
});
Expand All @@ -35,9 +36,10 @@ describe('ai', function () {
var players = choices.map(function (value) { return ai.basic[value]; });
var world = {};

async.map(players, function (player, done) {
player.inflection(choices, world, done);
}, function (err, players_choices) {
var tasks = players.map(function (player, i) {
return player.inflection.bind(player, choices, i, world);
});
async.parallel(tasks, function (err, players_choices) {
chai.expect(players_choices).to.deep.equal(choices);
done();
});
Expand Down

0 comments on commit 590abe8

Please sign in to comment.