Beispiel #1
0
        private static async Task PlayGame(PlayMoveOnServerAction makeMoveAction, FilterList filter, StartNewGameOnServerAction newGameAction)
        {
            var words = AllWords();
            var game  = await newGameAction.Execute();

            var wordLength = game.progress.Length;

            Console.WriteLine($"The word has {wordLength} letters.");
            words = filter.FilterByLength(words, wordLength);

            while (!game.complete)
            {
                var suggestion = filter.SuggestLetterIgnoringDuplicates(words, game.lettersGuessed.Select(d => d[0]).ToList());
                Console.Write($"Guessing {suggestion} - Possible words {words.Count()}");

                var previousMissesCount = game.misses;
                game = await makeMoveAction.Execute(suggestion);

                var newMissedCounts = game.misses;

                if (newMissedCounts > previousMissesCount)
                {
                    words = filter.RemoveWordsWhichDoNotContain(words, suggestion);
                    Console.WriteLine($" - Wrong");
                }
                else
                {
                    Console.WriteLine($" - Correct -  {ConvertToWord(game.progress)}");

                    for (int i = 0; i < game.progress.Length; i++)
                    {
                        if (!string.IsNullOrWhiteSpace(game.progress[i]) && game.progress[i][0] == suggestion)
                        {
                            words = filter.RemoveWordsWithOutALetterInKnownPosition(words, i, suggestion);
                        }
                    }
                }
            }

            if (DidWeWin(game))
            {
                var finalWord = ConvertToWord(game.progress);
                Console.WriteLine($"Well done - we won in {game.misses + game.progress.Count()} guesses.  The word was {finalWord}");

                var definitionQuery = new DefinitionFromOxfordDictionaryQuery();
                var meaning         = await definitionQuery.Evaluate(finalWord);

                if (!string.IsNullOrWhiteSpace(meaning))
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Word means " + meaning);
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
            else
            {
                Console.WriteLine($"Sorry,  but we lost :-(.  {ConvertToWord(game.progress)}");
            }
        }