Skip to content

Instantly share code, notes, and snippets.

@nelix
Created October 26, 2012 11:35
Show Gist options
  • Select an option

  • Save nelix/3958305 to your computer and use it in GitHub Desktop.

Select an option

Save nelix/3958305 to your computer and use it in GitHub Desktop.
Some idea I am playing with
// board.js
var roll, win_game, say, score, board, move, yeild;
var players = [{position:0, name: 'Jeff', points:0}, {position:0, name: 'Nathan', points: 0}];
//var players = [{position:0, name: 'bill'}];
var win = false;
// functions here take an argument and return how many places to move
roll = function() {
var dice = Math.floor((Math.random() * 6) + 1);
return dice;
};
score = function(n) {
this.points++;
console.log(this.name, 'scores', n, 'points, he is now on', this.points);
return 1;
};
win_game = function(what) {
win = what;
return 1;
};
say = function(what) {
console.log(this.name + '!', what);
return 1;
};
move = function(n) {
console.log(this.name, 'moves ', n, 'places!');
return n;
};
// todo add propper checking of returns to make sure they are fucking ints.
yeild = function(what) {
var n = roll();
//var n = parseInt(window.prompt(this.name + '! ' + what,roll()), 10);
console.log(this.name, 'rolls', n);
return n;
};
board = [
{action:say, what:'My spoon is too big!'},
{action:move, what:1},
{action:say, what:'Now an annoying javascript alert grants you a wish!'},
{action:yeild, what:'Take a leap of faith'},
{action:say, what:'I need some kinda content....'},
{action:move, what:1},
{action:yeild, what:'Roll 2 or more to jump the witch :('},
{action:move, what:1},
{action:say, what:'A witch sends you back to the start'},
{action:move, what:-4},
{action:move, what:1},
{action:score, what:1},
{action:yeild, what:'Roll your path'},
{action:score, what:10},
{action:move, what:1},
{action:score, what:-4},
{action:move, what:1},
{action:say, what:'you are about to win!'}
];
function exec(board, player) {
var position = player.position;
return board[position].action.call(player, board[position].what);
}
function isWin(player, board) {
if (player.position >= board.length) {
win = true;
$('#wins').append(player.name + ' with ' + player.points + ' points<br />');
console.log(player.name, ' wins with ' + player.points);
return true;
}
}
// new Function( 'return 0;' )();
var surface;
function updateTile(tile) {
tile.node.text((player ? '*' : ''));
}
function updateBoard(board) {
board.forEach(updateTile);
}
function drawTile(tile) {
var tileNode = $('<div class="tile"></div>').appendTo(surface);
tile.node = tileNode;
console.log(tileNode);
surface.append(tileNode);
}
function drawBoard(board) {
console.log('drawBoard', board);
board.forEach(drawTile);
}
function execPlayers(player) {
player.position += exec(board, player);
return isWin(player, board);
}
function resetPlayers(player) {
player.position = 0;
}
function game() {
win = false;
players.forEach(resetPlayers);
while(! win) {
players.forEach(execPlayers);
}
}
//console.log(board);
$(document).ready( function() {
surface = $('#board');
drawBoard(board);
var i = 0;
while (i <= 1) {
i++;
game();
}
});
@nelix

nelix commented Oct 27, 2012

Copy link
Copy Markdown
Author

Added players.
Hunting some odd bugs, how hard debugging is tells me refactoring is needed...

@nelix

nelix commented Oct 27, 2012

Copy link
Copy Markdown
Author

Fixed silly lack of parseInt making huge numbers with +=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment