123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- )
- var debug = 0
- func main() {
- if os.Getenv("DEBUG") == "true" {
- debug = 1
- }
- words, length, err := getWordsFromStdin()
- if err != nil {
- fmt.Printf("Error getting Words from stdin: %s\n", err)
- os.Exit(1)
- }
- game := NewGame()
- game.Words = words
- game.Length = length
- for i := 0; i < 3; i++ {
- simulator := NewSimulator(*game)
- _, err = simulator.SimulateAllPossibleGames()
- if err != nil {
- fmt.Printf("Error simulating all possible games: %s\n", err)
- os.Exit(1)
- }
- guess, score := getGuessAndScoreFromStdin()
- 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 {
- for word := range game.Words {
- fmt.Printf("\n\nThe answer is: %s\n\n", word)
- os.Exit(0)
- }
- }
- }
- }
- 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)
- }
- fmt.Print("\n")
- return guess, score
- }
- func getWordsFromStdin() (map[string]*Word, int, error) {
- fmt.Println("Enter Words, one per line. Enter a period to end input.")
- var length int
- words := make(map[string]*Word)
- for {
- reader := bufio.NewReader(os.Stdin)
- word, _ := reader.ReadString('\n')
- word = word[:len(word)-1]
- if word == "." {
- fmt.Print("\nOK!\n\n")
- break
- }
- if length == 0 {
- length = len(word)
- } else {
- if len(word) != length {
- fmt.Printf("Error: All words must be the same length (%d), skipping: %s\n", length, word)
- continue
- }
- }
- words[word] = &Word{Word: word}
- }
- return words, length, nil
- }
- func debugPrint(format string, args ...interface{}) {
- if debug == 1 {
- fmt.Printf(format, args...)
- }
- }
|