Rock, Paper, Scissors is a classic game that has been played for generations. In this blog, we will walk through how to build a simple command-line version of the game in Go.
Prerequisites
Before we dive into the code, ensure you have the Go programming language installed on your system. You can download it from Go's official website.
Understanding the Code
Let's break down the code and understand its structure.
Importing Necessary Packages
import (
"fmt"
"math/rand"
)
Here, we import the fmt
package for handling input and output operations and math/rand
to generate random choices for the computer.
Defining Variables
playerScore := 0
computerScore := 0
option := []string{"rock", "paper", "scissors"}
var playerChoice string
-
playerScore
andcomputerScore
are initialized to keep track of the game scores. -
option
is a slice containing the three possible choices: rock, paper, and scissors. -
playerChoice
is a variable that stores the player's choice.
Running the Game Loop
for {
computerChoice := option[rand.Intn(len(option))]
This loop runs indefinitely, allowing players to continue playing until they manually stop the program.
-
rand.Intn(len(option))
generates a random index to select the computer’s choice.
Taking Player Input
fmt.Print("choose an option, rock, paper and scissors: ")
fmt.Scan(&playerChoice)
fmt.Printf("Computer Choice: %v \n", computerChoice)
fmt.Printf("player Choice: %v \n", playerChoice)
-
fmt.Print()
prompts the player to enter a choice. -
fmt.Scan(&playerChoice)
captures the user's input. - The selected choices for both the player and the computer are displayed.
Determining the Winner
if playerChoice == computerChoice {
fmt.Println("no winner(tie)")
fmt.Printf("game score Player %v, computer %v \n", playerScore, computerScore)
If both choices are the same, it results in a tie, and scores remain unchanged.
Player Wins Condition
} else if playerChoice == "rock" && computerChoice == "scissors" ||
playerChoice == "paper" && computerChoice == "rock" ||
playerChoice == "scissors" && computerChoice == "paper" {
fmt.Println("player wins")
playerScore++
The player wins if:
- Rock beats scissors
- Paper beats rock
- Scissors beat paper
The player’s score is incremented accordingly.
Computer Wins Condition
} else {
fmt.Println("computer Wins")
computerScore++
fmt.Printf("game score Player %v, computer %v \n", playerScore, computerScore)
}
If none of the conditions above are met, the computer wins, and its score increases.
Running the Game
Save the code in a file, say main.go
, and run it using:
go run main.go
You can play multiple rounds until you choose to stop the game manually (Ctrl + C).
Fixing an Error in the Code
There's a small typo in the condition checking for the player's win. The condition:
playerChoice == "scissor" && computerChoice == "paper"
should be corrected to:
playerChoice == "scissors" && computerChoice == "paper"
Since "scissors" is the correct string defined in the option
slice, fixing this ensures the program works correctly.
Conclusion
This simple Rock, Paper, Scissors game demonstrates basic Go programming concepts such as loops, conditionals, and user input handling. You can enhance the game by adding features like a limit on rounds, an option to exit, or even integrating a graphical interface.
the full code also on my github here
Try modifying the code and share your improvements! 🚀
Top comments (0)