Last active
September 5, 2019 12:22
-
-
Save francbreno/cde5c371073e474fa4b9c04f7ebf4fcb to your computer and use it in GitHub Desktop.
Revisions
-
francbreno revised this gist
Sep 5, 2019 . No changes.There are no files selected for viewing
-
francbreno revised this gist
Feb 28, 2019 . 1 changed file with 8 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,22 +8,16 @@ trait Action { def actionType(): String } case class GuessAction(wasRight: Boolean, result: String) extends Action { override def actionType(): String = "GUESS" } case class GameState(tries: Int, wins: Int, lastResult: String, continue: Boolean = true) object GameState { def update(state: GameState, action: Action): GameState = action match { case GuessAction(true, result) => state.copy(tries = state.tries + 1, wins = state.wins + 1, lastResult = result) case GuessAction(false, result) => state.copy(tries = state.tries + 1, lastResult = result) case _ => state } } @@ -50,7 +44,7 @@ object GameInterface { object Game extends App { def start = gameLoop(GameState(0, 0, "", true), Random) @tailrec def gameLoop(state: GameState, random: Random): Unit = { @@ -67,8 +61,8 @@ object Game extends App { val rightGuess = isGuessRight(userOption, side) val action = if(rightGuess) new GuessAction(true, side) else new GuessAction(false, side) val nextState = GameState.update(state, action) @@ -100,4 +94,4 @@ object Game extends App { } Game.start } -
francbreno revised this gist
Feb 28, 2019 . 1 changed file with 43 additions and 29 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,39 +4,42 @@ import scala.annotation.tailrec import scala.io.StdIn import scala.util.Random trait Action { def actionType(): String } case class RightGuessAction(result: String) extends Action { override def actionType(): String = "RIGHT_GUESS" } case class WrongGuessAction(result: String) extends Action { override def actionType(): String = "WRONG_GUESS" } case class InvalidGuessAction() extends Action { override def actionType(): String = "INVALID_GUESS" } case class GameState(tries: Int, wins: Int, lastResult: String, continue: Boolean = true) object GameState { def update(state: GameState, action: Action): GameState = action match { case RightGuessAction(result) => state.copy(tries = state.tries + 1, wins = state.wins + 1, lastResult = result) case WrongGuessAction(result) => state.copy(tries = state.tries + 1, lastResult = result) case _ => state } } object PlayerInteraction { def inputOption(): String = StdIn.readLine.toUpperCase } object GameInterface { def showPrompt() = print("\n(h)eads, (t)ails or (q)quit? ") def showState(state: GameState) = println(s"tries: ${state.tries}, wins: ${state.wins}") def showAttemptResult(wasRight: Boolean) = if(wasRight) println("Yes! You guessed it!") else println("Sorry! You're wrong") def showGameOver(state: GameState) = { println("================") @@ -57,23 +60,34 @@ object Game extends App { // get user input (heads, tails or quit) val userOption = PlayerInteraction.inputOption() // Check user input userOption match { case "H" | "T" => { val side = flipCoin(random.nextInt(2)) val rightGuess = isGuessRight(userOption, side) val action = if(rightGuess) new RightGuessAction(side) else new WrongGuessAction(side) val nextState = GameState.update(state, action) guessResult(nextState, rightGuess) gameLoop(nextState, random) } case "Q" => endGame(state) case _ => gameLoop(state, random) } } def isGuessRight(playerGuess: String, side: String) = playerGuess == side def guessResult(state: GameState, wasRight: Boolean) = { GameInterface.showAttemptResult(wasRight) GameInterface.showState(state) } def endGame(state: GameState) = { GameInterface.showGameOver(state) GameInterface.showState(state) @@ -86,4 +100,4 @@ object Game extends App { } Game.start } -
francbreno revised this gist
Feb 27, 2019 . 1 changed file with 51 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,21 +5,28 @@ import scala.io.StdIn import scala.util.Random case class GameState(tries: Int, wins: Int, lastResult: String, continue: Boolean = true) //case class GameAction(typeAction: String, values: Any) // //case class Play(playerOption: String, flipResult: String) // //object Play { // // def check(play: Play): Boolean = play.flipResult == play.playerOption //} // //object GameState { // // def update(state: GameState, action: GameAction[String, Any]) = action match { // case GameAction("CONTINUE", _) => // new GameState(state.tries, state.wins, state.lastResult, true) // case GameAction("END", _) => // new GameState(state.tries, state.wins, state.lastResult, false) // case GameAction("WIN", _) => // new GameState(state.tries + 1, state.wins + 1, state.lastResult, true) // case GameAction("LOSE", _) => // new GameState(state.tries + 1, state.wins, action.values.get("").toString, true) // } //} object PlayerInteraction { @@ -32,39 +39,51 @@ object GameInterface { def showState(state: GameState) = println(s"tries: $state.tries, wins: $state.wins") def showGameOver(state: GameState) = { println("================") println("Game Over") println("================") } } object Game extends App { def start = gameLoop(new GameState(0, 0, "", true), Random) @tailrec def gameLoop(state: GameState, random: Random): Unit = { // Shows the prompt GameInterface.showPrompt() // get user input (heads, tails or quit) val userOption = PlayerInteraction.inputOption() userOption match { case "H" | "T" => { val side = flipCoin(random.nextInt(2)) val winsInc = if (rightGuess(userOption, side)) 1 else 0 val nextState = new GameState(state.tries + 1, state.wins + winsInc, side, true) gameLoop(nextState, random) } case "Q" => endGame(state) case _ => gameLoop(state, random) } } def rightGuess(playerGuess: String, side: String) = playerGuess == side def endGame(state: GameState) = { GameInterface.showGameOver(state) GameInterface.showState(state) } def flipCoin(value: Int): String = value match { case 0 => "H" case 1 => "T" case _ => "?" } Game.start } -
francbreno created this gist
Feb 27, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ package playground import scala.annotation.tailrec import scala.io.StdIn import scala.util.Random case class GameState(tries: Int, wins: Int, lastResult: String, continue: Boolean = true) case class GameAction[A](typeAction: String, values: Map[String, A]) object GameState { def update(state: GameState, action: GameAction[String, Any]) = action match { case GameAction("CONTINUE", _) => new GameState(state.tries, state.wins, state.lastResult, true) case GameAction("END", _) => new GameState(state.tries, state.wins, state.lastResult, false) case GameAction("WIN", _) => new GameState(state.tries + 1, state.wins + 1, state.lastResult, true) case GameAction("LOSE", _) => new GameState(state.tries + 1, state.wins, action.values.get("").toString, true) } } object PlayerInteraction { def inputOption(): String = StdIn.readLine() } object GameInterface { def showPrompt() = print("\n(h)eads, (t)ails or (q)quit? ") def showState(state: GameState) = println(s"tries: $state.tries, wins: $state.wins") def showGameOver(state: GameState) = { println("Game Over") println("================") showState(state) } } object Game extends App { @tailrec def gameLoop(state: GameState, random: Random): Unit = { // Shows the prompt GameInterface.showPrompt() // get user input (heads, tails or quit) val playerOption = PlayerInteraction.inputOption() // check play val result = // handle the action handleAction(state, playerOption) match { case GameState(_, _, "", false) => GameInterface.showGameOver(state) case nextState => gameLoop(nextState, random) } } // def checkResult(option: String, result: String) = def handleAction(state: GameState, option: String) = option match { case "H" | "T" => GameState.update(state, new GameAction("")) case "Q" => case _ => state } def winOrLose(option: String, result: String) = if (option == result) 1 else 0 }