Skip to content

Instantly share code, notes, and snippets.

@alcalyn
Last active August 29, 2015 14:09
Show Gist options
  • Save alcalyn/87c785e53fb4b501c763 to your computer and use it in GitHub Desktop.
Save alcalyn/87c785e53fb4b501c763 to your computer and use it in GitHub Desktop.
Simple and decoupled more or less game in Javascript
/**
* Run + or - game.
*
* To play it now, just call: runGame();
*
* @param {Object} interface which you want to use. Default is Prompt
* @param {Number} max number. Secret number will be between 0 and max. Default is 100
* @param {boolean} decimal whether you allow decimal number. Default is false
* @param {Number} secret number. Force the secret number. Default is random.
*/
function runGame(interface, max, decimal, secret) {
if (!interface) {
interface = Prompt;
}
if (!max) {
max = 100;
}
if (!secret) {
secret = Math.random() * max;
}
if (!decimal) {
secret = Math.floor(secret);
}
interface.init(max, decimal);
var user = max;
var tries = 0;
while (user != secret) {
tries++;
var clue = (user < secret) ? '+' : '-';
user = interface.ask(user, clue);
};
interface.found(user, tries);
}
/**
* Documentation interface. Do nothing.
*/
var Interface =
{
/**
* Called before running the game.
*
* @param {Number} max number the game allows. The secret number is between 0 and max.
* @param {boolean} decimal whether game allows decimal number.
*/
init: function (max, decimal)
{
},
/**
* The game ask a number.
*
* @param {Number} user the last number you said
* @param {string} clue '+' or '-' if the secret is more or less than the last number you said
* @returns {Number} the new number you say
*/
ask: function (user, clue)
{
return 0;
},
/**
* Called when you found the secret number
*
* @param {Number} number the secret number you just found
* @param {integer} tries number of tries used to find the number
*/
found: function (number, tries)
{
}
};
/**
* Prompt interface. Use default browser prompt and alert to play the game.
*/
var Prompt =
{
init: function (max, decimal)
{
alert('You have to find '+(decimal ? 'a decimal number' : 'an integer')+' between zero and ' + max);
},
ask: function (user, clue)
{
return prompt(clue + ' than ' + user);
},
found: function (number, tries)
{
alert('Congratulations ! Number ' + number + ' found in ' + tries + ' tries.');
}
};
/**
* Bot interface. Will find the number automatically by using the middle number.
*/
var Bot =
{
mem_min: 0,
mem_max: 0,
decimal: false,
init: function (max, decimal)
{
console.log('I will find the secret '+(decimal ? 'decimal' : 'integer')+' between zero and '+max);
Bot.mem_min = 0;
Bot.mem_max = max;
Bot.decimal = decimal;
},
ask: function (user, clue)
{
if (clue == '+') {
Bot.mem_min = user;
}
if (clue == '-') {
Bot.mem_max = user;
}
var botAnswer = (Bot.mem_min + Bot.mem_max) / 2;
if (!Bot.decimal) {
botAnswer = Math.floor(botAnswer);
}
console.log(clue + ' than '+user+', trying '+botAnswer);
return botAnswer;
},
found: function (number, tries)
{
console.log('found '+number+' in '+tries+' times');
}
};
/**
* Risky bot, will try to find the number faster than Bot.
*/
var RiskyBot =
{
mem_min: 0,
mem_max: 0,
decimal: false,
init: function (max, decimal)
{
console.log('Yeaaaaah I will find the secret '+(decimal ? 'decimal' : 'integer')+' between zero and '+max);
RiskyBot.mem_min = 0;
RiskyBot.mem_max = max;
RiskyBot.decimal = decimal;
},
ask: function (user, clue)
{
if (clue == '+') {
RiskyBot.mem_min = user;
}
if (clue == '-') {
RiskyBot.mem_max = user;
}
var botAnswer = (RiskyBot.mem_min + RiskyBot.mem_max) / 2;
var riskValue = Math.floor((RiskyBot.mem_max - RiskyBot.mem_min) / 5);
if (clue == '+') {
botAnswer += riskValue;
}
if (clue == '-') {
botAnswer -= riskValue;
}
if (!RiskyBot.decimal) {
botAnswer = Math.floor(botAnswer);
}
console.log(clue + ' than '+user+', trying '+botAnswer);
return botAnswer;
},
found: function (number, tries)
{
console.log('found '+number+' in '+tries+' times');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment