Skip to content

Instantly share code, notes, and snippets.

@kyleburton
Created June 30, 2012 13:04
Show Gist options
  • Save kyleburton/3023669 to your computer and use it in GitHub Desktop.
Save kyleburton/3023669 to your computer and use it in GitHub Desktop.
play a game
// http://www.hackerrank.com/
// open JS console in Chrome, paste into JS console
// run:
// Foo.playGames( 7, 99 )
// to play 99 games starting with the opening value of 7
Foo = (function () {
var self = {autoContinue: true};
self.startGame = function (numCandies) {
$.ajax({
type: 'POST',
url: '/splash/challenge.json',
data: {
n: numCandies,
remote: true,
utf8: '✓'
},
success: self.playRound
});
};
self.nextMove = function (current) {
return current % 6;
};
self.playRound = function (data) {
var nextMove;
console.log('play round: ' + JSON.stringify(data));
if (data.message) {
console.log(data.message);
}
//if (data.exit) {
// console.log('Game is over!');
// return;
//}
if (data.game) {
data = data.game;
}
if (data.solved) {
console.log('Game is solved!');
self.continue = null;
return self.gameWon();
}
// n the starting numCandies
// current the number of candies left
// limit: not sure wha thtis is
// moves array of moves taken?
nextMove = self.nextMove(data.current);
console.log('the next move is: ' + nextMove);
self.continue = function () {
$.ajax({
type: 'PUT',
url: '/splash/challenge.json',
data: {
move: nextMove,
remote: true,
utf8: '✓'
},
success: self.playRound
});
};
if (self.autoContinue) {
self.continue();
}
};
self.playGames = function ( startingValue, numGames ) {
var currentValue = startingValue - (startingValue % 6) + 1,
numGames = numGames;
self.gameWon = function () {
if (numGames < 1) {
console.log('games played, currentValue: ' + currentValue);
return;
}
numGames = numGames - 1;
currentValue += 6;
console.log('playing next game[' + numGames + ']: ' + currentValue);
return self.startGame(currentValue);
};
numGames = numGames - 1;
console.log('playing next game[' + numGames + ']: ' + currentValue);
return self.startGame(currentValue);
};
return self;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment