5.2: Program State for Game Modes
Last updated
var mode = 'green';
var main = function (input) {
if (input == 'greenmode') {
mode = 'green';
} else if (input == 'bluemode') {
mode = 'blue';
}
var myOutputValue =
'A fool sees not the same tree that a wise man sees. -William Blake';
if (mode == 'blue') {
myOutputValue =
'The sea, once it casts its spell, holds one in its net of wonder forever. -Jacques Cousteau';
}
return myOutputValue;
};var currentGameMode = 'waiting for user name';
var bankRoll = 10;
var userName = '';
var rollDice = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger + 1;
return diceNumber;
};
var main = function (input) {
var myOutputValue = '';
if (currentGameMode == 'waiting for user name') {
// if the game mode is user name... set the name as the input
userName = input;
// now that we have the name, switch the mode
currentGameMode = 'dice game';
myOutputValue = 'Hello ' + userName;
} else if (currentGameMode == 'dice game') {
// if the game mode is dice game... define userGuess as the input
var userGuess = input;
// dice game logic
var randomDiceRoll = rollDice();
myOutputValue =
userName +
' you lost! you guessed: ' +
input +
'. you rolled: ' +
randomDiceRoll +
'. current bank roll: ' +
bankRoll;
if (userGuess == randomDiceRoll) {
bankRoll = bankRoll + 1;
myOutputValue =
userName +
' you won! you guessed: ' +
input +
'. you rolled: ' +
randomDiceRoll +
'. your current bank roll: ' +
bankRoll;
}
}
return myOutputValue;
};var currentGameMode = 'waiting for user name';
var bankRoll = 10;
var userName = '';
var rollDice = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger + 1;
return diceNumber;
};
var playDiceGame = function (userName, userGuess) {
var message = '';
// dice game logic
var randomDiceRoll = rollDice();
message =
userName +
' you lost! you guessed: ' +
userGuess +
'. you rolled: ' +
randomDiceRoll +
'. current bank roll: ' +
bankRoll;
if (userGuess == randomDiceRoll) {
bankRoll = bankRoll + 1;
message =
userName +
' you won! you guessed: ' +
userGuess +
'. you rolled: ' +
randomDiceRoll +
'. your current bank roll: ' +
bankRoll;
}
return message;
};
var main = function (input) {
var myOutputValue = '';
if (currentGameMode == 'waiting for user name') {
// set the name
userName = input;
// now that we have the name, switch the mode
currentGameMode = 'dice game';
myOutputValue = 'Hello ' + userName;
} else if (currentGameMode == 'dice game') {
myOutputValue = playDiceGame(userName, input);
}
return myOutputValue;
};