dond
Version:
A library for simulating the hit UK/US game show.
127 lines (111 loc) • 3.84 kB
JavaScript
;
let readline = require('readline');
let rl = readline.createInterface(process.stdin, process.stdout);
let DondGame = require('./index.js');
let usDefaults = require('./settings/deal-us-standard');
let prefix = '> ';
let game = new DondGame(usDefaults.briefcases, usDefaults.casesToPick);
if (!game.isGameSane) {
console.log('Invalid settings passed in. Please fix.');
process.exit(-2);
}
let currentState = 'intro';
function whenLineEntered(line) {
line = line.trim();
function processIntroState() {
if (line === '') {
console.log('Who are you again? You have to have a name.');
}
else {
game.startGame(line);
console.log(`Welcome ${line}. Now, pick your case.`);
currentState = 'startGame';
}
}
function processStartGameState() {
let wantedNumber = Number.parseInt(line, 10);
let pickedCase = game.pickStartingCase(wantedNumber);
if (Number.isNaN(pickedCase)) {
console.log('Invalid case number chosen. Please select a valid case number.');
}
else {
console.log(`You now own case ${pickedCase}.`);
console.log(`Available cases: ${game.getAvailableCases()}`);
console.log(`Values in play: ${game.getValuesInPlay()}`);
console.log(`Please pick ${game.casesToPick} cases, one at a time.`);
currentState = 'eliminatingCases';
}
}
function processEliminatingCasesState() {
let wantedNumber = Number.parseInt(line, 10);
let eliminatedValue = game.pickCase(wantedNumber);
if (!eliminatedValue) {
console.log('Invalid case number chosen. Please select a valid case number.');
console.log(`Available cases: ${game.getAvailableCases()}`);
}
else {
console.log(`Case ${wantedNumber} contained ${eliminatedValue}.`);
let casesLeftToOpen = game.casesToPick;
console.log(`Available cases: ${game.getAvailableCases()}`);
console.log(`Values still in play: ${game.getValuesInPlay()}`);
if (casesLeftToOpen === 0) {
// switch the state.
currentState = 'offerAvailable';
console.log(`The offer is at ${game.getOffer()}`);
console.log('Deal or no deal? (Please type "deal" or "nodeal")');
}
else {
console.log(`Please pick another case. There are ${casesLeftToOpen} left to open.`);
}
}
}
function processOfferAvailableState() {
if (line !== 'deal' && line !== 'nodeal') {
console.log(`The offer is at ${game.lastOffer}`);
console.log('Deal or no deal? (Please type "deal" or "nodeal")');
}
else if (line === 'nodeal') {
let hasMoreRounds = game.sayNoDeal();
console.log(`Available cases: ${game.getAvailableCases()}`);
if (hasMoreRounds) {
console.log(`Please pick ${game.casesToPick} cases, one at a time.`);
currentState = 'eliminatingCases';
}
else {
console.log(`Your case is worth ${game.revealPlayerCase()}`);
rl.close();
}
}
else if (line === 'deal') {
game.sayDeal();
console.log(`A deal of ${game.lastOffer} has been accepted!`);
console.log(`Your case contained ${game.revealPlayerCase()}`);
rl.close();
}
}
switch (currentState) {
case 'intro':
processIntroState();
break;
case 'startGame':
processStartGameState();
break;
case 'eliminatingCases':
processEliminatingCasesState();
break;
case 'offerAvailable':
processOfferAvailableState();
break;
}
rl.setPrompt(prefix, prefix.length);
rl.prompt();
}
function whenLeaving() {
console.log();
console.log('Thanks for playing.');
process.exit(0);
}
rl.on('line', whenLineEntered).on('close', whenLeaving);
console.log('Welcome to Deal or No Deal. Now, who are you?');
rl.setPrompt(prefix, prefix.length);
rl.prompt();