Skip to content

Commit

Permalink
Cleanup how stats are printed, better ctrl+c behavior around dailies
Browse files Browse the repository at this point in the history
Printing stats was a mess. The code was duplicated between Hard and Normal mode, there were differences that shouldn't have been different, trimming the trailing decimal was converting "100.0" to "1". All streamlined and fixed.

If you started a daily puzzle and immediately quit, you shouldn't be punished by having the puzzle spoiled for you and being unable to play the puzzle on the next play. Quitting on the first guess won't reveal the word or reset your streak.

Cleaner labels and new lines for legibility.
  • Loading branch information
coreyog committed Jan 20, 2022
1 parent 8fd97b7 commit 13fb5de
Showing 1 changed file with 47 additions and 68 deletions.
115 changes: 47 additions & 68 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func main() {
initKeyboard()

if args.HardMode {
fmt.Println("Hard mode enabled. Guesses must use all revealed (green) hints.")
fmt.Println(" Hard Mode")
}

// prepare key listener
Expand Down Expand Up @@ -195,12 +195,12 @@ func main() {

stat.Finish()

if !win {
if !win && currentGuess != 0 {
gamestats.Streak = 0
_ = gamestats.save()
}

fmt.Printf("\nThe word was %s\n", word)
fmt.Printf("\nThe word was %s\n", word)
}

os.Exit(0)
}()
Expand Down Expand Up @@ -314,7 +314,7 @@ func main() {
fmt.Print("You win!\n\n")
} else {
gamestats.Streak = 0
fmt.Printf("The word was %s\n", word)
fmt.Printf("\nThe word was %s\n\n", word)
}

_ = gamestats.save()
Expand Down Expand Up @@ -525,95 +525,74 @@ func (gs *GameStats) save() error {
}

func (gs *GameStats) print(win *bool) {
// setup for easy mode
wins := gs.Wins
totalGames := gs.TotalGames
hardInd := ""

fmt.Print("Game Stats")

if args.HardMode {
fmt.Print(" (Hard Mode)")
}

fmt.Println()
wins = gs.HardWins
totalGames = gs.TotalHardGames

if args.HardMode {
totalWins := 0
for i := 0; i < TotalGuesses; i++ {
totalWins += gs.HardWins[i]
}
hardInd = "*"
}

fmt.Printf(" Total Games: %d\n", gs.TotalHardGames)
fmt.Print("\n\n")

if gs.TotalHardGames > 0 {
rawPercent := float64(totalWins*10000/gs.TotalHardGames) / 100
strPercent := strconv.FormatFloat(rawPercent, 'f', 1, 64)
strPercent = strings.TrimRight(strPercent, ".0")
fmt.Printf(" Win %%: %s\n", strPercent)
} else {
fmt.Println(" Win %%: 0")
}
totalWins := 0
for i := 0; i < TotalGuesses; i++ {
totalWins += wins[i]
}

fmt.Printf("Current Streak: %d\n", gs.Streak)
fmt.Printf(" Best Streak: %d\n", gs.BestStreak)
fmt.Println()
fmt.Println("Guess Distribution:")
fmt.Printf(" Total Games: %d\n", totalGames)

for i := 0; i < TotalGuesses; i++ {
fmt.Printf("%d: %d\n", i+1, gs.HardWins[i])
}
if totalGames > 0 {
rawPercent := float64(totalWins*10000) / float64(totalGames) / 100
strPercent := strconv.FormatFloat(rawPercent, 'f', 1, 64)
strPercent = strings.TrimRight(strPercent, "0")
strPercent = strings.TrimRight(strPercent, ".")
fmt.Printf(" Win %%: %s\n", strPercent)
} else {
totalWins := 0
for i := 0; i < TotalGuesses; i++ {
totalWins += gs.Wins[i]
}

fmt.Printf(" Total Games: %d\n", gs.TotalGames)

if gs.TotalGames > 0 {
rawPercent := float64(totalWins*10000/gs.TotalGames) / 100
strPercent := strconv.FormatFloat(rawPercent, 'f', 1, 64)
strPercent = strings.TrimRight(strPercent, ".0")
fmt.Printf(" Win %%: %s\n", strPercent)
} else {
fmt.Println(" Win %%: 0")
}
fmt.Println(" Win %%: 0")
}

fmt.Printf("Current Streak: %d\n", gs.Streak)
fmt.Printf(" Best Streak: %d\n", gs.BestStreak)
fmt.Println()
fmt.Println("Guess Distribution:")
fmt.Printf("Current Streak: %d\n", gs.Streak)
fmt.Printf(" Best Streak: %d\n", gs.BestStreak)
fmt.Println()
fmt.Print("Guess Distribution:\n\n")

// prepare histogram
hist := make([]float64, TotalGuesses)
max := float64(-1)
// prepare histogram
hist := make([]float64, TotalGuesses)
max := float64(-1)

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

mult := MaxHistogramBarLength / max
mult := MaxHistogramBarLength / max

// histogram
for i := 0; i < TotalGuesses; i++ {
fmt.Printf("%d: %d %s\n", i+1, gs.Wins[i], strings.Repeat("█", int(math.Min(MaxHistogramBarLength, hist[i]*mult))))
}
// 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))))
}

if gs.ExperimentalEmojiSupport && win != nil {
fmt.Println()

hardInd := ""
if args.HardMode {
hardInd = "*"
}
turn := "X"

if *win {
fmt.Printf("Wordle %d %d/6%s\n", dayOffset, currentGuess+1, hardInd)
} else {
fmt.Printf("Wordle %d X/6%s\n", dayOffset, hardInd)
turn = strconv.Itoa(currentGuess + 1)
}

fmt.Println()
fmt.Printf("Wordle %d %s/6%s\n\n", dayOffset, turn, hardInd)

for _, line := range emojiStack {
fmt.Println(line)
Expand Down

0 comments on commit 13fb5de

Please sign in to comment.