123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package main
- import (
- "fmt"
- "sort"
- )
- type Game struct {
- Words map[string]*Word
- Length int
- }
- func NewGame() *Game {
- return &Game{
- Words: make(map[string]*Word),
- }
- }
- func (g Game) scoreWordsByCommonLetterLocations() {
- letterIdxScores := make(map[int]map[string]int)
- var lastWord string
- for _, word := range g.Words {
- word.Score = 0
- lastWord = word.Word
- }
- for idx := range lastWord {
- letterIdxScores[idx] = make(map[string]int)
- }
- for _, word := range g.Words {
- for idx, letter := range word.Word {
- letterIdxScores[idx][string(letter)]++
- }
- }
- for _, word := range g.Words {
- for idx, letter := range word.Word {
- word.Score += letterIdxScores[idx][string(letter)]
- }
- }
- return
- }
- func (g Game) FilterWords(guess string, score int) {
- for _, word := range g.Words {
- if !word.MatchesGuess(guess, score) {
- delete(g.Words, word.Word)
- }
- }
- }
- func (g Game) getSortedScores() []string {
- var sortedWordScores []string
- for _, word := range g.Words {
- sortedWordScores = append(sortedWordScores, word.Word)
- }
- debugPrint("Sorted Word Scores: %v\n", sortedWordScores)
- // sort words by score
- sort.Slice(sortedWordScores, func(i, j int) bool {
- if g.Words[sortedWordScores[i]].Score != g.Words[sortedWordScores[j]].Score {
- return g.Words[sortedWordScores[i]].Score > g.Words[sortedWordScores[j]].Score
- }
- // sort by word if scores are equal - this is for consistent answers for unit testing
- return g.Words[sortedWordScores[i]].Word < g.Words[sortedWordScores[j]].Word
- })
- return sortedWordScores
- }
- func (g Game) getBestGuess() string {
- return g.getSortedScores()[0]
- }
- func (g Game) printSortedScores() []string {
- sortedWordScores := g.getSortedScores()
- fmt.Println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
- fmt.Print("Words sorted by Score:\n")
- for _, word := range sortedWordScores {
- fmt.Printf("%s: %d\n", word, g.Words[word].Score)
- }
- fmt.Println("")
- return sortedWordScores
- }
|