Created
April 17, 2014 12:08
-
-
Save DeepSky8/10978270 to your computer and use it in GitHub Desktop.
Updated, more modular CoinFlip betting 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
object coinFlip { | |
val wagers = "Great! How much do you want to wager? " | |
val increments25 = "Remember, Honest Freddy needs you to bet in increments of 25 credits: " | |
val increments25ForYou = "If you don't bet in exact increments of 25 credits, Freddy will just bet 25 credits for you. " | |
val headsTails = "Do you want to bet heads or tails? " | |
val freddyBet = "If Honest Freddy doesn't understand whether you picked heads or tails, he'll just bet heads for you. " | |
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 ready = "Are you ready for another match of Outback CoinFlip? " | |
def wager(bet: Int, heads: Boolean, total: Int, flip: Int): (Boolean, Boolean, Int) = { | |
if (total > 1000) { | |
(false, !(heads), total - bet) | |
} else { | |
if (heads == flip) { | |
(true, heads, total + bet) | |
} else { | |
(false, !(heads), total - bet) | |
} | |
} | |
} | |
case class WagerResult(wagerResult: Boolean, humanFlipChoice: Int, reamingBalance: Int) | |
def confirm(n: Int): Int = { | |
if (n % 25 == 0) { | |
n | |
} else { | |
25 | |
} | |
} | |
def afford(bet: Int, stake: Int): Boolean = bet <= stake | |
def feedBack(list: (Boolean, Boolean, Int)): List[String] = list match { | |
case (true, false, result) => List("won", "tails", result.toString) | |
case (true, true, result) => List("won", "heads", result.toString) | |
case (false, true, result) => List("lost", "heads", result.toString) | |
case (false, false, result) => List("lost", "tails", result.toString) | |
} | |
def readOut(l: List[String]): String = l(0) match { | |
case "won" => "Congratulations! You won the flip, which ended up being " + l(1) + " and you now have " + l(2) + " credits!" | |
case "lost" => "My condolences. You lost the flip, which ended up being " + l(1) + " and you now have " + l(2) + " credits." | |
} | |
def call(s: String): Boolean = s.toLowerCase() match { | |
case "heads" => false | |
case "head" => false | |
case "tails" => true | |
case "tail" => true | |
case _ => false | |
} | |
abstract class GameAction { | |
def execute(gameState: GameState): GameState | |
} | |
sealed trait GameState | |
case object Start extends GameState | |
case class StartAgain(stake: Int) extends GameState | |
case class Wager(stake: Int) extends GameState | |
case class CallFlip(stake: Int, bet: Int) extends GameState | |
case class Results(list: (Boolean,Boolean, Int)) extends GameState | |
case object End extends GameState | |
object StartGameAction extends GameAction { | |
def execute(gameState: GameState): GameState = { | |
println(ready) | |
val input = readLine() | |
input.toLowerCase match { | |
case "yes" => Wager(500) | |
case "no" => End | |
case _ => StartAgain(500) | |
} | |
} | |
} | |
object StartAgainGameAction extends GameAction { | |
def execute(gameState: GameState): GameState = gameState match { | |
case StartAgain(stake) => { | |
println(again) | |
print(ready) | |
val input = readLine() | |
input.toLowerCase match { | |
case "yes" => Wager(stake) | |
case "no" => End | |
case _ => StartAgain(stake) | |
} | |
} | |
case _ => gameState | |
} | |
} | |
val r = new java.util.Random | |
object WagerGameAction extends GameAction { | |
def execute(gameState: GameState): GameState = gameState match { | |
case Wager(stake) => { | |
println(wagers) | |
print(increments25) | |
print(increments25ForYou) | |
val betVal = readLine() | |
val realBet = confirm(scala.math.abs(betVal.toInt)) | |
if (!afford(realBet, stake)) { | |
println("You don't have enough credits to back that bet. You have " + stake + " credits. ") | |
Wager(stake) | |
} else { | |
CallFlip(stake, realBet) | |
} | |
} | |
} | |
} | |
object CallFlipGameAction extends GameAction { | |
def execute(gameState: GameState): GameState = gameState match { | |
case CallFlip(stake, realBet) => { | |
val flip = r.nextInt(2) | |
print(headsTails) | |
println(freddyBet) | |
val tails = readLine() | |
val result = wager(realBet, call(tails), stake, flip) | |
Results(result) | |
} | |
} | |
} | |
object ResultsGameAction extends GameAction { | |
def execute(gameState: GameState): GameState = gameState match { | |
case Results(result) => { | |
println(readOut(feedBack(result))) | |
val (_, _, newTotal) = result | |
print("Are you ready for another match of Outback CoinFlip? You can wager up to " + newTotal + " credits. ") | |
val input = readLine() | |
input.toLowerCase match { | |
case "yes" => Wager(newTotal) | |
case "no" => End | |
case _ => StartAgain(newTotal) | |
} | |
} | |
} | |
} | |
def gameLoop(gameState: GameState): Unit = gameState match { | |
case Start => gameLoop(StartGameAction.execute(Start)) | |
case StartAgain(stake) => gameLoop(StartAgainGameAction.execute(StartAgain(stake))) | |
case w: Wager => gameLoop(WagerGameAction.execute(w)) | |
case CallFlip(stake:Int, bet: Int) => gameLoop(CallFlipGameAction.execute(CallFlip(stake:Int, bet: Int))) | |
case Results(result) => gameLoop(ResultsGameAction.execute(Results(result))) | |
case End => println(leave) | |
} | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment