public void Can_Run_A_Robot_Through_A_Game_And_Get_A_Score() { IDictionary <Situation, RobotAction> moveStrategy = StrategyGenerator.Random(); Scorer scorer = new Scorer(); Robot robot = new Robot(moveStrategy, scorer); Board board = new Board(10, 10); var litterer = new BoardLitterer(0.5); litterer.Litter(board); board.AddElement(robot, new Point(0, 0)); const int numberOfTurns = 200; for (int i = 0; i < numberOfTurns; i++) { robot.Act(board); } int score = robot._scorer.Score; Debug.Print("Score is: {0}", score); }
public void Can_Test_Multiple_Strategies_And_Order_Them_By_Score() { int strategiesToTest = 10; var strategies = new Dictionary <Situation, RobotAction> [strategiesToTest] .Select(x => StrategyGenerator.Random()).ToArray(); var testingRobots = new Robot[strategiesToTest] .Select((x, i) => new Robot(strategies[i], new Scorer())).ToArray(); foreach (var robot in testingRobots) { Board b = new Board(10, 10); var litterer = new BoardLitterer(0.5); litterer.Litter(b); b.AddElement(robot, new Point(0, 0)); const int numberOfTurns = 200; for (int i = 0; i < numberOfTurns; i++) { robot.Act(b); } } var orderedResults = testingRobots.OrderBy(x => x._scorer.Score); Debug.Print(String.Join(", ", orderedResults.Select(x => x._scorer.Score))); }
public double CalculateFitness(Robot robot) { for (int boardsToTest = 0; boardsToTest < BoardCountToCalculateFitness; boardsToTest++) { Board b = new Board(10, 10); var litterer = new BoardLitterer(0.5); litterer.Litter(b); b.AddElement(robot, new Point(0, 0)); const int numberOfTurns = 200; for (int i = 0; i < numberOfTurns; i++) { robot.Act(b); } } return((double)robot._scorer.Score / BoardCountToCalculateFitness); }
public void BoardLitterer_Can_Randomly_Place_Rubbish_On_Board() { var board = new Board(100, 100); var litterer = new BoardLitterer(0.5); litterer.Litter(board); int rubbishTotal = 0; for (int x = 0; x < board.Width; x++) { for (int y = 0; y < board.Height; y++) { Assert.IsTrue(board.Contents(new Point(x, y)).Count() <= 1); if (board.Contents(new Point(x, y)).Count() == 1) { ++rubbishTotal; } } } Debug.Print("Rubbish count: {0}", rubbishTotal); Assert.IsTrue(rubbishTotal > 4000 && rubbishTotal < 6000, "Might fail on occasion, but is PROBABLY still working. Rerun test to check, if it failes."); }