123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- )
- var debug = 0
- func main() {
- if os.Getenv("DEBUG") == "true" {
- debug = 1
- }
- game := NewGame()
- err := game.getWordsFromStdin()
- if err != nil {
- fmt.Printf("Error getting Words from stdin: %s\n", err)
- os.Exit(1)
- }
- simulator := NewSimulator(*game)
- _, err = simulator.SimulateAllPossibleGames()
- if err != nil {
- fmt.Printf("Error simulating all possible games: %s\n", err)
- os.Exit(1)
- }
- for i := 0; i < 3; i++ {
- guess, score := getGuessAndScoreFromStdin()
- fmt.Printf("Guess: %s, Score: %d\n", guess, score)
- game.FilterWords(guess, score)
- if len(game.Words) == 0 {
- fmt.Printf("\n\nFATAL ERROR: something went wrong! No words are left, this shouldn't happen!!!!!!!\n\n")
- os.Exit(1)
- } else if len(game.Words) == 1 {
- fmt.Printf("Game is down to one word: %+v\n", game.Words)
- for word := range game.Words {
- fmt.Printf("\n\nThe answer is: %s\n\n", word)
- os.Exit(0)
- }
- }
- err = game.scoreWordsByCommonLetterLocations()
- if err != nil {
- fmt.Printf("Error scoring Words: %s\n", err)
- os.Exit(1)
- }
- _, err = game.printSortedScores()
- if err != nil {
- fmt.Printf("Error printing sorted scores: %s\n", err)
- os.Exit(1)
- }
- bestGuess, err := game.getBestGuess()
- if err != nil {
- fmt.Printf("Error getting best guess: %s\n", err)
- os.Exit(1)
- }
- fmt.Printf("Best Guess: %s", bestGuess)
- }
- }
- func getGuessAndScoreFromStdin() (string, int) {
- reader := bufio.NewReader(os.Stdin)
- fmt.Print("Enter Guess: ")
- guess, _ := reader.ReadString('\n')
- guess = guess[:len(guess)-1]
- fmt.Print("Enter Score: ")
- var score int
- _, err := fmt.Scanf("%d", &score)
- if err != nil {
- fmt.Printf("Error reading score: %s\n", err)
- os.Exit(1)
- }
- return guess, score
- }
- func debugPrint(format string, args ...interface{}) {
- if debug == 1 {
- fmt.Printf(format, args...)
- }
- }
|