Ok, since Creating a Street Dice Game Pt1 I realize I needed a testing interface, and I thought back to DOS batch files. When I was young and everything was run from command line, sometimes you wanted to boot a disk and run a program or you wanted a simple menu on boot and you had to create a .bat file. So what I set out to do today was to create a simple menu for testing function that I will use in the Street Dice game and this gave me a chance to practice the switch syntax and also to again use the readline library in this project.
Here is my final code for the night:
//determine when to declare a variable and when to import?
// when you import it is just a code block
// when you declare a variable and require you bring in an object
const readline = require('readline');
// simplified
//https://altcodeunicode.com/alt-codes-die-checkers-shogi-symbols/
// Players must first identify the player who will be shooting dice – the shooter.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function rollDice(numDice, sides) {
let diceArray = [];
for (let i = 0; i < numDice; i++) {
let dice = Math.floor(Math.random() * sides) + 1; // should be 1 - 6
diceArray.push(dice);
}
return diceArray;
}
function sumDice(diceArray) {
let diceTotal = 0;
for (let i = 0; i < diceArray.length;i++) {
diceTotal += diceArray[i]
}
return diceTotal;
}
function setPoint(player, rollTotal) {
//this need to set a players point parameter (value) PROPERTY!
console.log(`${player+1}'s point is ${rollTotal}`);
}
function determineOutcome(throwTotal, timesRolled) {
if (timesRolled === 0) {
}
// win, lose or play again
// true, false or undefined
}
let player0;
let player1;
// let player0Bank = 20;
// let player1Bank = 20;
// let timesRolled = 0;
function setShooter() {
let shooterDetermined = false;
//loop roll until see who goes first
while (!shooterDetermined) {
player0 = rollDice(1,6)[0];
console.log("Player 1: ", player0);
player1 = rollDice(1,6)[0];
console.log("Player 2: ", player1);
if ( player0 > player1) {
// UI Human Interface to allow player to click to roll
console.log('Player 1 goes first.')
shooterDetermined = true;
return 0;
} else if (player1 > player0) {
// Computer rolls till outcome
console.log('Player 2 goes first.')
shooterDetermined = true;
return 1;
}
}
}
// The shooter will then need to make a bet followed by the rest of the group in the clockwise direction.
// Each player can cover a portion of or all of the shooter’s bet.
// Betting continues until the shooter’s wager is matched.
let bet = 0;
// The come out roll comes next.
// This is the game’s first roll and it could end the game if it is a 7, 11, 2, 3 or 12.
// The shooter and any other player who bet in favor of the shooter win the game if a 7 or 11 is rolled.
// If a 2, 3 or 12 come up when the dice are rolled the shooter and other players who bet for him lose.
// A Point number, which is a number other than those mentioned above, must be set up.
// So if the come out roll is not any of those numbers listed above that number will be designated as the point number.
// The roll is next and the goal is for the shooter to roll the number identified as the point before he rolls a 7.
// The 7 is referred to an “Out 7” and once the shooter gets this before rolling the point he loses the game.
// Rolling dice proceeds until a 7 or the Point is rolled.
// The shooter loses if the 7 comes up and wins if the Point is rolled.
// If other numbers are rolled the shooter continues rolling the dice. The round ends only after a 7 or the point is rolled.
//if we try to resolve the dice and there is now point value then set point
var recursiveAsyncReadLine = function () {
rl.question('MENU: \n 1. Set shooter \n 2. Roll Dice \n 3. Set Point \n 4. Resolve dice(game logic) \n 5. Set Bet \n 6. Resolve Bet\nEnter an option =>', function (entry) {
if (entry == 'exit' || entry == 'quit') //we need some base case, for recursion
return rl.close(); //closing RL and returning from function.
// Create switch to run functions
switch(entry) {
case ('1'):
let startingPlayer = setShooter();
console.log(`Player${startingPlayer+1} is shooter.`);
// code block
break;
case ('2'):
let diceResult = rollDice(2, 6);
console.log("Roll Result: ", diceResult);
console.log('Dice Total: ', sumDice(diceResult));
// code block
break;
case ('3'):
setPoint(startingPlayer, diceResult);
default:
// code block
console.log('Invalid Response: ');
}
//console.log('Entry: ', entry);
recursiveAsyncReadLine(); //Calling this function again to ask new question
});
};
recursiveAsyncReadLine(); //we have to actually start our recursion somehow
rl.on("close", function() {
console.log("\n Exiting Program... \n");
process.exit(0);
});
Please check out the video below, where I walk through my thought process: