fallout.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. words, length, err := getWordsFromStdin()
  13. if err != nil {
  14. fmt.Printf("Error getting Words from stdin: %s\n", err)
  15. os.Exit(1)
  16. }
  17. game := NewGame()
  18. game.Words = words
  19. game.Length = length
  20. for i := 0; i < 3; i++ {
  21. simulator := NewSimulator(*game)
  22. _, err = simulator.SimulateAllPossibleGames()
  23. if err != nil {
  24. fmt.Printf("Error simulating all possible games: %s\n", err)
  25. os.Exit(1)
  26. }
  27. guess, score := getGuessAndScoreFromStdin()
  28. game.FilterWords(guess, score)
  29. if len(game.Words) == 0 {
  30. fmt.Printf("\n\nFATAL ERROR: something went wrong! No words are left, this shouldn't happen!!!!!!!\n\n")
  31. os.Exit(1)
  32. } else if len(game.Words) == 1 {
  33. for word := range game.Words {
  34. fmt.Printf("\n\nThe answer is: %s\n\n", word)
  35. os.Exit(0)
  36. }
  37. }
  38. }
  39. }
  40. func getGuessAndScoreFromStdin() (string, int) {
  41. reader := bufio.NewReader(os.Stdin)
  42. fmt.Print("Enter Guess: ")
  43. guess, _ := reader.ReadString('\n')
  44. guess = guess[:len(guess)-1]
  45. fmt.Print("Enter Score: ")
  46. var score int
  47. _, err := fmt.Scanf("%d", &score)
  48. if err != nil {
  49. fmt.Printf("Error reading score: %s\n", err)
  50. os.Exit(1)
  51. }
  52. fmt.Print("\n")
  53. return guess, score
  54. }
  55. func getWordsFromStdin() (map[string]*Word, int, error) {
  56. fmt.Println("Enter Words, one per line. Enter a period to end input.")
  57. var length int
  58. words := make(map[string]*Word)
  59. for {
  60. reader := bufio.NewReader(os.Stdin)
  61. word, _ := reader.ReadString('\n')
  62. word = word[:len(word)-1]
  63. if word == "." {
  64. fmt.Print("\nOK!\n\n")
  65. break
  66. }
  67. if length == 0 {
  68. length = len(word)
  69. } else {
  70. if len(word) != length {
  71. fmt.Printf("Error: All words must be the same length (%d), skipping: %s\n", length, word)
  72. continue
  73. }
  74. }
  75. words[word] = &Word{Word: word}
  76. }
  77. return words, length, nil
  78. }
  79. func debugPrint(format string, args ...interface{}) {
  80. if debug == 1 {
  81. fmt.Printf(format, args...)
  82. }
  83. }