private void BuildNeighbours() { _neighbourMap = new Dictionary <string, Neighbours>(); foreach (Column column in Enum.GetValues(typeof(CoordinatesHelper.Column))) { foreach (var row in Enumerable.Range(1, GameConstants.MaxRowCount)) { var key = CoordinateKey.Build(column, row); var neighbourUp = GetNeighbourUp(column, row); var neighbourDown = GetNeighbourDown(column, row); var neighbourLeft = GetNeighbourLeft(column, row); var neighbourRight = GetNeighbourRight(column, row); var neighbours = new Neighbours() { NeighbourUp = neighbourUp.Exist ? neighbourUp.Key : string.Empty, NeighbourDown = neighbourDown.Exist ? neighbourDown.Key : string.Empty, NeighbourLeft = neighbourLeft.Exist ? neighbourLeft.Key : string.Empty, NeighbourRight = neighbourRight.Exist ? neighbourRight.Key : string.Empty, }; _neighbourMap.Add(key, neighbours); } } }
// [Fact] public void Benchmark_AverageElapsedTime_PerPrediction_ForHunter() { var gameBoard = base.CreateGameBoard(); var elapsed = new List <long>(); var AIManager = new AIManager(); var maxScore = 100; var stopWatch = new Stopwatch(); foreach (var move in Enumerable.Range(0, maxScore)) { stopWatch.Restart(); var(column, row) = AIManager.PredictCoordinate(AILevel.Hunter, gameBoard.ForOpponent().Matrix); stopWatch.Stop(); elapsed.Add(stopWatch.ElapsedMilliseconds); var key = CoordinateKey.Build(column, row); var(shipFound, shipDestroyed) = gameBoard.MarkCoordinate(key); if (gameBoard.IsAllDestroyed()) { var min = elapsed.Min(); var avg = elapsed.Average(); var max = elapsed.Max(); File.AppendAllText("benchmark-hunter-elapsed-time.txt", $"\n{min} ms\t {avg} ms\t {max} ms"); return; } } }
private void FillMatrix() { foreach (Column column in Enum.GetValues(typeof(CoordinatesHelper.Column))) { foreach (var row in Enumerable.Range(1, GameConstants.MaxRowCount)) { var key = CoordinateKey.Build(column, row); Matrix.TryAdd(key, new CoordinateContainerBase(column, row)); } } }
private (bool Exist, string Key) GetNeighbourLeft(Column col, int row) { if ((int)col == 1) { return(false, string.Empty); } var newCol = (Column)((int)col - 1); var key = CoordinateKey.Build(newCol, row); return(true, key); }
private (bool Exist, string Key) GetNeighbourDown(Column col, int row) { if (row == 10) { return(false, string.Empty); } var newRow = row + 1; var key = CoordinateKey.Build(col, newRow); return(true, key); }
public void NeighbourMap_ShouldContain_AllCoordOnGameBoard() { // Arrange var flattenedCords = new List <string>(); foreach (Column column in Enum.GetValues(typeof(CoordinatesHelper.Column))) { foreach (var row in Enumerable.Range(1, GameConstants.MaxRowCount)) { flattenedCords.Add(CoordinateKey.Build(column, row)); } } foreach (var coordKey in flattenedCords) { var neighbour = CoordinateNeighbours.Instance.GetNeighbours(coordKey); Assert.NotEmpty(neighbour.AvailableNeighbours); } }
public int RunRandomGame() { var gameBoard = CreateGameBoard(); var maxScore = 100; var AIManager = new AIManager(); foreach (var move in Enumerable.Range(0, maxScore)) { var(column, row) = AIManager.PredictCoordinate(AILevel.Random, gameBoard.ForOpponent().Matrix); var(_, _x) = gameBoard.MarkCoordinate(CoordinateKey.Build(column, row)); maxScore--; if (gameBoard.IsAllDestroyed()) { return(maxScore); } } return(maxScore); }
public int RunMonteCarloWithHuntGame() { var gameBoard = CreateGameBoard(); var maxScore = 100; var AIManager = new AIManager(); foreach (var move in Enumerable.Range(0, maxScore)) { var(column, row) = AIManager.PredictCoordinate(AILevel.MonteCarloAndHunt, gameBoard.ForOpponent().Matrix); var key = CoordinateKey.Build(column, row); var(shipFound, shipDestroyed) = gameBoard.MarkCoordinate(key); maxScore--; if (gameBoard.IsAllDestroyed()) { return(maxScore); } } return(maxScore); }