Welcome to the Rock, Paper, Scissors game! This simple game lets you play against the computer in the classic hand game of Rock, Paper, Scissors. The goal of the game is to choose one of the three options (rock, paper, or scissors) and compare it with the computer’s choice to determine the winner. The rules are as follows:

  • Rock beats Scissors.
  • Scissors beats Paper.
  • Paper beats Rock.

Objective

The objective of this program is to provide a basic implementation of the Rock, Paper, Scissors game in the Go programming language. It demonstrates the use of random number generation, basic input and output, and conditional statements to control the game flow.

Go Code: Rock, Paper, Scissors Game


package main

import (
    "fmt"
    "math/rand"
    "strings"
    "time"
)

func main() {
    // Seed the random number generator
    rand.Seed(time.Now().UnixNano())

    // Define the choices
    choices := []string{"rock", "paper", "scissors"}

    // Prompt the user to enter their choice
    fmt.Println("Welcome to Rock, Paper, Scissors!")
    fmt.Print("Enter your choice (rock, paper, or scissors): ")
    var userChoice string
    fmt.Scanln(&userChoice)
    userChoice = strings.ToLower(userChoice)

    // Validate user input
    if userChoice != "rock" && userChoice != "paper" && userChoice != "scissors" {
        fmt.Println("Invalid choice. Please enter rock, paper, or scissors.")
        return
    }

    // Generate the computer's choice
    computerChoice := choices[rand.Intn(len(choices))]

    // Display choices
    fmt.Println("Your choice:", userChoice)
    fmt.Println("Computer's choice:", computerChoice)

    // Determine the winner
    if userChoice == computerChoice {
        fmt.Println("It's a tie!")
    } else if (userChoice == "rock" && computerChoice == "scissors") ||
        (userChoice == "scissors" && computerChoice == "paper") ||
        (userChoice == "paper" && computerChoice == "rock") {
        fmt.Println("You win!")
    } else {
        fmt.Println("Computer wins!")
    }
}
        

Program Explanation

The program follows these basic steps:

  • The user is prompted to enter their choice (rock, paper, or scissors).
  • The program checks the user’s input for validity and ensures it’s one of the three options.
  • The computer randomly selects one of the three choices using the rand package.
  • Both the user’s and the computer’s choices are displayed.
  • The winner is determined based on the rules of Rock, Paper, Scissors, and the result is displayed.

How to Run the Program

To run this program on your local machine, you need to have Go installed. Follow these steps:

  1. Download and install Go from here.
  2. Save the above Go code in a file named rock_paper_scissors.go.
  3. Open a terminal or command prompt and navigate to the directory where you saved the file.
  4. Run the program using the command: go run rock_paper_scissors.go.
  5. Follow the on-screen instructions to play the game.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)