Example #1
0
        /// <summary>
        /// Run the simulations and save result to ViewState and JS script block
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRunSimulations_OnClick(object sender, EventArgs e)
        {
            var deck = new Deck();
            List<int> position = Request.Form["robotPosition"].Split(',').Select(int.Parse).ToList();
            var cards = Request.Form["cards"].Split(',').Select(x => GetCardPriority(deck, x));

            var robot = new Robot
            {
                Position = new Coordinate {X = position[0], Y = position[1]},
                Facing = (Orientation)Enum.Parse(typeof(Orientation), Request.Form["robotOrientation"])
            };

            foreach (var priority in cards)
            {
                robot.DealCard(priority);
            }

            var map = MapParser.JsonToMap(LoadMapJson("~/Maps/ScottRallyMap.rrdl"));

            var game = new Game(new Map {Squares = map}, new List<Robot> {robot});
            game.Initialize();

            List<List<CardExecutionResult>> results = Simulator.Simulate(robot);
            List<List<CardExecutionResult>> productiveResults = results.Where(result => result.Last().Position.X != -1).ToList();

            ViewState["PosX"] = position[0];
            ViewState["PosY"] = position[1];
            ViewState["Facing"] = Request.Form["robotOrientation"];
            ViewState["Cards"] = Request.Form["cards"];

            ClientScript.RegisterClientScriptBlock(GetType(), "results", "results = " + JsonConvert.SerializeObject(productiveResults, Formatting.Indented), true);
        }
Example #2
0
        protected void btnRunSimulations_OnClick(object sender, EventArgs e)
        {
            List<int> position = Request.Form["robotPosition"].Split(',').Select(int.Parse).ToList();
            var cards = Request.Form["cards"].Split(',').Select(int.Parse);

            var robot = new Robot
            {
                Position = new Coordinate {X = position[0], Y = position[1]},
                Facing = (Orientation)Enum.Parse(typeof(Orientation), Request.Form["robotOrientation"])
            };
            foreach (var c in cards)
            {
                robot.DealCard(c);
            }

            var game = new Game { Board = { Squares = Maps.GetMap(Maps.MapLayouts.ScottRallyMap) }, Robots = new List<Robot> { robot } };
            game.Initialize();

            List<List<CardExecutionResult>> results = Simulator.RunSimulations(robot);
            List<List<CardExecutionResult>> productiveResults = results.Where(result => result.Last().Position.X != -1).ToList();

            ViewState["PosX"] = position[0];
            ViewState["PosY"] = position[1];
            ViewState["Facing"] = Request.Form["robotOrientation"];
            ViewState["Cards"] = Request.Form["cards"];

            ClientScript.RegisterClientScriptBlock(GetType(), "results", "results = " + JsonConvert.SerializeObject(productiveResults, Formatting.Indented), true);
        }
Example #3
0
        public static string RunSimulations(string body)
        {
            JToken json = JToken.Parse(body);

            var deck = new Deck();
            IEnumerable<int> cards = json["cards"].ToString().Split(',').Select(x => GetCardPriority(deck, x));
            List<int> robotPosition = json["robotPosition"].ToString().Split(',').Select(int.Parse).ToList();
            string robotOrientation = json["robotOrientation"].ToString();

            var robot = new Robot
            {
                Position = new Coordinate { X = robotPosition[0], Y = robotPosition[1] },
                Facing = (Orientation)Enum.Parse(typeof(Orientation), robotOrientation)
            };

            foreach (var priority in cards)
            {
                robot.DealCard(priority);
            }

            var map = MapParser.JsonToMap(LoadMapJson(ActiveMap));

            var game = new Game(new Map { Squares = map }, new List<Robot> { robot });
            game.Initialize();

            List<List<CardExecutionResult>> results = Simulator.Simulate(robot);
            List<List<CardExecutionResult>> productiveResults = results.Where(result => result.Last().Position.X != -1).ToList();

            return JsonConvert.SerializeObject(productiveResults, Formatting.Indented);
        }
        public void Simulator_PermutationCounts_7Cards_2TypeRepeatedTwice()
        {
            // Arrange
            var robot = new Robot();
            robot.DealCard(10);		// UTurn
            robot.DealCard(20);		// UTurn
            robot.DealCard(70);		// RotateLeft
            robot.DealCard(80);		// RotateRight
            robot.DealCard(430);	// BackUp
            robot.DealCard(490);	// Move1
            robot.DealCard(500);	// Move1

            // Act
            int permutations = PermutationCounts(robot);

            // Assert
            Assert.AreEqual(630, permutations);
        }
Example #5
0
        public void Simulator_RunSimulations_7Cards2TypeRepeatedTwice()
        {
            // Arrange
            var robot = new Robot();
            robot.DealCard(10);		// UTurn
            robot.DealCard(20);		// UTurn
            robot.DealCard(70);		// RotateLeft
            robot.DealCard(80);		// RotateRight
            robot.DealCard(430);	// BackUp
            robot.DealCard(490);	// Move1
            robot.DealCard(500);	// Move1
            var game = new Game(new Map {Squares = Maps.GetMap(Maps.MapLayouts.ScottRallyMap)}, new List<Robot> {robot});
            game.Initialize();

            // Act
            List<List<CardExecutionResult>> results = Simulator.Simulate(robot);

            // Assert
            Assert.AreEqual(690, results.Count);
        }
Example #6
0
        /// <summary>
        /// Runs simulations on moves based on the cards the robot currently has assigned. 
        /// Robot is expected to be initialized and part of an existing Game.
        /// The robot will not be in the same state after simulation is executed.
        /// </summary>
        /// <param name="robot">Robot to run simulations for</param>
        /// <returns>List of results</returns>
        public static List<List<CardExecutionResult>> Simulate(Robot robot)
        {
            var results = new List<List<CardExecutionResult>>();
            List<List<ProgramCardType>> permutations = CalculateMovePermutations(robot);
            Coordinate position = robot.Position;
            Orientation facing = robot.Facing;

            foreach (var permutation in permutations)
            {
                robot.Position = position;
                robot.Facing = facing;
                robot.Damage = 0;
                robot.PickUpCards();

                var deck = new Deck();
                var permutationResult = new List<CardExecutionResult>();
                for (int i = 0 ; i < permutation.Count; ++i)
                {
                    var card = deck.GetCard(permutation[i]);
                    robot.DealCard(card.Priority, i + 1);
                }

                robot.Game.StartTurn(false /* Deal Cards */);
                int previousDamage = 0;

                while (true)
                {
                    int registersLeft = robot.Game.ExecuteNextRegister();

                    permutationResult.Add(new CardExecutionResult
                    {
                        Card = permutation[permutationResult.Count],
                        Position = robot.Position,
                        Facing = robot.Facing,
                        Damage = robot.Damage - previousDamage
                    });

                    previousDamage = robot.Damage;

                    if (registersLeft == 0)
                        break;
                }

                robot.Game.EndTurn();

                results.Add(permutationResult);
            }

            return results;
        }
Example #7
0
        public void Simulator_CalculateMovePermutations_7UniqueCards()
        {
            // Arrange
            var robot = new Robot();
            robot.DealCard(10);		// UTurn
            robot.DealCard(70);		// RotateLeft
            robot.DealCard(80);		// RotateRight
            robot.DealCard(430);	// BackUp
            robot.DealCard(490);	// Move1
            robot.DealCard(670);	// Move2
            robot.DealCard(790);	// Move3

            // Act
            var moves = Simulator.CalculateMovePermutations(robot);

            // Assert
            Assert.AreEqual(2520, moves.Count);
        }
Example #8
0
        public void Simulator_CalculateMovePermutations_7Cards_2TypeRepeatedTwice()
        {
            // Arrange
            var robot = new Robot();
            robot.DealCard(10);		// UTurn
            robot.DealCard(20);		// UTurn
            robot.DealCard(70);		// RotateLeft
            robot.DealCard(80);		// RotateRight
            robot.DealCard(430);	// BackUp
            robot.DealCard(490);	// Move1
            robot.DealCard(500);	// Move1

            // Act
            var moves = Simulator.CalculateMovePermutations(robot);

            // Assert
            Assert.AreEqual(690, moves.Count);
        }