fallout.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. var debug = 0
  8. func main() {
  9. if os.Getenv("DEBUG") == "true" {
  10. debug = 1
  11. }
  12. game := NewGame()
  13. err := game.getWordsFromStdin()
  14. if err != nil {
  15. fmt.Printf("Error getting Words from stdin: %s\n", err)
  16. os.Exit(1)
  17. }
  18. simulator := NewSimulator(*game)
  19. _, err = simulator.SimulateAllPossibleGames()
  20. if err != nil {
  21. fmt.Printf("Error simulating all possible games: %s\n", err)
  22. os.Exit(1)
  23. }
  24. for i := 0; i < 3; i++ {
  25. guess, score := getGuessAndScoreFromStdin()
  26. fmt.Printf("Guess: %s, Score: %d\n", guess, score)
  27. game.FilterWords(guess, score)
  28. if len(game.Words) == 0 {
  29. fmt.Printf("\n\nFATAL ERROR: something went wrong! No words are left, this shouldn't happen!!!!!!!\n\n")
  30. os.Exit(1)
  31. } else if len(game.Words) == 1 {
  32. fmt.Printf("Game is down to one word: %+v\n", game.Words)
  33. for word := range game.Words {
  34. fmt.Printf("\n\nThe answer is: %s\n\n", word)
  35. os.Exit(0)
  36. }
  37. }
  38. err = game.scoreWordsByCommonLetterLocations()
  39. if err != nil {
  40. fmt.Printf("Error scoring Words: %s\n", err)
  41. os.Exit(1)
  42. }
  43. _, err = game.printSortedScores()
  44. if err != nil {
  45. fmt.Printf("Error printing sorted scores: %s\n", err)
  46. os.Exit(1)
  47. }
  48. bestGuess, err := game.getBestGuess()
  49. if err != nil {
  50. fmt.Printf("Error getting best guess: %s\n", err)
  51. os.Exit(1)
  52. }
  53. fmt.Printf("Best Guess: %s", bestGuess)
  54. }
  55. }
  56. func getGuessAndScoreFromStdin() (string, int) {
  57. reader := bufio.NewReader(os.Stdin)
  58. fmt.Print("Enter Guess: ")
  59. guess, _ := reader.ReadString('\n')
  60. guess = guess[:len(guess)-1]
  61. fmt.Print("Enter Score: ")
  62. var score int
  63. _, err := fmt.Scanf("%d", &score)
  64. if err != nil {
  65. fmt.Printf("Error reading score: %s\n", err)
  66. os.Exit(1)
  67. }
  68. return guess, score
  69. }
  70. func debugPrint(format string, args ...interface{}) {
  71. if debug == 1 {
  72. fmt.Printf(format, args...)
  73. }
  74. }