Post

Kotlin Rock, Paper, Scissors Game

Introduction

  • Just the classic game of rock, paper, scissors, but in the terminal.
  • The game will be interactive and the user will be able to play against the computer.
  • The user will be prompted to enter their choice.
  • The computer will randomly choose rock, paper, or scissors.
  • The game will then determine the winner and display the result.

Technics and Methods used

  • when expression
  • readLine() function
  • Random class

Steps

  1. Create a new Kotlin file named RockPaperScissors.kt.
  2. Create a function named main to start the game.
  3. Create an empty variable called computerChoice to store the computer’s choice.
  4. Create an empty variable called playerChoice to store the player’s choice.
  5. use the readLine() function to prompt the player to enter their choice and store it in the playerChoice variable.
  6. Use the Random class to generate a random number from a range between 1 and 3.
  7. Use the random number generated to determine the computer’s choice (rock, paper, or scissors) and store it in the computerChoice variable.
  8. print the computer’s choice based on the random number generated.
  9. Use the when expression to determine the winner based on the player’s choice and the computer’s choice.
  • All together the code in the main function will look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
fun main(){
    // Variables to store the choices
    var computerChoice = ""
    var playerChoice = ""
    // Prompt the player to enter their choice
    print("*******************************\n")
    print("Welcome to this interactive Rock \uD83E\uDEA8, Paper \uD83D\uDCC4, Scissors ✂\uFE0F Game!\n")
    print("*******************************\n")
    print("\n")
    print("************** Your Turn: *****************\n")
    print("Rock, Paper or Scissors? Enter your choice:\n")

    // Store the player's choice
    playerChoice = readln()

    // Generate a random number between 1 and 3
    val randomNumber = (1..3 ).random()

    // Determine the computer's choice based on the random number

    when (randomNumber) {
        1 -> computerChoice = "Rock"
        2 -> computerChoice = "Paper"
        3 -> computerChoice = "Scissors"
    }
    print("************ Computers Turn: *******************\n")

    // Print the computer's choice
    println(computerChoice)

// Determine the winner
    val winner = when {playerChoice == computerChoice -> "Tie"
        playerChoice == "Rock" && computerChoice == "Scissors" -> "Player"
        playerChoice == "Paper" && computerChoice == "Rock" -> "Player"
        playerChoice == "Scissors" && computerChoice == "Paper" -> "Player"
        else -> "Computer"

    }

    print("*******************************\n")
    print(" Results: \n")
    print("\n")

    // Print the winner
    when (winner) {
        "Tie" -> println("It's a tie! \uD83E\uDD1D\uD83C\uDFFF")
        "Player" -> println("Player won! \uD83E\uDD73")
        else -> println("Computer won! \uD83D\uDCBB\uFE0F")
    }
    print("\n")
    print("************ Thanks for Playing! \uD83D\uDE0C *******************\n")

}
  1. Run the program and play the game!

Adding a check for invalid input

  • If the player enters an invalid choice, the game should prompt the player to enter a valid choice.
  • Add a check to ensure that the player’s choice is either rock, paper, or scissors.
1
2
3
4
5
6
// Check for invalid input

while (playerChoice != "Rock" && playerChoice != "Paper" && playerChoice != "Scissors") {
    print("Invalid choice! Please enter Rock, Paper, or Scissors:\n")
    playerChoice = readln()
}
This post is licensed under CC BY 4.0 by the author.