Skip to content

Commit a91e752

Browse files
Refactor game over and round result messages for clarity and conciseness
1 parent 596fb9c commit a91e752

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

internal/rockpaperscissors/rockpaperscissors.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rockpaperscissors
33
import (
44
"fmt"
55
"math/rand"
6+
"strings"
67
"time"
78
)
89

@@ -119,11 +120,26 @@ func (g *Game) isGameOver() bool {
119120
// getGameOverMessage returns the message to display when the game is over.
120121
func (g *Game) getGameOverMessage() string {
121122
if g.PlayerScore > g.ComputerScore {
122-
return fmt.Sprintf("You win the match! Final score: Player %d - Computer %d", g.PlayerScore, g.ComputerScore)
123+
return fmt.Sprintf("GAME OVER: Player WINS (%d - %d)", g.PlayerScore, g.ComputerScore)
123124
} else if g.ComputerScore > g.PlayerScore {
124-
return fmt.Sprintf("Computer wins the match! Final score: Player %d - Computer %d", g.PlayerScore, g.ComputerScore)
125+
return fmt.Sprintf("GAME OVER: Player LOSES (%d - %d)", g.PlayerScore, g.ComputerScore)
126+
}
127+
return fmt.Sprintf("GAME OVER: DRAW (%d - %d)", g.PlayerScore, g.ComputerScore)
128+
}
129+
130+
// getRoundResultMessage returns a concise message about the round result
131+
func (g *Game) getRoundResultMessage() string {
132+
// Capitalize the first letter of choices for better display
133+
playerChoice := strings.Title(g.PlayerChoice)
134+
computerChoice := strings.Title(g.ComputerChoice)
135+
136+
if g.Winner == "draw" {
137+
return fmt.Sprintf("Draw! Player (%s) - CPU (%s)", playerChoice, computerChoice)
138+
} else if g.Winner == "player" {
139+
return fmt.Sprintf("Player (%s) beats CPU (%s)", playerChoice, computerChoice)
140+
} else {
141+
return fmt.Sprintf("Player (%s) loses to CPU (%s)", playerChoice, computerChoice)
125142
}
126-
return fmt.Sprintf("The match is a draw! Final score: Player %d - Computer %d", g.PlayerScore, g.ComputerScore)
127143
}
128144

129145
// PlayGame plays a game of Rock Paper Scissors.
@@ -159,9 +175,8 @@ func PlayGame(prompter Prompter) {
159175
break
160176
}
161177

162-
fmt.Printf("You chose: %s\n", game.PlayerChoice)
163-
fmt.Printf("Computer chose: %s\n", game.ComputerChoice)
164-
fmt.Printf("Round result: %s wins!\n", game.Winner)
178+
// Display a more concise round result
179+
fmt.Println(game.getRoundResultMessage())
165180
}
166181
fmt.Println(game.GameOverMessage)
167182
}

0 commit comments

Comments
 (0)