Пример #1
0
        static async Task Main(string[] args)
        {
            try
            {
                var hangmanServer = HangmanServer();

                // NOTE - Call RegisterUserOnServerAction just once,  and then remember the value returned for x-access-token for next time
                var registerUserAction = new RegisterUserOnServerAction(hangmanServer);
                var x_access_token     = await registerUserAction.Execute(username : "******", password : "******");

                Login(hangmanServer, x_access_token);

                var newGameAction = new StartNewGameOnServerAction(hangmanServer);
                var newGame       = await newGameAction.Execute();

                var allGamesQuery = new AllGamesOnServerQuery(hangmanServer);
                var allGames      = await allGamesQuery.Evalulate();

                var currentGameQuery = new CurrentGameOnServerQuery(hangmanServer);
                var game             = await currentGameQuery.Evalulate();

                var specificGameQuery = new SpecificGameOnServerQuery(hangmanServer);
                var specificGame      = await specificGameQuery.Evalulate(allGames.First().id);

                var makeMoveAction = new PlayMoveOnServerAction(hangmanServer);
                game = await makeMoveAction.Execute('I');
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return;
            }
        }
Пример #2
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)}");
            }
        }