-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRockPaperScissors.kt
39 lines (31 loc) · 1.25 KB
/
RockPaperScissors.kt
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
34
35
36
37
38
39
import kotlin.random.Random
fun main() {
val options = listOf("rock", "paper", "scissors")
println("Welcome to Rock-Paper-Scissors game!")
while (true) {
println("Enter your choice (rock, paper, or scissors): ")
val playerChoice = readLine()?.toLowerCase()
if (playerChoice !in options) {
println("Invalid choice. Please choose from rock, paper, or scissors.")
continue
}
val computerChoice = options[Random.nextInt(options.size)]
println("Computer chose: $computerChoice")
println("You chose: $playerChoice")
when {
playerChoice == computerChoice -> println("It's a tie!")
playerChoice == "rock" && computerChoice == "scissors" ||
playerChoice == "paper" && computerChoice == "rock" ||
playerChoice == "scissors" && computerChoice == "paper" -> {
println("You win!")
}
else -> println("Computer wins!")
}
println("Do you want to play again? (yes/no): ")
val playAgain = readLine()?.toLowerCase()
if (playAgain != "yes") {
println("Thanks for playing!")
break
}
}
}