private static void ExportFrames(CompletedSimulation sim, IAiFactory aiFactory, String outputDir) { const Int32 tileSize = 10; IAi ai = aiFactory.Create(sim.Parameters); Level level = sim.ProtoLevel.Generate(); Rover rover = new Rover(level, sim.Parameters); VisibleState state = VisibleState.GenerateBlank(level.BottomRight, rover.Position); using var actionEnumerator = ai.Simulate(rover.Accessor).GetEnumerator(); Int32 maxFrameCount = sim.Parameters.InitialMovesLeft; Int32 filenameDigits = (Int32)Math.Ceiling(Math.Log10(maxFrameCount)) + 1; String fileBase = Path.Combine(outputDir, $"frame-{sim.ProtoLevel.Seed}-"); DirectoryInfo dir = new DirectoryInfo(outputDir); if (dir.Exists) { foreach (var file in dir.EnumerateFiles()) { file.Delete(); } } else { dir.Create(); } Int32 width = level.Width * tileSize; Int32 height = level.Height * tileSize; using Bitmap bitmap = new Bitmap(width, height); using Graphics surface = Graphics.FromImage(bitmap); Update update = new Update(terrain: rover.Adjacent); Int32 frameIndex = 0; do { if (!state.Apply(update)) { continue; } GdiRenderer.Draw(surface, width, height, state); String suffix = frameIndex.ToString().PadLeft(filenameDigits, '0'); String filename = fileBase + suffix + ".png"; bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png); frameIndex++; }while (actionEnumerator.MoveNext() && rover.Perform(actionEnumerator.Current, out update)); }
public void TestSingleFile(string exe) { var badShots = 0; var crashes = 0; var gamesPlayed = 0; var shots = new List <int>(); var ai = aiFactory.Create(exe, monitor); for (var gameIndex = 0; gameIndex < settings.GamesCount; gameIndex++) { var map = mapGenerator.GenerateMap(); var game = gameFactory.Create(map, ai); RunGameToEnd(game); gamesPlayed++; badShots += game.BadShots; if (game.AiCrashed) { crashes++; if (crashes > settings.CrashLimit) { break; } ai = aiFactory.Create(exe, monitor); } else { shots.Add(game.TurnsCount); } if (settings.Verbose) { Console.WriteLine( "Game #{3,4}: Turns {0,4}, BadShots {1}{2}", game.TurnsCount, game.BadShots, game.AiCrashed ? ", Crashed" : "", gameIndex); } } ai.Dispose(); WriteTotal(ai, shots, crashes, badShots, gamesPlayed); }