private void ShowPlay(PlaytestRecording recording, int speedMS) { List <PlaytestRoundInfo> fitnessRoundInfoList = recording.PlaytestRoundInfoList; int score = recording.InitialPlaytestRoundInfo.Score; bool isAlive = recording.InitialPlaytestRoundInfo.IsAlive; UpdateCurrentGridInitial(recording.InitialPlaytestRoundInfo.ChangedFields); SnakeGameGUI snakeGameGUI = new SnakeGameGUI(snakeSettings, currentGrid, score, isAlive); int snakeSpeedMS = speedMS; // START GUI snakeGameGUI.OpenGameWindow(); // Loop throug frames. for (int i = 0; i < fitnessRoundInfoList.Count && snakeGameGUI.WindowRunning; i++) { UpdateCurrentGrid(fitnessRoundInfoList[i].ChangedFields); snakeGameGUI.UpdateView(currentGrid, fitnessRoundInfoList[i].Score, fitnessRoundInfoList[i].IsAlive, fitnessRoundInfoList[i].SnakeHeadPoint); Thread.Sleep(snakeSpeedMS); } }
public PlaytestResult(TestResult testResult, PlaytestRecording playtestRecorder) { TestResult = testResult; PlaytestRecorder = playtestRecorder; }
/// <summary> /// Represents a class which is able to play a recording of a playtest. /// </summary> /// <param name="recording"></param> public PlaytestReplay(PlaytestRecording recording) { this.recording = recording; networkSettings = recording.NetworkSettings; snakeSettings = recording.SnakeSettings; }
/// <summary> /// Runs a play test simulation of the saved agent and returns the result. /// </summary> /// <param name="savedAgent"></param> /// <param name="testType"></param> /// <param name="record"></param> /// <returns></returns> public PlaytestResult RunSimulation(SavedAgent savedAgent, TestType testType, bool record) { int action = 0; int movesTotal = 0; int currentScore = 0; int movesSincePoint = 0; int maxStepsBeforeTerminating = 1000; double averageMovesPerFood = 0; bool isInBounds = true; PlaytestRecording recorder = null; TestResult testResult = TestResult.Failed; NetworkSettings networkSettings = (savedAgent.geneticSettings.FitnessCalculator as SnakeFitnessCalculator).NetworkSettings; SnakeSettings snakeSettings = (savedAgent.geneticSettings.FitnessCalculator as SnakeFitnessCalculator).SnakeSettings; // Setup new snake game and neural network NeuralNetwork neuralNetwork = new NeuralNetwork(networkSettings, savedAgent.agent.Chromosome.ToArray()); double[] input = new double[networkSettings.numberOfInputNeurons]; SnakeGame snakeGame = new SnakeGame(snakeLevel); // Make recorder if (record) { recorder = new PlaytestRecording(savedAgent.agent, networkSettings, snakeSettings); recorder.TakeSnapShotInitial(snakeGame); } if (record) // Save round info. { recorder.TakeSnapShotRound(snakeGame); } // Check within bounds. if (testType == TestType.WithinBounds) { if (snakeGame.Grid.PointWithinGrid(snakeGame.Snake.Head.Point)) { testResult = TestResult.Passed; } else { testResult = TestResult.Failed; isInBounds = false; } } if (isInBounds) { do { input = ConvertGridToInput(snakeGame.Grid, snakeGame.Snake, snakeGame.Food); action = neuralNetwork.CalculateOutput(input); snakeGame.UpdateDirection(action); // Check if got point if (snakeGame.Score != currentScore) { movesSincePoint = 0; currentScore = snakeGame.Score; } else { movesSincePoint++; } movesTotal++; if (record) // Save round info. { recorder.TakeSnapShotRound(snakeGame); } // Test within bounds. if (testType == TestType.WithinBounds) { if (snakeGame.Grid.PointWithinGrid(snakeGame.Snake.Head.Point)) { testResult = TestResult.Passed; } else { testResult = TestResult.Failed; isInBounds = false; } } } while(snakeGame.Snake.IsAlive && movesSincePoint < maxStepsBeforeTerminating && isInBounds); } // Check if snake completed game. if (testType == TestType.CanComplete) { if (snakeGame.MaxScore != snakeGame.Score) { testResult = TestResult.Failed; } else { testResult = TestResult.Passed; } } // Get avg. moves per food. if (snakeGame.FoodEaten != 0) { averageMovesPerFood = movesTotal / (double)snakeGame.FoodEaten; } EndTestInfo endTestInfo = new EndTestInfo(snakeGame, movesTotal); if (record) { recorder.TakeSnapShotEndTest(endTestInfo); } PlaytestResult playtestResult = new PlaytestResult(testResult, recorder); return(playtestResult); }