Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Computer Wins Everytime in Rock Paper Scissor Game

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!")
}

}
`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading