Как сохранить счет игры при использовании петель и получить счет в конце игры после выхода из игры. используя "Kotlin" - PullRequest
0 голосов
/ 20 апреля 2020

Как видите, я новичок. Я ищу помощь в обновлении оценки для каждого раунда пользовательской дозы. Я также хочу получить общее количество, когда пользователь выходит из игры. У меня проблемы с использованием переменной var вне l oop. Я понятия не имею, как сложить счет вместе. Спасибо за ваше время! Я использую Kotlin

fun main() {

    //explain game
    println("Welcome to Rock - Paper - Scissors!")
    println("This is a simple game of choosing on of either Rock, Paper, or Scissors against the computer's choice")
    println("The rules are:  ")
    println("   Rock beats Scissors")
    println("   Scissors beasts paper")
    println("   Paper beast Rock")
    println()

    //Prompt user if they would like to play
    print("Would you like to play ( Y or N )")

    var play = readLine()
// end of game stats


    if (play == "N" || play == "n") {  // if N or n
        println("Sorry you don't want to play with me!")
    }  else if (play == "Y" || play == "y") {

        // print the menu if Y or y
        println("1. Rock")
        println("2. Paper")
        println("3. Scissors")
        println()
        println("4. Quit ")
        println()
        print("Enter your choice:")
        // perform that action // display users choice
        var userChoice = readLine()!!.toInt()
        while (userChoice != 5) {
            println()



                // computer choice random
                var rand = (1..3).random()

                // stats
                var tie = 0
                var win = 0
                var losses = 0


                // computer choice display
                if (rand == 1) {
                    println(" Computer chose: Rock")
                }

                if (rand == 2) {
                    println(" Computer chose: Paper")
                }

                if (rand == 3) {
                    println(" Computer chose: Scissors")
                }


                // Players choice display
                if (userChoice == 1) {
                    println(" You chose: Rock ")
                }
                if (userChoice == 2) {
                    println(" You chose: Paper")
                }
                if (userChoice == 3) {
                    println(" You chose: Scissors")
                }
                if (userChoice == 4) {
                    println("Have a nice day!")
                    println("Stats")
                    //print stats here

                } else {
                    println("ERROR Please enter a valid menu choice.")
                }

                // win,lose.tie

                //win
                if (rand == 1 && userChoice == 2) {
                    win++
                }
                if (rand == 2 && userChoice == 3) {
                    win++
                }
                if (rand == 3 && userChoice == 1) {
                    win++
                }

                //losses
                if (rand == 1 && userChoice == 3) {
                    losses++
                }
                if (rand == 2 && userChoice == 1) {
                    losses++
                }
                if (rand == 3 && userChoice == 2) {
                    losses++
                }
                //ties
                if (rand == userChoice) {
                    tie++
                }

                //Stats
                println("----Stats----")
                println("Wins: $win")
                println("Losses: $losses")
                println("Ties: $tie")

                // menu
                println("1. Rock")
                println("2. Paper")
                println("3. Scissors")
                println()
                println("4. Quit ")
                println()
                print("Enter your choice:")
                // get users choice
                userChoice = readLine()!!.toInt()


            }

        }


}

1 Ответ

1 голос
/ 20 апреля 2020

Значения статистики должны быть объявлены вне скобок while, чтобы позволить var быть доступным за пределами l oop. Вместо

} else if () {
 while () {
var win = 0
var losses = 0
var ties = 0

println(" Win = $win")
println(" Losses = $losses")
println(" Ties = $ties")
 }
}

Должно быть

} else if () {
 var win = 0
var losses = 0
var ties = 0

while () {

}

println(" Win = $win")
println(" Losses = $losses")
println(" Ties = $ties")
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...