Home > Enterprise >  Kotlin: Rock, Paper, Scissors
Kotlin: Rock, Paper, Scissors

Time:05-06

I'm trying to make a rock paper scissors program via Kotlin using Kotlin scripting only. I have the menu made and the functionality drawn out, but I'm having a hard time linking the two together. I feel like there is something I am missing to make it loop together. Any guidance would be appreciated!

fun main () {
  var userChoice = 0
  do {
    // start Program Start menu
    println("Hello! Hows about we play a game of Rock-Paper-Scissors? ")
    println("Rules are simple:")
    println(" Rock beats Scissors.")
    println(" Scissors beats Paper.")
    println(" Paper beats Rock.")
    println("Wanna play? (Y or N)")
    val answer = readLine()!!
    if (answer == "N" || answer == "n") {
      println("D'aw, maybe next time then!")
    } else if (answer == "Y" || answer == "y") {
      println("AW YEAH! Let's play!")

      // instruction menu
      println("It's simple! type in: ")
      println("'1' for Rock")
      println("'2' for Paper")
      println("'3' for Scissors")
      println("'4' to quit game")

      // Re-Prompt for user choice
      println("SO! What will it be? Rock, Paper, or Scissors?: ")
      // Get user choice
      userChoice = readLine()!!.toInt()

      when (userChoice) {
        // user choice from menu options

        1 -> println("You selected Rock!")
        2 -> println("You selected Paper!")
        3 -> println("You selected Scissors!")
        else -> {
          if (userChoice != 4) {
            println("ERROR: Please selection a valid option.")
          }
        }
      }
    }
  } while (userChoice != 4)
  //Computer's choice

  var win = 0
  var loose = 0
  var draw = 0

  for (i in 1..3) {
    var compChoice = (1..3).random()

    if (userChoice == 1 && compChoice == 1) {
      println("Rock breaks scissors - You win!")
      win
    } else if (userChoice == 2 && compChoice == 2) {
      println("Paper covers rock - You win!")
      win
    } else if (userChoice == 3 && compChoice == 3) {
      println("Scissors cut paper - You win!")
      win
    } else if (userChoice == 1 && compChoice == 2) {
      println("Rock breaks scissors - Computer wins!")
      loose  
    } else if (userChoice == 2 && compChoice == 3) {
      println("Paper covers rock - Computer wins!")
      loose  
    } else if (userChoice == 3 && compChoice == 1) {
      println("Scissors cut paper - Computer wins!")
      loose  
    } else {
      println("It's a draw!")
      draw  
    }
    println("Score: You $win, Computer $loose, Neither $draw")

  }
}```

CodePudding user response:

PLEASE don't rewrite user's code that is obviously a homework problem!!! This does not help the student - only makes you feel smarter and gain reputation. It is OK to help people with general questions like : how do I get a random number? or how do I truncate a string? BUT NOT TO DO THEIR HOMEWORK FOR THEM!!!

CodePudding user response:

Here take a look code below, if you have questions you can ask in comments

var win = 0
var loose = 0
var draw = 0

fun main() {
    var isGameRunning = true

    // start Program Start menu
    println("Hello! Hows about we play a game of Rock-Paper-Scissors? ")
    println("Rules are simple:")
    println(" Rock beats Scissors.")
    println(" Scissors beats Paper.")
    println(" Paper beats Rock.")

    while (isGameRunning) {

        println("Wanna play? (Y or N)")

        val answer = readLine()!!
        if (answer == "N" || answer == "n") {
            println("D'aw, maybe next time then!")
            break // exit while loop
        }

        println("AW YEAH! Let's play!")

        // instruction menu
        println("It's simple! type in: ")
        println("'1' for Rock")
        println("'2' for Paper")
        println("'3' for Scissors")
        println("'4' to quit game")

        win = 0
        loose = 0
        draw = 0

        for (i in 1..3) {
            val userChoice = userTurn()
            if (userChoice == 4) {
                isGameRunning = false
                break
            }
            //Computer's choice
            computerTurn(userChoice)
        }

    }

}

private fun userTurn(): Int {
    // Re-Prompt for user choice
    println("SO! What will it be? Rock, Paper, or Scissors?: ")
    // Get user choice
    val userChoice = readLine()!!.toInt()

    when (userChoice) {
        // user choice from menu options

        1 -> println("You selected Rock!")
        2 -> println("You selected Paper!")
        3 -> println("You selected Scissors!")
        else -> {
            if (userChoice != 4) {
                println("ERROR: Please selection a valid option.")
            }
        }
    }

    return userChoice
}

private fun computerTurn(userChoice: Int) {
    val compChoice = (1..3).random()

    if (userChoice == 1 && compChoice == 1) {
        println("Rock breaks scissors - You win!")
        win  
    } else if (userChoice == 2 && compChoice == 2) {
        println("Paper covers rock - You win!")
        win  
    } else if (userChoice == 3 && compChoice == 3) {
        println("Scissors cut paper - You win!")
        win  
    } else if (userChoice == 1 && compChoice == 2) {
        println("Rock breaks scissors - Computer wins!")
        loose  
    } else if (userChoice == 2 && compChoice == 3) {
        println("Paper covers rock - Computer wins!")
        loose  
    } else if (userChoice == 3 && compChoice == 1) {
        println("Scissors cut paper - Computer wins!")
        loose  
    } else {
        println("It's a draw!")
        draw  
    }
    println("Score: You $win, Computer $loose, Neither $draw")

}

  • Related