Created
December 30, 2016 05:19
-
-
Save WesJD/a2d8821e0c3d52a6572f96dec8bba248 to your computer and use it in GitHub Desktop.
Rock Paper Scissors in Scala (a little intro project)
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
package net.wesjd.rockpaperscissors | |
import java.util.concurrent.ThreadLocalRandom | |
import scala.collection.JavaConverters | |
import scala.io.StdIn._ | |
import scala.reflect.ClassTag | |
class RockPaperScissors { | |
class Player(name: String) { | |
var wins: Int = 0 | |
} | |
object PlayType extends Enumeration { | |
type PlayType = Value | |
val ROCK, PAPER, SCISSORS = Value | |
def compare(x: PlayType.Value, y: PlayType.Value): Int = { | |
if(x == y) return 0 | |
x match { | |
case PlayType.PAPER => | |
y match { | |
case PlayType.SCISSORS => 1 | |
case PlayType.ROCK => -1 | |
} | |
case PlayType.SCISSORS => | |
y match { | |
case PlayType.PAPER => 1 | |
case PlayType.ROCK => -1 | |
} | |
case PlayType.ROCK => | |
y match { | |
case PlayType.SCISSORS => 1 | |
case PlayType.PAPER => -1 | |
} | |
} | |
} | |
} | |
do { | |
println() | |
val computer: Player = new Player("Computer") | |
val user: Player = new Player("User") | |
val rounds = requestTypeSafely[Int]("How many rounds do you want to play? (must be odd!)", (input: String) => { | |
val converted = input.toInt | |
if(converted % 2 == 0) throw new Exception | |
else converted | |
}) | |
val i: Int = 0 | |
for( i <- 1 to rounds) { | |
println("---------------[ ROUND " + i + " ]---------------") | |
var computerPlay, userPlay: Option[PlayType.Value] = None | |
do { | |
computerPlay = Some(PlayType.values.toVector(ThreadLocalRandom.current().nextInt(PlayType.values.size))) | |
userPlay = Some(requestTypeSafely[PlayType.Value]( | |
"What do you want to play? " + String.join(", ", JavaConverters.asJavaIterable(PlayType.values.map(v => v.toString))), | |
(input: String) => PlayType.withName(input.toUpperCase) | |
)) | |
println("The computer plays " + computerPlay.get + "!") | |
} | |
while({ | |
if(computerPlay.isEmpty || userPlay.isEmpty) true | |
else { | |
val result: Int = PlayType.compare(userPlay.get, computerPlay.get) | |
result match { | |
case 1 => | |
println("You win this round!") | |
user.wins += 1 | |
false | |
case 0 => true | |
case -1 => | |
println("You loose this round!") | |
computer.wins += 1 | |
false | |
} | |
} | |
}) | |
} | |
println("------------------------------") | |
println("You had " + user.wins + " wins.") | |
println("The computer had " + computer.wins + " wins.") | |
print("Overall, ") | |
if(user.wins == computer.wins) println("it was a tie!") | |
else if(user.wins > computer.wins) println("you win!") | |
else println("the computer wins.") | |
} | |
while(requestPlayAgain()) | |
println("Goodbye.") | |
def requestPlayAgain(): Boolean = requestTypeSafely[Boolean]("Do you want to play again? [y/n]", (input: String) => | |
if (input.startsWith("y")) true | |
else if (input.startsWith("n")) false | |
else throw new Exception | |
) | |
def requestTypeSafely[T:ClassTag](ask: String, conversion: Function[String, T]): T = { | |
requestTypeSafely(ask, conversion, failed = false) | |
} | |
def requestTypeSafely[T:ClassTag](ask: String, conversion: Function[String, T], failed: Boolean): T = { | |
try { | |
println(ask) | |
conversion.apply(readLine()) | |
} catch { | |
case e: Exception => requestTypeSafely(if (failed) ask else "Invalid answer. " + ask, conversion, failed = true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment