Published on

Android Kotlin 101: Rock Paper Scissors Game

Authors

Now that you have a basic understanding of the Kotlin programming language, it's time to do a small project to affirm your coding skills! Let's go!!!

Analysis of the Rock Paper Scissors Game

  • Refer to the rules of Rock Paper Scissors here --> Wiki
  • Rock Paper Scissors has 3 different strikes for 1 game: We can use an Enum to represent them
  • The state for 1 game is Win, Draw, Lose: We can use an Enum to represent them
  • The score of each game is a number: We can use an Integer variable to represent it
  • The game needs many different rounds without interruption (turning off and on the program): We can use a while(true) loop to simulate. When the player wants to stop the game, a condition is needed to exit the while loop
  • Logic to compare the strikes with each other
ROCK > SISSORS > PAPER > ROCK

Implement Rock Paper Scissors Game using Kotlin

// Version: 1.0

enum class Role(val number: Int) {
    ROCK(1),
    PAPER(2),
    SCISSORS(3)
}

enum class TurnResult(val number: Int) {
    DRAW(0),
    USER_WIN(1),
    COMPUTER_WIN(2)
}

fun playRoleFromNumber(number: Int): String {
    return when (number) {
        Role.ROCK.number -> "Rock"
        Role.PAPER.number -> "Paper"
        Role.SCISSORS.number -> "Scissors"
        else -> "Invalid input"
    }
}

fun main() {
    // implement paper, rock, scissors game here
    // 1 = rock, 2 = paper, 3 = scissors
    // print the result of the game
    // print "You win!" if you win, "You lose!" if you lose, "Draw!" if it's a draw
    var userScore: Int = 0
    var computerScore: Int = 0

    while (true) {
        println("------ ------")

        println("Enter your choice: 1 - Rock, 2 - Paper, 3 - Scissors, 0 - Exit")
        val userInput: Int = readLine()?.toIntOrNull() ?: 0
        if (userInput  computerScore) {
                println("User win the game")
            } else if (userScore  {
                when (computerInput) {
                    Role.ROCK.number -> turnResult = TurnResult.DRAW
                    Role.PAPER.number -> turnResult = TurnResult.COMPUTER_WIN
                    Role.SCISSORS.number -> turnResult = TurnResult.USER_WIN
                }
            }

            Role.PAPER.number -> {
                when (computerInput) {
                    Role.ROCK.number -> turnResult = TurnResult.USER_WIN
                    Role.PAPER.number -> turnResult = TurnResult.DRAW
                    Role.SCISSORS.number -> turnResult = TurnResult.COMPUTER_WIN
                }
            }

            Role.SCISSORS.number -> {
                when (computerInput) {
                    Role.ROCK.number -> turnResult = TurnResult.COMPUTER_WIN
                    Role.PAPER.number -> turnResult = TurnResult.USER_WIN
                    Role.SCISSORS.number -> turnResult = TurnResult.DRAW
                }
            }

            else -> {
                println("Invalid input")
                continue
            }
        }
        println("User: ${playRoleFromNumber(userInput)}")
        println("Computer: ${playRoleFromNumber(computerInput)}")
        if (turnResult == TurnResult.USER_WIN) {
            println("User win this turn!")
            userScore++
        } else if (turnResult == TurnResult.COMPUTER_WIN) {
            println("Computer win this turn!")
            computerScore++
        } else {
            println("This turn is draw!")
        }
        println("User score: $userScore")
        println("Computer score: $computerScore")
    }
}

Implement the logic for the Rock Paper Scissors game - Kotlin 101 project - Android Mastery