Skip to content

Commit 2d0e451

Browse files
Merge pull request #11 from chrisreddington/extend-rockpaperscissors
Extend rockpaperscissors
2 parents 6885609 + c7beec2 commit 2d0e451

3 files changed

Lines changed: 151 additions & 26 deletions

File tree

cmd/rockpaperscissors.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The game logic is in the internal/rockpaperscissors package.
33

44
// This file sets up the command line interface for the game. It should call the PlayGame function from the rockpaperscissors package.
5-
5+
// Package cmd contains all the commands for the CLI application.
66
package cmd
77

88
import (
@@ -13,6 +13,8 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16+
var secretMode bool
17+
1618
// rootCmd represents the base command when called without any subcommands
1719
var rockPaperScissorsCmd = &cobra.Command{
1820
Use: "rockpaperscissors",
@@ -21,6 +23,11 @@ var rockPaperScissorsCmd = &cobra.Command{
2123
You can choose from rock, paper, or scissors. The computer will randomly choose its move and the winner will be determined based on the rules of the game.`,
2224
Run: func(cmd *cobra.Command, args []string) {
2325
input := userPrompt.New(os.Stdin, os.Stdout, os.Stderr)
24-
rockpaperscissors.PlayGame(input)
26+
rockpaperscissors.PlayGame(input, secretMode)
2527
},
2628
}
29+
30+
func init() {
31+
rockPaperScissorsCmd.Flags().BoolVar(&secretMode, "spock", false, "Enable secret game mode")
32+
rootCmd.AddCommand(rockPaperScissorsCmd)
33+
}

internal/rockpaperscissors/rockpaperscissors.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99

1010
// Game options
1111
var (
12-
options = []string{"rock", "paper", "scissors", "exit"}
12+
standardOptions = []string{"rock", "paper", "scissors", "exit"}
13+
secretOptions = []string{"rock", "paper", "scissors", "lizard", "spock", "exit"}
1314
)
1415

1516
// Game represents a single game of Rock Paper Scissors.
@@ -32,14 +33,16 @@ type Game struct {
3233
GameOverMessage string
3334
// GamesPlayed tracks the number of games played
3435
GamesPlayed int
36+
// SecretMode indicates if the game is in secret mode
37+
SecretMode bool
3538
}
3639

3740
// Prompter defines an interface for getting user input
3841
type Prompter interface {
3942
Select(prompt, defaultValue string, options []string) (int, error)
4043
}
4144

42-
func NewGame(bestOf int) *Game {
45+
func NewGame(bestOf int, secretMode bool) *Game {
4346
if bestOf%2 == 0 {
4447
bestOf++ // Ensure we have an odd number for "best of"
4548
}
@@ -53,6 +56,7 @@ func NewGame(bestOf int) *Game {
5356
BestOf: bestOf,
5457
GameOver: false,
5558
GameOverMessage: "",
59+
SecretMode: secretMode,
5660
}
5761
}
5862

@@ -78,6 +82,10 @@ func (g *Game) Play(playerChoice string) {
7882
// getComputerChoice returns the choice made by the computer.
7983
func (g *Game) getComputerChoice() string {
8084
rand.Seed(time.Now().UnixNano())
85+
options := standardOptions
86+
if g.SecretMode {
87+
options = secretOptions
88+
}
8189
// Only use the game options excluding "exit"
8290
choices := options[:len(options)-1]
8391
return choices[rand.Intn(len(choices))]
@@ -88,10 +96,19 @@ func (g *Game) getWinner() string {
8896
if g.PlayerChoice == g.ComputerChoice {
8997
return "draw"
9098
}
91-
if (g.PlayerChoice == "rock" && g.ComputerChoice == "scissors") ||
92-
(g.PlayerChoice == "paper" && g.ComputerChoice == "rock") ||
93-
(g.PlayerChoice == "scissors" && g.ComputerChoice == "paper") {
94-
return "player"
99+
100+
winningMoves := map[string][]string{
101+
"rock": {"scissors", "lizard"},
102+
"paper": {"rock", "spock"},
103+
"scissors": {"paper", "lizard"},
104+
"lizard": {"paper", "spock"},
105+
"spock": {"rock", "scissors"},
106+
}
107+
108+
for _, beatenChoice := range winningMoves[g.PlayerChoice] {
109+
if g.ComputerChoice == beatenChoice {
110+
return "player"
111+
}
95112
}
96113
return "computer"
97114
}
@@ -143,7 +160,7 @@ func (g *Game) getRoundResultMessage() string {
143160
}
144161

145162
// PlayGame plays a game of Rock Paper Scissors.
146-
func PlayGame(prompter Prompter) {
163+
func PlayGame(prompter Prompter, secretMode bool) {
147164
// Get the number of rounds from the user
148165
roundOptions := []string{"3", "5", "7", "9"}
149166
roundIndex, err := prompter.Select("How many rounds would you like to play (best of)?", "3", roundOptions)
@@ -156,12 +173,20 @@ func PlayGame(prompter Prompter) {
156173
bestOf = parseInt(roundOptions[roundIndex])
157174
}
158175

159-
game := NewGame(bestOf)
176+
game := NewGame(bestOf, secretMode)
160177
fmt.Printf("Playing best of %d games\n", bestOf)
178+
if secretMode {
179+
fmt.Println("🖖 Secret mode activated: Rock Paper Scissors Lizard Spock!")
180+
}
161181

162182
for !game.GameOver {
163183
fmt.Printf("\nCurrent score - Player: %d, Computer: %d\n", game.PlayerScore, game.ComputerScore)
164-
184+
185+
options := standardOptions
186+
if secretMode {
187+
options = secretOptions
188+
}
189+
165190
// Get player choice using prompter
166191
playerChoiceIndex, err := prompter.Select("Choose your move", "rock", options)
167192
if err != nil {

0 commit comments

Comments
 (0)