Exemplo n.º 1
0
        private void CreateAllPossibleMoves()
        {
            if (allPossible != null)
            {
                return;
            }
            var all = new List <AgentCommand>();

            Direction[] directions = new Direction[4] {
                Direction.N, Direction.E, Direction.S, Direction.W
            };
            foreach (Direction d in directions)
            {
                all.Add(AgentCommand.CreateMove(d));
                foreach (Direction d2 in directions)
                {
                    if (d == d2)
                    {
                        all.Add(AgentCommand.CreatePush(d, d2));
                    }
                    else if (d2.Opposite() == d)
                    {
                        all.Add(AgentCommand.CreatePull(d, d2));
                    }
                    else
                    {
                        all.Add(AgentCommand.CreatePull(d, d2));
                        all.Add(AgentCommand.CreatePush(d, d2));
                    }
                }
            }
            allPossible = all;
        }
Exemplo n.º 2
0
 internal static List <AgentCommand> RightHandBoxSwimming(Direction d)
 {
     return(new List <AgentCommand>()
     {
         AgentCommand.CreateMove(Opposite(d)),
         AgentCommand.CreatePull(d, Clockwise(d)),
         AgentCommand.CreatePush(Clockwise(d), Opposite(d)),
         AgentCommand.CreatePull(CounterClockwise(d), d),
         AgentCommand.CreatePush(d, Clockwise(d))
     });
 }
Exemplo n.º 3
0
        private void MoveToLocation(List <Point> path, List <AgentCommand> commands, int agentIndex)
        {
            for (int i = 0; i < path.Count - 1; ++i)
            {
                Point agentPos     = path[i];
                Point nextAgentPos = path[i + 1];

                Direction agentDir = PointsToDirection(agentPos, nextAgentPos);

                commands.Add(AgentCommand.CreateMove(agentDir));

                MoveAgent(nextAgentPos, agentIndex);
            }
        }
Exemplo n.º 4
0
        public void SendCommandsSequentially(List <AgentCommands> commands, Level level)
        {
            string[] output = new string[level.AgentCount];

            foreach (AgentCommands agentCommands in commands)
            {
                foreach (var command in agentCommands.Commands)
                {
                    Array.Fill(output, AgentCommand.NoOp());
                    output[agentCommands.AgentIndex] = command.ToString();

                    SendCommand(CreateCommand(output));
                }
            }
        }
Exemplo n.º 5
0
        private void PushOnPath(List <Point> path, Point agentPos, List <AgentCommand> commandList, int agentIndex, int boxIndex)
        {
            for (int i = 0; i < path.Count - 1; i++)
            {
                Point currentBoxPos = path[i];
                Point nextBoxPos    = path[i + 1];

                Direction agentDir = PointsToDirection(agentPos, currentBoxPos);
                Direction boxDir   = PointsToDirection(currentBoxPos, nextBoxPos);

                commandList.Add(AgentCommand.CreatePush(agentDir, boxDir));
                agentPos = currentBoxPos;

                MoveBox(nextBoxPos, boxIndex);
                MoveAgent(currentBoxPos, agentIndex);
            }
        }
Exemplo n.º 6
0
        public static string[] Parallelize(List <AgentCommands> allCommands, Level level)
        {
            int[,] world = new int[level.Width, level.Height];
            Point[]         agentPositions       = new Point[level.AgentCount];
            List <string[]> parallelizedCommands = new List <string[]>();

            Span <Entity> initialAgents = level.GetAgents();

            for (int i = 0; i < initialAgents.Length; i++)
            {
                agentPositions[i] = initialAgents[i].Pos;
            }

            foreach (AgentCommands agentCommands in allCommands)
            {
                foreach (AgentCommand command in agentCommands.Commands)
                {
                    Point agentPos = agentPositions[agentCommands.AgentIndex];
                    int   time     = world[agentPos.X, agentPos.Y];

                    Point newAgentPos = command.GetNextAgentPos(agentPos);
                    int   newTime     = Math.Max(world[agentPos.X, agentPos.Y], world[newAgentPos.X, newAgentPos.Y]);

                    if (command.CType != CommandType.MOVE)
                    {
                        Point boxPos     = command.GetBoxPos(agentPos);
                        Point nextBoxPos = command.GetNextBoxPos(boxPos);

                        newTime = Math.Max(newTime, world[boxPos.X, boxPos.Y]);
                        newTime = Math.Max(newTime, world[nextBoxPos.X, nextBoxPos.Y]);

                        world[boxPos.X, boxPos.Y]         = newTime + 1;
                        world[nextBoxPos.X, nextBoxPos.Y] = newTime + 1;
                    }

                    for (int i = parallelizedCommands.Count; i <= newTime; i++)
                    {
                        parallelizedCommands.Add(new string[level.AgentCount]);
                    }
                    parallelizedCommands[newTime][agentCommands.AgentIndex] = command.ToString();

                    world[agentPos.X, agentPos.Y]            = newTime + 1;
                    world[newAgentPos.X, newAgentPos.Y]      = newTime + 1;
                    agentPositions[agentCommands.AgentIndex] = newAgentPos;
                }
            }

            string[] finishedCommands = new string[parallelizedCommands.Count];
            int      index            = 0;

            foreach (var commands in parallelizedCommands)
            {
                for (int i = 0; i < commands.Length; i++)
                {
                    if (commands[i] == null)
                    {
                        commands[i] = AgentCommand.NoOp();
                    }
                }

                finishedCommands[index++] = ServerCommunicator.CreateCommand(commands);
            }

            return(finishedCommands);
        }
Exemplo n.º 7
0
        // GOD FUNCTION TROIS
        public List <AgentCommands> Solve()
        {
            List <AgentCommand> solution = new List <AgentCommand>();

            State currentState = Level.InitialState;

            foreach (Entity e in currentState.Entities)
            {
                if (e.Color != Agent.Color)
                {
                    Level.AddPermanentWalll(e.Pos);
                }
            }

            Point[] meisterPath = Precomputer.GetPath(Level, Agent.Pos, End, false);
            Point   freeSpot    = meisterPath[1];

            foreach (Point nextPoint in meisterPath)
            {
                if (nextPoint == meisterPath.First())
                {
                    continue;
                }

                Direction nextDir     = LessNaiveSolver.PointsToDirection(Agent.Pos, nextPoint);
                Direction freeSpotDir = LessNaiveSolver.PointsToDirection(Agent.Pos, freeSpot);

                if (nextDir == freeSpotDir)
                {
                    solution.Add(AgentCommand.CreateMove(nextDir));
                    freeSpot = Agent.Pos;
                    Agent    = Agent.Move(nextPoint);
                    continue;
                }

                else if (BoxSwimming.Opposite(nextDir) == freeSpotDir)
                {
                    if (BoxSwimming.CanLeftHandBoxSwim(nextDir, Agent.Pos, Level))
                    {
                        solution.AddRange(BoxSwimming.LeftHandBoxSwimming(nextDir));
                    }

                    else if (BoxSwimming.CanRightHandBoxSwim(nextDir, Agent.Pos, Level))
                    {
                        solution.AddRange(BoxSwimming.RightHandBoxSwimming(nextDir));
                    }

                    else
                    {
                        throw new Exception("Box swimming operations failed.");
                    }

                    freeSpot = Agent.Pos;
                    Agent    = Agent.Move(nextPoint);
                    continue;
                }

                else if (BoxSwimming.CounterClockwise(nextDir) == freeSpotDir)
                {
                    if (BoxSwimming.CanLeftHandBoxSwim(nextDir, Agent.Pos, Level))
                    {
                        solution.AddRange(BoxSwimming.SwimLeft(BoxSwimming.Opposite(freeSpotDir)));
                    }
                    else
                    {
                        throw new Exception("Box swimming operations failed.");
                    }
                    freeSpot = Agent.Pos;
                    Agent    = Agent.Move(nextPoint);
                    continue;
                }

                else if (BoxSwimming.Clockwise(nextDir) == freeSpotDir)
                {
                    if (BoxSwimming.CanRightHandBoxSwim(nextDir, Agent.Pos, Level))
                    {
                        solution.AddRange(BoxSwimming.SwimRight(BoxSwimming.Opposite(freeSpotDir)));
                    }
                    else
                    {
                        throw new Exception("Box swimming operations failed.");
                    }
                    //{
                    // He's got gotten!

                    //solution.Add(AgentCommand.CreateMove(freeSpotDir));
                    //solution.Add(AgentCommand.CreatePull)

                    // Considering that FORWARD right NOW is the opposite of freeSpot

                    /* Move into freeSpot
                     * Pull left-back box into free spot
                     * Push left-box into left-back
                     * Move nextDir
                     * if agent isn't at nextPoint, then L/R BoxSwim in nextDir
                     */

                    //}
                    freeSpot = Agent.Pos;
                    Agent    = Agent.Move(nextPoint);
                    continue;
                }

                else
                {
                    break;
                }
            }
            EndSolution = new List <AgentCommands>()
            {
                new AgentCommands(solution, 0)
            };
            return(EndSolution);
        }