Beispiel #1
0
        void ProcessCommandBuy(ref State state, Player player, Turn.Command command, bool reflected)
        {
            Unit temp = null;
            int  type = command.arguments[0];
            int  line = command.arguments[1];

            if (line < 0 || line >= Const.NumberOfLines)
            {
                command.comment = "Неверно указана горизонталь";
                return;
            }
            int bas         = reflected ? Const.NumberOfColumns + 1 : 0;
            int destination = reflected ? (Const.NumberOfColumns + 1 - command.arguments[2]) : command.arguments[2];

            if (destination < 1 || destination > Const.NumberOfColumns)
            {
                command.comment = "Неверно указано место назначения";
                return;
            }
            OwnerType owner = player.Owner;

            switch (type)
            {
            case (int)UnitTypes.Armored:
                temp = new Armored(owner, line, bas, destination);
                break;

            case (int)UnitTypes.Cannon:
                temp = new Cannon(owner, line, bas, destination);
                break;

            case (int)UnitTypes.Combine:
                temp = new Combine(owner, line, bas, destination);
                break;

            case (int)UnitTypes.Mine:
                temp = new Mine(owner, line, bas, destination);
                break;

            case (int)UnitTypes.Tank:
                temp = new Tank(owner, line, bas, destination);
                break;

            default:
                command.comment = "Неверный тип юнита";
                return;
            }
            if (player.Money < temp.Cost)
            {
                command.comment = "Недостаточно денег";
                return;
            }
            if (player.Units[line] != null)
            {
                command.comment = "Горизонталь занята";
                return;
            }
            player.Money -= temp.Cost;
            state.AddUnit(bas, line, player, temp);
        }
Beispiel #2
0
        void ProcessCommandStart(ref State state, Player player, Turn.Command command, bool reflected)
        {
            int line = command.arguments[0];
            int bas  = reflected ? Const.NumberOfColumns + 1 : 0;

            if (line < 0 || line >= Const.NumberOfLines)
            {
                command.comment = "Неверно указана горизонталь";
                return;
            }
            int destination = reflected ? (Const.NumberOfColumns + 1 - command.arguments[1]) : command.arguments[1];

            if (destination < 1 || destination > Const.NumberOfColumns)
            {
                command.comment = "Неверно указано место назначения";
                return;
            }
            if (player.Units[line] == null || player.Units[line].State == UnitState.Dead)
            {
                command.comment = "На указанной горизонтали нет юнитов";
                return;
            }
            if (player.Units[line].PosTileX != bas)
            {
                command.comment = "На указанной горизонтали юнит уже в поле";
                return;
            }

            player.Units[line].State       = UnitState.Forward;
            player.Units[line].Destination = destination;
        }
Beispiel #3
0
        void ProcessCommandRemove(ref State state, Player player, Turn.Command command, bool reflected)
        {
            int line = command.arguments[0];
            int bas  = (reflected ? Const.NumberOfColumns + 1 : 0);

            if (line < 0 || line >= Const.NumberOfLines)
            {
                command.comment = "Неверно указана горизонталь";
                return;
            }
            if (player.Units[line] == null)
            {
                command.comment = "На указанной горизонтали нет юнитов";
                return;
            }
            if (player.Units[line].PosTileX != bas)
            {
                command.comment = "Указанный юнит не находится на базе";
                return;
            }
            state.DestroyUnit(player.Units[line]);
        }
Beispiel #4
0
        Turn GetTurnFromProgramExecuter(State state, Player player)
        {
            ExternalProgramExecuter epe = new ExternalProgramExecuter(player.programAddress,
                                                                      "input.txt", "output.txt", _javaPath);
            bool   reflected = player.Owner == OwnerType.Player1 ? false : true;
            Turn   ret       = new Turn();
            string input     = ret.input = GetInput(state, player, reflected ? state.players[0] : state.players[1]);
            string output;
            string comment;

            try
            {
                ExternalProgramExecuteResult res = epe.Execute(input, 2, out output, out comment);


                //пустой файл допускается
                if (res == ExternalProgramExecuteResult.EmptyOutput)
                {
                    output = "";
                    res    = ExternalProgramExecuteResult.Ok;
                }
                ret.TurnStatus = res;

                if (res == ExternalProgramExecuteResult.Ok)
                {
                    ret.rawOutput = output;
                    StringReader reader = new StringReader(output);
                    string       linexxx;   List <string> lines = new List <string>();
                    while (!string.IsNullOrEmpty(linexxx = reader.ReadLine()))
                    {
                        lines.Add(linexxx);
                    }

                    player.Memory = null;
                    if (lines.Count > 0 && lines.Last().Contains("memory"))
                    {
                        ret.memory = lines.Last();
                        lines.RemoveAt(lines.Count - 1);
                        player.Memory = ret.memory.Replace("memory", "");
                    }

                    for (int i = 0; i < lines.Count; i++)
                    {
                        string line    = lines[i];
                        var    command = new Turn.Command();
                        try
                        {
                            string[] temp = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            command.type = (Turn.CommandType)(Convert.ToInt32(temp[0]));
                            for (int j = 1; j < temp.Length; j++)
                            {
                                command.arguments.Add(Convert.ToInt32(temp[j]));
                            }


                            if ((command.type == Turn.CommandType.Buy && command.arguments.Count < 3) ||
                                (command.type == Turn.CommandType.Start && command.arguments.Count < 2) ||
                                (command.type == Turn.CommandType.Remove && command.arguments.Count < 1))
                            {
                                throw new Exception();
                            }

                            //нарушение логики проверяется в Engine

                            //не надо ничего писать в коммент, если команда хорошая
                            // command.comment = "ok";
                        }
                        catch
                        {
                            command.type    = Turn.CommandType.Error;
                            command.comment = "неверный формат";
                        }

                        ret.commands.Add(command);
                    }
                }
            }
            catch (ApplicationException err)
            {
                return(ret);
            }

            return(ret);
        }