Merge pull request #340 from patimen/main

HiLow
This commit is contained in:
Jeff Atwood
2022-01-01 21:50:54 -08:00
committed by GitHub
2 changed files with 53 additions and 0 deletions

50
47_Hi-Lo/kotlin/HiLo.kt Normal file
View File

@@ -0,0 +1,50 @@
fun main() {
println(introText)
var winnings = 0
do {
winnings += playGame()
println("YOUR TOTAL WINNINGS ARE NOW $winnings DOLLARS")
} while(playAgain())
println("SO LONG. HOPE YOU ENJOYED YOURSELF!!!")
}
fun playGame():Int {
val amount = (1..100).random()
repeat(6) {
println("YOUR GUESS")
val guess = readln().toInt()
when {
guess == amount -> {
println("GOT IT!!!!!!!! YOU WIN $amount DOLLARS.")
return amount
}
guess > amount -> println("YOUR GUESS IS TOO HIGH")
else -> println("YOUR GUESS IS TOO LOW")
}
}
println("YOU BLEW IT...TOO BAD...THE NUMBER WAS $amount")
return 0
}
fun playAgain():Boolean {
println("PLAY AGAIN (YES OR NO)")
return readLine()?.uppercase() == "YES"
}
val introText = """
HI LO
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
THIS IS THE GAME OF HI LO.
YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE
HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS. IF YOU
GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!
THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,
IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS
""".trimIndent()

View File

@@ -0,0 +1,3 @@
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
Conversion to [Kotlin](https://kotlinlang.org/)