Exemple #1
0
        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);
                }
            }
        }
Exemple #2
0
        // [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;
                }
            }
        }
Exemple #3
0
 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));
         }
     }
 }
Exemple #4
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        public override (Column Column, int Row) Predict(
            Dictionary <string, CoordinateContainerBase> currentGameBoardState)
        {
            var availableCoords = BuildFlattenedKeys(currentGameBoardState);

            if (!availableCoords.Any())
            {
                throw new ArgumentOutOfRangeException("Can't predict a coordinate when all coordinates are marked! Game should have ended by now!");
            }

            availableCoords.Shuffle();

            return(CoordinateKey.Parse(availableCoords.First()));
        }
Exemple #7
0
        protected (Column Column, int Row) PredictBySimulation(
            Dictionary <string, CoordinateContainerBase> currentGameBoardState)
        {
            var orderedByHighestProbability = SimulateShipsOnBoard();

            if (FindNonMarkedBoxFromHighestProbability(
                    orderedByHighestProbability,
                    currentGameBoardState,
                    out var foundKey))
            {
                return(CoordinateKey.Parse(foundKey));
            }

            // If no available box has been found after 20 simulations, fallback to random prediction
            return(base.PredictRandom(currentGameBoardState));
        }
Exemple #8
0
            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);
                }
            }
Exemple #9
0
        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);
        }
Exemple #10
0
        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);
        }