示例#1
0
        public void Run()
        {
            var  game = new Game();
            bool keepPlaying;

            BoardRenderer.PrintBoard(game);

            do
            {
                var turn = game.GetTurn();
                var cmd  = GetCommand(turn);

                keepPlaying = ExecuteCommand(game, cmd);

                Console.WriteLine();
            } while (keepPlaying);
        }
示例#2
0
        private static void PerformMove(Game game, string move)
        {
            var errorCode = game.Move(move);

            if (errorCode != 0)
            {
                var message = GetErrorMessage(errorCode);
                Console.WriteLine($"{message}, please try again");
            }

            BoardRenderer.PrintBoard(game);
            if (game.AttackState != AttackState.None)
            {
                Console.Write(EnumHelper.GetAttackString(game.AttackState));
                Console.WriteLine("!");
            }
        }
示例#3
0
        public async Task Run()
        {
            var pgn = await GetPGN(_file);

            var game = new Game();

            if (pgn.Moves.Any())
            {
                BoardRenderer.PrintBoard(game);

                foreach (var move in pgn.Moves)
                {
                    await Task.Delay(_moveDelay);

                    game.Move(move);
                    BoardRenderer.PrintBoard(game);
                }
            }
        }
示例#4
0
        private static bool ExecuteCommand(Game game, InteractiveGameCommand cmd)
        {
            switch (cmd.CommandName)
            {
            case InteractiveGameCommands.Hint:
            {
                if (string.IsNullOrEmpty(cmd.CommandArgs))
                {
                    Console.WriteLine("Please enter a square (ex: 'hint a4')");
                    BoardRenderer.PrintBoard(game);
                }
                else
                {
                    var sq    = Game.ParseSquare(cmd.CommandArgs);
                    var moves = game.GetValidMoves(sq.File, sq.Rank);
                    if (moves.Count == 0)
                    {
                        Console.WriteLine("No valid moves for that square");
                    }

                    BoardRenderer.PrintBoard(game, moves);
                }

                return(true);
            }

            case InteractiveGameCommands.Exit:
            {
                Console.Write("Are you sure (Y\\N) ?: ");
                return((Console.ReadLine() ?? string.Empty).ToLower() != "y");
            }

            case InteractiveGameCommands.Help:
            {
                Console.WriteLine();
                Console.WriteLine("List of commands:");
                foreach (var kvp in InteractiveGameCommands.Descriptions)
                {
                    Console.WriteLine($"- {kvp.Key}: {kvp.Value}");
                }

                Console.WriteLine("Not entering a command is assumed to be a move");
                BoardRenderer.PrintBoard(game);

                return(true);
            }

            case InteractiveGameCommands.Move:
            {
                PerformMove(game, cmd.CommandArgs);
                return(game.IsOngoing);
            }

            case InteractiveGameCommands.Undo:
            {
                var success = game.Undo();
                if (!success)
                {
                    Console.WriteLine("Can't undo from this state");
                }

                BoardRenderer.PrintBoard(game);

                return(true);
            }

            default:
            {
                // By default, treat no command as a move
                PerformMove(game, cmd.TotalInput);
                return(true);
            }
            }
        }