Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Last active August 29, 2015 13:58
Show Gist options
  • Save DeepSky8/10201550 to your computer and use it in GitHub Desktop.
Save DeepSky8/10201550 to your computer and use it in GitHub Desktop.
Current Collected Coinflip Contents
CCCC
object coinFlip {
val wagers = "Great! How much do you want to wager?"
val headsTails = "Do you want to bet heads or tails? "
val freddy = "Remember, Honest Freddy needs you to bet in increments of 25 credits: "
val freddy2 = "If you don't bet in exact increments of 25 credits, Freddy will just bet 25 credits for you."
val heads = "Do you want to bet heads or tails? "
val heads2 = "If Honest Freddy doesn't understand whether you picked heads or tails, he'll just bet heads for you."
val yourStake = "Your stake is now "
val leave = "Well, thanks for coming out today, partner."
val again = "I didn't quite catch that, sonny. You'll have to speak up."
val stake = 500
var total = 500
def wager(b: Int, h: Int, t: Int): List[Int] = t match {
val r = new java.util.Random
val flip = r.nextInt(2)
case a if (t >= 1000) => h match {
case 0 => List(0, 1, t-b)
case 1 => List(0, 0, t-b)
case c if (t < 1000) => h match
case w if (h == flip) => List(1, h, t+b)
case l if (h != flip) => h match{
case 0 => List(0, 1, t-b)
case 1 => List(0, 0, t-b)
}
}
}
def confirm(n: Int): Int = {
if(n/25d == Int){
n
}else{
25
}
}
def feedBack(l: List[Int]): List[String] = l(0) match {
case 1 => l(1) match{
case 1 => List("won", "tails", l(2).toString)
case 0 => List("won", "heads", l(2).toString)
case 0 => l(1) match{
case 1 => List("lost", "tails", l(2).toString)
case 0 => List("lost", "heads", l(2).toString)
}
}
}
def readOut(l: List[String]): String = l(0) match {
case "won" => "Congratulations! You won the flip, which ended up being " + l(1) + "and " + l(2) + " credits were added to your total!"
case "lost" => "My condolences. You lost the flip, which ended up being " + l(1) + "and " + l(2) + " credits were subtracted to your total!"
}
def call(s: String): Int = s.toLowerCase() match {
case "heads" => 0
case "head" => 0
case "tails" => 1
case "tail" => 1
case _ => 0
}
abstract class GameAction {
def execute: GameState
}
sealed trait GameState
case object Start extends GameState
case object Wager extends GameState
case object End extends GameState
object StartGameAction extends GameAction {
def execute: GameState = {
println("Are you ready for another match of Outback CoinFlip?")
val input = readLine()
input.toLowerCase match {
case "yes" => Wager
case "no" => End
case _ => Start
}
}
}
object WagerGameAction extends GameAction {
def execute: GameState = {
//val r = new java.util.Random
//val flip = r.nextInt(2)
println(wagers)
print(freddy)
val betVal = readLine()
val intBet = scala.math.abs(betVal.toInt)
val finalBet = confirm(intBet)
println(freddy2)
print(heads)
val tails = readLine()
println(heads2)
val called = call(tails)
val result = wager(intBet, called, total)
println(readOut(feedBack(result)))
val newTotal[Int] = result(2)
var total = newTotal
println(yourStake + ".")
println("Are you ready for another match of Outback CoinFlip?")
val input = readLine()
input.toLowerCase match {
case "yes" => Wager
case "no" => End
case _ => Start
}
}
}
def gameLoop(gameState: GameState): Unit = gameState match {
case Start => gameLoop(StartGameAction.execute)
case Wager => gameLoop(WagerGameAction.execute)
case End =>
}
def main(args: Array[String]): Unit = {
println("Welcome to Outback CoinFlip!")
println("In this game, you'll be given 500 credits, your 'stake.'")
println("You can bet those credits in increments of 25 on the outcome of Honest Freddy's coin flip.")
println("Yessir, you won't find a better, more forgiving betting game out there.")
println("Honest Freddy won't hold your past bets against you. Every time you bet, the slate will be wiped clean.")
println("Honest Freddy DOES have to earn a living, however. Each time you bet, you must bet at least 25 credits.")
println("You can increase your bet in increments of 25.")
println("This means you can bet 25, 50, 75, and 100 credits, up to as many credits as you have.")
println()
println("Each time you bet, you have a chance to DOUBLE your money!")
println("If you bet 25 credits, and you bet correctly, you will receive 50 CREDITS back from Honest Freddy!")
println("If you bet 200 credits, you will get back a whopping 400 CREDITS!")
println("Tell me partner, have you EVER played a game of chance with such good odds?")
print("I didn't think so. Now, are you ready to play? Type Yes or No: ")
gameLoop(Start)
}
}
@bkyrlach
Copy link

bkyrlach commented Apr 9, 2014

Line 30 is an assignment statement, which always evaluates to Unit.

I believe what you want is...

def fixnum(n: Int): Int = scala.math.abs(n)

Which, since it's identical to just calling scala.math.abs, may not actually be a necessary def.

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