Skip to content

Instantly share code, notes, and snippets.

@nicl
Created July 5, 2017 16:09
Show Gist options
  • Save nicl/721ac92979fb6d8141a3f586d33c3772 to your computer and use it in GitHub Desktop.
Save nicl/721ac92979fb6d8141a3f586d33c3772 to your computer and use it in GitHub Desktop.
Example higher or lower
import scala.util.Random
trait Guess
case object Higher extends Guess
case object Lower extends Guess
val deck = 1 to 10
def shuffle(decks: Seq[Seq[Int]]): Seq[Int] = {
val shuffled = Random.shuffle(decks.flatten)
println(s"\nShuffled decks $shuffled\n")
shuffled
}
def guess: Guess = Random.shuffle(Seq(Higher, Lower)).head
def play(deck: Seq[Int], correctGuessesToWin: Int, correctGuessesSoFar: Int): Unit = {
if (correctGuessesSoFar >= correctGuessesToWin) {
println("You win!!! Partay")
} else {
val head = deck.head
println(s"Card is $head. Guess 'Higher' or 'Lower'!?")
val playerGuess: Guess = guess // Get from user directly
println(s"Player guesses $playerGuess")
val next = deck.tail.head
playerGuess match {
case Higher if head < next => play(deck.tail, correctGuessesToWin, correctGuessesSoFar +1)
case Lower if next < head => play(deck.tail, correctGuessesToWin, correctGuessesSoFar + 1)
case _ => println(s"Oops, you dead (you made $correctGuessesSoFar correct guesses)")
}
}
}
for (n <- 1 to 3) play(shuffle(Seq(deck, deck)), correctGuessesToWin = 3, correctGuessesSoFar = 0)
@nicl
Copy link
Author

nicl commented Jul 5, 2017

Note, this was done in a Scala worksheet so that is the best place to copy and paste if you're keen on trying this out yourself.

@rrees
Copy link

rrees commented Jul 11, 2017

So this is a lot more sophisticated than I've seen in the wild. The recursion is pretty good and the deck modelling is a lot better than most.

You have hit one of the issues in the problem though I think. Once you have more than one deck there is the possibility of cards being equal in value and I think that causes you to lose the game here.

I think the recursive function should also return the state at the end of the game rather than Unit.

I had an extension to this (which seemed ridiculous in the light of experience) where the user could wager how many cards they would turn over and for each card above the minimum you scored more. I don't think it would add a lot more complexity to the problem you have here you would just need to calculate the game end state.

I also deliberately left out face cards because I was interviewing people with English as a second language and the vernacular around playing cards was proving too difficult for the interview process. Again I think face cards are relatively easy to incorporate into this solution by just mapping the numbers to string values.

In higher or lower an Ace can be counted as either a top or a bottom card but I think that would just add a condition to your matcher.

Looks like it's a pretty good solution (apart from the potential bug) and if I had this in an interview I would hire this person. Recursion, language library knowledge, pattern matching and range use. I'm not sure I'd want to spend another 30 minutes adding features.

Up to you whether you want to adopt it but I think this works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment