fallout.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. game := NewGame()
  9. err := game.getWordsFromStdin()
  10. if err != nil {
  11. fmt.Printf("Error getting Words from stdin: %s\n", err)
  12. os.Exit(1)
  13. }
  14. err = game.scoreWordsByCommonLetterLocations()
  15. if err != nil {
  16. fmt.Printf("Error scoring Words: %s\n", err)
  17. os.Exit(1)
  18. }
  19. _, err = game.printSortedScores()
  20. if err != nil {
  21. fmt.Printf("Error printing sorted scores: %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. if len(game.Words) == 1 {
  39. for word := range game.Words {
  40. fmt.Printf("The word is: %s\n", word)
  41. os.Exit(0)
  42. }
  43. }
  44. }
  45. }
  46. func getGuessAndScoreFromStdin() (string, int) {
  47. reader := bufio.NewReader(os.Stdin)
  48. fmt.Print("Enter Guess: ")
  49. guess, _ := reader.ReadString('\n')
  50. guess = guess[:len(guess)-1]
  51. fmt.Print("Enter Score: ")
  52. var score int
  53. _, err := fmt.Scanf("%d", &score)
  54. if err != nil {
  55. fmt.Printf("Error reading score: %s\n", err)
  56. os.Exit(1)
  57. }
  58. return guess, score
  59. }