Last active
August 29, 2015 14:04
-
-
Save basicer/1d7f6f5e3c4ef2a826ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your goal is to 'buy' a path of tiles from bottom to top. | |
// One group of tiles is available for bidding each turn. | |
// Each turn, bid to win one tile from the tile group. | |
// 1. Pick a tile to bid for. | |
// You can only choose tiles that are | |
// a) in the tileGroup and | |
// b) don't already have an owner. | |
var tileGroup = this.tileGroups[tileGroupLetter]; | |
var tile = null; | |
for (var i = 0; i < tileGroup.length; ++i) { | |
if (!tileGroup[i].owner) { | |
tile = tileGroup[i]; // Found an unclaimed tile. | |
break; | |
} | |
} | |
// 2. Choose your bid price. | |
// You only pay and win the tile if your bid is the highest. | |
var myBid = Math.floor(1 + Math.random() * 7); | |
// 3. Respond with an object with properties 'gold' and 'desiredTile'. | |
return {gold: myBid, desiredTile: tile}; | |
// For complete rules, read the Guide. It's at the top bar. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function shuffle(o){ //v1.0 | |
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
return o; | |
} | |
function Box(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
Box.prototype = { | |
draw: function(cursor) { | |
if ( this.owner == undefined ) cursor.bg.grey(); | |
else if ( this.owner == "humans") cursor.bg.red(); | |
else cursor.bg.blue(); | |
var dy = 6-this.y; | |
cursor.goto(3+this.x*10, 2+dy * 5 + 0).write(" "); | |
cursor.goto(3+this.x*10, 2+dy * 5 + 1).write(" "); | |
cursor.goto(3+this.x*10, 2+dy * 5 + 2).write(" "); | |
cursor.goto(3+this.x*10, 2+dy * 5 + 3).write(" "); | |
cursor.goto(10+this.x*10, 2+dy * 5 + 0).cyan().write(this.tileGroup); | |
cursor.goto(6+this.x*10, 2+dy * 5 + 2).cyan().write(this.x + "," + this.y); | |
cursor.reset(); | |
} | |
}; | |
function Board() { | |
this.nx = 7; | |
this.ny = 7; | |
this.tiles = new Array(this.nx*this.ny); | |
this.humanGold = 128; | |
this.orcGold = 128; | |
this.humanTiles = []; | |
this.orcTiles = []; | |
this.tilesOwned = []; | |
var pool = []; | |
for ( var l = 0; l < this.tileGroupLetters.length; ++l ) { | |
for ( var i = 0; i < this.tileGroupSize; ++i ) pool.push(this.tileGroupLetters[l]); | |
} | |
shuffle(pool); | |
for ( var x = 0; x < this.nx; ++x ) { | |
for ( var y = 0; y < this.ny; ++y ) { | |
var b = new Box(x,y); | |
b.tileGroup = pool.pop(pool); | |
this.tiles[x+y*this.nx] = b; | |
} | |
} | |
} | |
Board.prototype = { | |
nx: 7, | |
ny: 7, | |
tileGroupSize: 7, | |
tileGroupLetters: ['A', 'B', 'C', 'D', 'E', 'F', 'G'], | |
draw: function(cursor) { | |
for ( var i = 1; i < 40; ++i ) cursor.goto(1,i).bg.black().eraseLine(); | |
for ( var bi in this.tiles ) this.tiles[bi].draw(cursor); | |
cursor.bg.black().goto(20,1).red().write("Humans: " + this.humanGold); | |
cursor.bg.black().goto(40,1).blue().write("Orcs : " + this.orcGold); | |
cursor.goto(1,40); | |
}, | |
getTileAt: function(x, y) { | |
return this.tiles[x + y * this.nx]; | |
}, | |
validate: function(who, tile, amt) { | |
if ( amt <= 0 ) return false; | |
if ( tile.owner ) return false; | |
//if ( !(tile in this.tiles) ) return false; | |
if ( who == "orcs" ) { | |
if ( amt > this.orcGold ) return false; | |
} else if ( who == "humans" ) { | |
if ( amt > this.humanGold ) return false; | |
} else { | |
return false; | |
} | |
return true; | |
}, | |
buy: function(who, tile, amt) { | |
if ( who == "humans" ) { | |
this.humanGold -= amt; | |
this.humanTiles.shift(tile); | |
} else { | |
this.orcGold -= amt; | |
this.orcTiles.shift(tile); | |
} | |
this.tilesOwned.shift(tile); | |
tile.owner = who; | |
}, | |
tileGroups: function() { | |
var obj = {}; | |
for ( var i in this.tiles ) { | |
var t= this.tiles[i]; | |
if ( !( t.tileGroup in obj ) ) obj[t.tileGroup] = []; | |
obj[t.tileGroup].push(t); | |
} | |
return obj; | |
} | |
}; | |
exports.Board = Board; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ansi = require("ansi"); | |
var cursor = ansi(process.stdout); | |
var Board = require("./board.js").Board; | |
var fs = require("fs"); | |
var usleep = require("sleep").usleep; | |
var ai_humans = new Function('tileGroupLetter', fs.readFileSync("ai_default.js")); | |
var ai_orcs = new Function('tileGroupLetter', fs.readFileSync("ai_default.js")); | |
var humans = {team: "humans"}; | |
var orcs = {team: "orcs"}; | |
var humans_u = Object.create(humans); | |
var orcs_u = Object.create(orcs); | |
var board = new Board(); | |
function prime(o) { | |
o.tileGroups = board.tileGroups(); | |
if ( o.team == "human" ) { | |
o.myTiles = board.humanTiles; | |
o.opponentTiles = board.orcTiles; | |
o.gold = board.humanGold; | |
} else { | |
o.myTiles = board.orcTiles; | |
o.opponentTiles = board.humanTiles; | |
o.gold = board.orcGold; | |
} | |
o.tilesOwned = board.tilesOwned; | |
} | |
for ( var i = 0; i < 7; ++i ) { | |
for ( var li in board.tileGroupLetters ) { | |
board.draw(cursor); | |
usleep(500); | |
var letter = board.tileGroupLetters[li]; | |
prime(humans); | |
prime(orcs); | |
var h_move = ai_humans.call(humans_u, letter); | |
var o_move = ai_orcs.call(orcs_u, letter); | |
console.dir(h_move); | |
console.dir(o_move); | |
if ( !board.validate("humans", h_move.desiredTile, h_move.gold) ) h_move = null; | |
if ( !board.validate("orcs", o_move.desiredTile, o_move.gold) ) o_move = null; | |
if ( h_move == null && o_move == null ) break;; | |
if ( h_move == null ) board.buy("orcs", o_move.desiredTile, o_move.gold); | |
else if ( o_move == null ) board.buy("humans", h_move.desiredTile, h_move.gold); | |
else { | |
var humans_win = h_move.gold > o_move.gold; | |
if ( humans_win ) { | |
board.buy("humans", h_move.desiredTile, h_move.gold); | |
} else { | |
board.buy("orcs", o_move.desiredTile, o_move.gold); | |
} | |
} | |
} | |
} | |
board.draw(cursor); | |
cursor.goto(1,42).eraseLine(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "cc", | |
"version": "0.0.0", | |
"description": "ERROR: No README.md file found!", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": "", | |
"author": "", | |
"license": "BSD", | |
"dependencies": { | |
"ansi": "~0.3.0", | |
"ncurses": "~0.4.1", | |
"sleep": "~1.1.8" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment