Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Random
- import kotlin.random.Random
- val choices = listOf("rock", "paper", "scissors")
- val winningCases = arrayOf(listOf("rock", "scissors"), listOf("paper", "rock"), listOf("scissors", "paper"))
- fun main() {
- println("Rock, Scissors or Paper?")
- val userChoice = userChoice()
- val compChoice = compChoice()
- result(userChoice, compChoice)
- }
- fun compChoice(): String {
- val randIndex = Random.nextInt(0, 3)
- return choices[randIndex]
- }
- fun userChoice(): String? {
- return readLine()?.toLowerCase()
- }
- fun result(userChoice: String?, compChoice: String) {
- print("I chose $compChoice, you chose $userChoice. ")
- val case = listOf(userChoice, compChoice)
- when(case) {
- in winningCases -> println("You won:'(")
- else -> if(userChoice != compChoice) println("You lost!") else println("Draw!")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement