Advertisement
jasurbekdev

Rock

Aug 1st, 2020 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.86 KB | None | 0 0
  1. package Random
  2.  
  3. import kotlin.random.Random
  4.  
  5. val choices = listOf("rock", "paper", "scissors")
  6. val winningCases = arrayOf(listOf("rock", "scissors"), listOf("paper", "rock"), listOf("scissors", "paper"))
  7.  
  8. fun main() {
  9.     println("Rock, Scissors or Paper?")
  10.     val userChoice = userChoice()
  11.     val compChoice = compChoice()
  12.     result(userChoice, compChoice)
  13. }
  14.  
  15. fun compChoice(): String {
  16.     val randIndex = Random.nextInt(0, 3)
  17.     return choices[randIndex]
  18. }
  19.  
  20. fun userChoice(): String? {
  21.     return readLine()?.toLowerCase()
  22. }
  23.  
  24. fun result(userChoice: String?, compChoice: String) {
  25.     print("I chose $compChoice, you chose $userChoice. ")
  26.     val case = listOf(userChoice, compChoice)
  27.     when(case) {
  28.         in winningCases -> println("You won:'(")
  29.         else -> if(userChoice != compChoice) println("You lost!") else println("Draw!")
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement