Exemplo n.º 1
0
        public SessionInformation Command(string command)
        {
            var commandDetail = CommandResolver.Resolve(command);

            if (commandDetail.CommandType == CommandType.Draw)
            {
                DrawTable();
            }

            if (commandDetail.CommandType == CommandType.Undo)
            {
                if (Checkmate)
                {
                    WriteError("Checkmate !!!");
                }

                if (Checkmate == false && MovementInstructions.Keys.Count != 0)
                {
                    var lastInstraction = GetLastMovement();
                    var lastResult      = MovementInstructions[lastInstraction];

                    if (lastResult.Eated != null)
                    {
                        CurrentPlayer.Stones.Add(lastResult.Eated);
                        PreviousPlayer.Eats.Remove(lastResult.Eated);
                    }

                    lastInstraction.MovingStone.ForceMove(lastInstraction.FromLocation);

                    SetPlayerReturn();

                    MovementInstructions.Remove(lastInstraction);
                    DrawTable();

                    SessionTimer.Restart();
                }
            }

            if (commandDetail.CommandType == CommandType.Stat)
            {
                DrawStatistics();
            }

            if (commandDetail.CommandType == CommandType.Play && Checkmate == false)
            {
                if (commandDetail.IsCorrect == false)
                {
                    WriteError(commandDetail.ReturnMessage);

                    MovementInstructions.Add(default, new MovementResult(false, null, null, null, commandDetail.ReturnMessage));
Exemplo n.º 2
0
        public virtual void Start()
        {
            Logger?.SetFileName(_logFileName);
            CurrentPlayer = Players[CurrentIndexer];
            NextPlayer    = Players[CurrentIndexer == 0 ? 1 : 0];

            DrawTable();

            string command = string.Empty;

            if (StartingCommands.Count != 0)
            {
                command = StartingCommands[0];
            }



            return;

            while (command != "quit" && command != "exit")
            {
                if (string.IsNullOrEmpty(command))
                {
                    ShowInfo();
                }
                else
                {
                    string[] commandArr = command.Split(' ');
                    if (commandArr.Length == 2 && commandArr[0].Length == 2 && commandArr[1].Length == 2)
                    {
                        command = $"play {command}";
                    }

                    if (command == "draw")
                    {
                        DrawTable();
                        if (Checkmate)
                        {
                            WriteError("Checkmate !!!");
                        }
                    }

                    if (command == "undo")
                    {
                        if (Checkmate)
                        {
                            WriteError("Checkmate !!!");
                        }

                        if (Checkmate == false && MovementInstructions.Keys.Count != 0)
                        {
                            var lastInstraction = GetLastMovement();
                            var lastResult      = MovementInstructions[lastInstraction];

                            if (lastResult.Eated != null)
                            {
                                CurrentPlayer.Stones.Add(lastResult.Eated);
                                PreviousPlayer.Eats.Remove(lastResult.Eated);
                            }

                            lastInstraction.MovingStone.ForceMove(lastInstraction.FromLocation);

                            SetPlayerReturn();

                            MovementInstructions.Remove(lastInstraction);
                            DrawTable();

                            SessionTimer.Restart();
                        }
                    }

                    if (Checkmate == false && command.StartsWith("play"))
                    {
                        var commandDetail = CommandResolver.Resolve(command);
                        if (commandDetail.IsCorrect == false)
                        {
                            WriteError(commandDetail.ReturnMessage);
                            command = WaitCommand();
                            continue;
                        }

                        var fromLocation = Table.GetLocation(commandDetail.From_X, commandDetail.From_Y);

                        var stone          = Table.Stones.GetFromLocation(fromLocation);
                        var targetLocation = Table.GetLocation(commandDetail.To_X, commandDetail.To_Y);

                        if (targetLocation == null)
                        {
                            WriteError("Location is not found!");
                            command = WaitCommand();
                            continue;
                        }

                        if (stone == null)
                        {
                            WriteError("Stone is not found at given location!");
                            command = WaitCommand();
                            continue;
                        }

                        var instraction = Instruction.CreateOne(stone, targetLocation, this, command);

                        var result = instraction.TryDo();
                        if (result.IsOK)
                        {
                            MovementInstructions.Add(instraction, result);

                            if (result.Eated != null)
                            {
                                CurrentPlayer.Eat(result.Eated);
                                Table.Stones.Remove(result.Eated);
                            }

                            if (result.CheckRemoved)
                            {
                                Check = false;
                            }

                            WriteMessage(result.Message);
                            IsCheck(stone);

                            SessionTimer.Stop();
                            CurrentPlayer.Duration += SessionTimer.ElapsedMilliseconds;

                            if (Checkmate)
                            {
                                DrawTable();
                                WriteEmpty();
                                WriteError("Checkmate !!!");
                            }
                            else
                            {
                                SetPlayerReturn();
                                DrawTable();

                                SessionTimer.Restart();
                            }
                        }
                        else
                        {
                            WriteError(result.Message);
                        }
                    }

                    if (command == "stat")
                    {
                        DrawStatistics();
                    }
                }

                if (StartingCommands.Count == 0)
                {
                    var lastMovementInstaction = GetLastMovement();
                    if (lastMovementInstaction != null)
                    {
                        WriteLastCommand(lastMovementInstaction.Log);
                    }

                    command = WaitCommand();
                }
                else
                {
                    var current = StartingCommands.First();
                    StartingCommands.Remove(current);
                    if (StartingCommands.Count != 0)
                    {
                        command = StartingCommands[0];
                    }
                    else
                    {
                        var lastMovementInstaction = GetLastMovement();
                        if (lastMovementInstaction != null)
                        {
                            WriteLastCommand(lastMovementInstaction.Log);
                        }
                        command = WaitCommand();
                    }
                }
            }

            Logger?.SaveInstructions(MovementInstructions.Where(m => m.Value.IsOK).Select(m => m.Key));
        }