Skip to content

Commit

Permalink
Histogram fix
Browse files Browse the repository at this point in the history
When printing the histogram of victories by number of guesses it took, the length of the count is now taken into consideration so all the counts can be padded so the bars still line up.
  • Loading branch information
coreyog committed Mar 29, 2022
1 parent e9c9850 commit 62da3ef
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,18 +567,28 @@ func (gs *GameStats) print(win *bool) {
hist := make([]float64, TotalGuesses)
max := float64(-1)

winPadding := 0

for i := 0; i < TotalGuesses; i++ {
hist[i] = float64(wins[i]) / float64(totalWins)
if max < hist[i] {
max = hist[i]
}

winWord := strconv.Itoa(wins[i])

if len(winWord) > winPadding {
winPadding = len(winWord)
}
}

mult := MaxHistogramBarLength / max

// histogram
for i := 0; i < TotalGuesses; i++ {
fmt.Printf("%d: %d %s\n", i+1, wins[i], strings.Repeat("█", int(math.Min(MaxHistogramBarLength, hist[i]*mult))))
count := strconv.Itoa(wins[i])
count = strings.Repeat(" ", winPadding-len(count)) + count
fmt.Printf("%d: %s %s\n", i+1, count, strings.Repeat("█", int(math.Min(MaxHistogramBarLength, hist[i]*mult))))
}

if gs.ExperimentalEmojiSupport && win != nil {
Expand Down

0 comments on commit 62da3ef

Please sign in to comment.