HTMLify
Rock Paper Scissors Game in Kotlin
Views: 630 | Author: demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Random fun main() { val options = arrayOf("Rock", "Paper", "Scissors") while (true) { println("Enter your choice (Rock, Paper, Scissors) or 'exit' to quit:") val userChoice = readLine()?.capitalize() if (userChoice == "Exit") { println("Thanks for playing. Goodbye!") break } if (userChoice !in options) { println("Invalid choice. Please enter Rock, Paper, or Scissors.") continue } val random = Random().nextInt(options.size) val computerChoice = options[random] println("Computer chose: $computerChoice") when { userChoice == computerChoice -> println("It's a tie!") (userChoice == "Rock" && computerChoice == "Scissors") || (userChoice == "Paper" && computerChoice == "Rock") || (userChoice == "Scissors" && computerChoice == "Paper") -> println("You win!") else -> println("Computer wins!") } } } |