fallout.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. err = game.scoreWordsByCommonLetterLocations()
  29. if err != nil {
  30. fmt.Printf("Error scoring Words: %s\n", err)
  31. os.Exit(1)
  32. }
  33. _, err = game.printSortedScores()
  34. if err != nil {
  35. fmt.Printf("Error printing sorted scores: %s\n", err)
  36. os.Exit(1)
  37. }
  38. bestGuess, err := game.getBestGuess()
  39. if err != nil {
  40. fmt.Printf("Error getting best guess: %s\n", err)
  41. os.Exit(1)
  42. }
  43. fmt.Printf("Best Guess: %s", bestGuess)
  44. if len(game.Words) == 1 {
  45. for word := range game.Words {
  46. fmt.Printf("The word is: %s\n", word)
  47. os.Exit(0)
  48. }
  49. }
  50. }
  51. }
  52. func getGuessAndScoreFromStdin() (string, int) {
  53. reader := bufio.NewReader(os.Stdin)
  54. fmt.Print("Enter Guess: ")
  55. guess, _ := reader.ReadString('\n')
  56. guess = guess[:len(guess)-1]
  57. fmt.Print("Enter Score: ")
  58. var score int
  59. _, err := fmt.Scanf("%d", &score)
  60. if err != nil {
  61. fmt.Printf("Error reading score: %s\n", err)
  62. os.Exit(1)
  63. }
  64. return guess, score
  65. }
  66. func debugPrint(format string, args ...interface{}) {
  67. if debug == 1 {
  68. fmt.Printf(format, args...)
  69. }
  70. }