scala猜拳小游戏
时间: 2024-03-24 14:34:35 浏览: 83
微信小程序-猜拳游戏
Scala猜拳小游戏是一个简单的命令行游戏,玩家可以与计算机进行猜拳对战。以下是一个简单的实现示例:
```scala
import scala.io.StdIn
import scala.util.Random
object RockPaperScissorsGame {
def main(args: Array[String]): Unit = {
val validChoices = List("rock", "paper", "scissors")
val random = new Random()
println("Welcome to Rock-Paper-Scissors game!")
println("Enter your choice (rock, paper, or scissors):")
val playerChoice = StdIn.readLine().toLowerCase()
if (!validChoices.contains(playerChoice)) {
println("Invalid choice. Please choose again.")
return
}
val computerChoice = validChoices(random.nextInt(validChoices.length))
println(s"Player chooses $playerChoice")
println(s"Computer chooses $computerChoice")
if (playerChoice == computerChoice) {
println("It's a tie!")
} else if (
(playerChoice == "rock" && computerChoice == "scissors") ||
(playerChoice == "paper" && computerChoice == "rock") ||
(playerChoice == "scissors" && computerChoice == "paper")
) {
println("Player wins!")
} else {
println("Computer wins!")
}
//
阅读全文