Last active
October 26, 2018 14:54
-
-
Save 22a/dc429e3b883b082e93fcb2827bab60e6 to your computer and use it in GitHub Desktop.
tcd <-> inter.com mentoring - high-low card game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// importing readline library - basically a scanner equivalent | |
const readlineSync = require('readline-sync'); | |
// define our number of guesses required to win as a constant | |
const winningNumberOfGuesses = 4; | |
// define a function that takes an integer card number and returns the english | |
// name string for that card 2->"2", 10->"Jack", etc. | |
// how you implement this doesn't matter all that much - here I've defined a | |
// hash of number to card name pairs and used the funtion's cardNum parameter | |
// to perform a lookup in this hash. | |
// this hash could just as easibly be defined as a constant | |
const cardToString = (cardNum) => { | |
return { | |
2: '2', | |
3: '3', | |
4: '4', | |
5: '5', | |
6: '6', | |
7: '7', | |
8: '8', | |
9: '9', | |
10: 'Jack', | |
11: 'Queen', | |
12: 'King', | |
13: 'Ace', | |
}[cardNum] | |
}; | |
// define a function that returns a random number between 2 and 13 (inclusive) | |
// you could implement this using `import java.util.Random;` in the java world | |
const chooseRandomCard = () => { | |
const min = 2; | |
const max = 13; | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
// declare some variables we're going to be using later in the loop | |
let currentCard; | |
let nextCard; | |
let correctGuessCount = 0; | |
// pick the first card and print it to the console - we need to do this outside | |
// the loop so that we have an initial value for the card | |
currentCard = chooseRandomCard(); | |
console.log(`The card is a ${cardToString(currentCard)}`); | |
while (correctGuessCount < winningNumberOfGuesses) { | |
let currentGuess; | |
let hasValidInput = false; | |
// implement error handling on the input - keep asking the user for input | |
// until they give us either "higher" or "lower" | |
while (!hasValidInput) { | |
// you would use scanner here insead of readline - this basically just | |
// prints the string "Do you think ...", reads the user input, and stores | |
// it in the variable `currentGuess` | |
currentGuess = readlineSync.question('Do you think the next card will be higher, lower or equal? '); | |
if (currentGuess === "higher" || currentGuess === "lower") { | |
// if the input was valid, set `hasValidInput` to true so that we exit | |
// this inner loop and accept the input (stored in `currentGuess` above) | |
hasValidInput = true; | |
} else { | |
console.log(`${currentGuess} is not a valid input - please choose "higher" or "lower"`); | |
} | |
} | |
// Boolean: encode whether the current guess thought the next card would be | |
// higher here - if the guess was not "higher" then it was "lower" | |
const guessIsThatNextCardWillBeHigher = currentGuess === "higher"; | |
// pick the next random card | |
nextCard = chooseRandomCard(); | |
// Boolean: check if the current card is lower or higher than the next card | |
const nextCardIsHigher = nextCard > currentCard; | |
// Boolean: if the user's guess matches the outcome of the high/low check | |
const guessWasCorrect = guessIsThatNextCardWillBeHigher === nextCardIsHigher; | |
// prepare the next iteration of the loop by setting the currentCard variable | |
currentCard = nextCard; | |
// having this print here (instead of below the if(guessWasCorrect) statement | |
// at the end of the loop) allows us to show the user what the last card was | |
console.log(`The card is a ${cardToString(currentCard)}`); | |
if (guessWasCorrect) { | |
// increment our correct guess tally so that we will exit the loop when we | |
// hit 4 | |
correctGuessCount++; | |
} else { | |
// mock the user for losing | |
console.log('Oh no, you done goofed. Game over.') | |
// you would use System.exit() | |
process.exit() | |
} | |
} | |
// if code execution ever gets to here (ie. we were able to exit the loop) then | |
// we must have won - because if at any point we guessed incorrectly the | |
// program would have terminated prematurely (in the `process.exit()` line above) | |
console.log('Congratulations. You got them all correct.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment