Skip to content

Instantly share code, notes, and snippets.

@Linus-Albertus
Last active September 7, 2021 20:50
Show Gist options
  • Save Linus-Albertus/41d7343acf4867d648b48307f81993ed to your computer and use it in GitHub Desktop.
Save Linus-Albertus/41d7343acf4867d648b48307f81993ed to your computer and use it in GitHub Desktop.
[Bagels] #Scala #BigBookPython #qu4nt
/*Bagels, by Lino Urdaneta based in al@inventwithpython.com
A deductive logic game where you must guess a number based on clues.
View this code at https://nostarch.com/big-book-small-python-projects
A version of this game is featured in the book "Invent Your Own
Computer Games with Python" https://nostarch.com/inventwithpython
Tags: short, game, puzzle*/
object Main extends App {
def playOnce() = {
val numDigits: Int = 4
val maxGuesses: Int = 10
val numbers = List("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
val secretNum = getSecretNum(numbers, numDigits)
printGameIntro(numDigits)
printGuessesByGame(maxGuesses, numDigits)
var numGuesses: Int = 1
while (numGuesses <= maxGuesses) {
val guess = inputGuess(numDigits)
val clues = getClues(guess, secretNum)
println(clues)
numGuesses += 1
if (guess == secretNum) {
println("You got it!")
numGuesses = maxGuesses + 1
}
else if (numGuesses > maxGuesses) {
println("You ran out of guesses.")
println(s"The answer was $secretNum.")
}
}
/**
* Play a round of the game.
*/
}
def inputGuess(numDigits: Int): String = {
var guess = scala.io.StdIn.readLine("> ")
while (guess.length != numDigits) {
println(s"The guess must have $numDigits characters!")
guess = scala.io.StdIn.readLine("> ")
}
guess
/**
* Requests an input from the user and checks the correct length.
*/
}
def getSecretNum(list: List[String], numDigits: Int): String = {
import scala.util.Random
val randomNumber = Random.shuffle(list).slice(0, numDigits).mkString("")
randomNumber
/**
* Returns a string made up of numDigits unique random digits.
*/
}
def printGameIntro(numDigits: Int) = {
println("Bagels, a deductive logic game.")
println("By Lino Urdaneta and Al Sweigart al@inventwithpython.com")
println("")
println(s"I am thinking of a ${numDigits}-digit number with no repeated digits.")
println("Try to guess what it is. Here are some clues:")
println("When I say: That means:")
println("Pico One digit is correct but in the wrong position.")
println("Fermi One digit is correct and in the right position.")
println("Bagels No digit is correct.")
println("")
println("For example, if the secret number was 248 and your guess was 843, the")
println("clues would be Fermi Pico.")
/**
* Prints intro at the start of the game.
*/
}
def printGuessesByGame(guesses: Int, numDigits: Int) = {
println(s"I have thought up a number of $numDigits (without repetitions).")
println(s"You have $guesses guesses to get it.")
/**
* Prints information at the start of the round.
*/
}
def getClues(guess: String, secretNum: String): String = {
val clues = cluesExpression(guess, secretNum)
if (clues.length == 0) "Bagles"
else {
scala.util.Sorting.quickSort(clues)
val sorted_clues: String = clues.mkString(" ")
sorted_clues
}
/**
* Returns a string with the pico, fermi, bagels clues for a guess
* and secret number pair.
*/
}
def cluesExpression(guess: String, secretNum: String): Array[String] = {
var clues = Array[String]()
for (i <- 0 until guess.length)
if (guess(i) == secretNum(i)) clues = clues :+ "Fermi"
else if (secretNum.contains(guess(i))) clues = clues :+ "Pico"
clues
/**
* Returns an array of strings with the clues sorted alphabetically.
* Take as parameters a guess (string) and secretNum (string).
*/
}
playOnce()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment