public void GetBestMoveTest(string input, Color color1, Color color2, int expeceted) { var matrix = input.StringToByteMatrix(10); var weights = new TsitsiklisWeights(1, 3); var algorithm = new Tsitsiklis(weights); var manager = new BoardManager(matrix); manager.SpawnPill(color1, color2); var engine = new AiEngine(algorithm); var bestMove = engine.GetNextMove(manager); Assert.AreEqual(expeceted, bestMove.Fitness); }
public void MovesAreCorrect(string input, Move[] expeceted) { var matrix = input.StringToByteMatrix(8); var weights = new AiWeights() { PillsCleared = -100, WellSums = 1, NumberOfHoles = 10, }; var algorithm = new FeatureAi(weights); var manager = new BoardManager(matrix); manager.SpawnPill(Color.Red, Color.Blue); var engine = new AiEngine(algorithm); var bestMove = engine.GetNextMove(manager); var moves = bestMove.Moves; Assert.IsTrue(bestMove.IsValid); Console.WriteLine(JsonConvert.SerializeObject(bestMove)); foreach (var move in moves) { manager.Move(move); Console.WriteLine(manager.GameBoard.MatrixToString(manager.ActivePill)); } while (manager.ActivePill != null) { manager.Move(Move.Down); Console.WriteLine(manager.GameBoard.MatrixToString(manager.ActivePill)); } Assert.AreEqual(expeceted.Where(x => x == Move.Right).Count(), moves.Where(x => x == Move.Right).Count()); Assert.AreEqual(expeceted.Where(x => x == Move.Left).Count(), moves.Where(x => x == Move.Left).Count()); Assert.AreEqual(expeceted.Where(x => x == Move.RotateLeft).Count(), moves.Where(x => x == Move.RotateLeft).Count()); Assert.AreEqual(expeceted.Where(x => x == Move.RotateRight).Count(), moves.Where(x => x == Move.RotateRight).Count()); Assert.That(bestMove.Fitness, Is.AtMost(1), "Fitness"); }
static void Main(string[] args) { var uiEnabled = false; var gameCount = 0; Engine.Random.Instance().SetNewSeed(123); _colorMap = MapColors(); var httpclient = new ApiClient.ApiClient(new Uri("http://localhost:3000")); var algorithmSettings = new AlgorithmSetting <AiWeights>(); var moves = new Stack <Move>(); while (true) { try { algorithmSettings = httpclient.GetAlgorithmSettings <FeatureAi, AiWeights>( new AlgorithmSetting <AiWeights> { Name = "Dr Mario - Engine" }); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine($"Error: retrying in a sec"); Thread.Sleep(1000); continue; } var gameManager = new GameManager(20, 10); gameManager.AddBacterias(15, 3); var ai = new AiEngine(new FeatureAi(algorithmSettings.Weights)); IEnumerator moveIterator = null; AI.Move aiMove = null; var blockNumber = -1; while (!gameManager.GameState.IsGameOver()) { if (uiEnabled) { PrintState(gameManager, moves, aiMove); } Thread.Sleep(2); gameManager.OnGameLoopStep(); if (moveIterator != null && moveIterator.MoveNext()) { gameManager.MoveBlock((Move)moveIterator.Current); moves.Push((Move)moveIterator.Current); continue; } // Make sure we only calculate best move once per spawned block. if (blockNumber != gameManager.GameStats.PillsSpawned) { aiMove = ai.GetNextMove(gameManager.BoardManager); moveIterator = aiMove.Moves.GetEnumerator(); blockNumber = gameManager.GameStats.PillsSpawned; } if (gameManager.Bacterias.Count < BacteriaCount) { var color = gameManager.Bacterias.GroupBy(x => x.Color).OrderByDescending(o => o.Count()).First().Key; gameManager.AddBacteria(color); } } if (uiEnabled) { Console.Clear(); Console.Write("Game Over {0}", gameManager.GameStats.Fitness); Console.WriteLine(); Console.WriteLine(); } else { Console.Write("{0}, ", gameManager.GameStats.Fitness); } try { httpclient.PostStats(new GameResult <AiWeights>(algorithmSettings) { PillsSpawned = gameManager.GameStats.PillsSpawned, Bacterias = gameManager.GameStats.TotalBacteriaClearings, Pills = gameManager.GameStats.TotalPillClearings, Fitness = gameManager.GameStats.Fitness }); gameCount++; } catch (Exception e) { Console.WriteLine(e); Thread.Sleep(1000); } Thread.Sleep(50); if (uiEnabled) { Thread.Sleep(5000); } } }