Hello so I have created a simple rock paper scissor game using Kotlin. But the computer seems to win everytime even though I had won technically. Please have a look at my code and help me. Thanks `package com.blinky.kotlinbasics
fun main() {
val computerChoice = ""
var playerChoice = ""
println("Rock, Paper or Scissor? Enter your choice!")
playerChoice = readln()
val randomNumber = (1..3).random()
if(randomNumber == 1){
println("Rock")
}
else if(randomNumber == 2){
println("Paper")
}
else if(randomNumber == 3){
println("Scissor")
}
val winner = when{
playerChoice == computerChoice -> "Tie"
playerChoice == "Rock" && computerChoice == "Scissor" -> "Player"
playerChoice == "Paper" && computerChoice == "Rock" -> "Player"
playerChoice == "Scissor" && computerChoice == "Paper" -> "Player"
else -> "Computer"
}
if(winner == "Tie"){
println("The game is a tie")
}
else{
println(winner + " won!")
}
}
`
>Solution :
The computers choice is never saved, it is only printed. So instead of only printing the computers choice, you might want to modify one part of your code to:
if(randomNumber == 1){
println("Rock")
computerChoice = "Rock"
}
else if(randomNumber == 2){
println("Paper")
computerChoice = "Paper"
}
else if(randomNumber == 3){
println("Scissor")
computerChoice = "Scissor"
}