Exemple #1
0
        public IEnumerable <Choice> GetChoices(IMineGame mineGame, MineDetector mineDetector)
        {
            var result = new MineArray <double>(mineGame.LineCount, mineGame.ColumnCount);

            foreach (var coordinate in mineGame.AllCells())
            {
                var contant = mineGame.NeighboursMineCountAt(coordinate);
                if (contant == null)
                {
                    continue;
                }

                result[coordinate] = double.PositiveInfinity;

                var neighbours          = mineGame.GetNeighbours(coordinate).ToList();
                var unclickedNeighbours =
                    neighbours.Where(coords => mineGame.NeighboursMineCountAt(coords) == null).ToList();
                if (unclickedNeighbours.Count == 0)
                {
                    continue;
                }

                var score = contant.Value / unclickedNeighbours.Count;
                foreach (var c in unclickedNeighbours)
                {
                    result[c] += score;
                }
            }

            var bestScore  = result.Min();
            var candidates = mineGame.AllCells().Where(coords => Equals(result[coords], bestScore));

            return(candidates.Select(c => new Choice(c, 0.2)).ToList());
        }
Exemple #2
0
        public static bool IsGameFinished(this IMineGame mineGame)
        {
            var mines = new MineDetector(mineGame);

            return(mineGame.AllCells().Where(c => mineGame.NeighboursMineCountAt(c) == null)
                   .All(c => mines.IsSureAMine(c)));
        }
Exemple #3
0
        public CellCoordinate GetBestChoice(IMineGame mineGame)
        {
            var mineDetector = new MineDetector(mineGame);
            var bestChoices  = _strategies.SelectMany(s => s.GetChoices(mineGame, mineDetector))
                               .OrderBy(c => c.MineProbability).GroupBy(c => c.MineProbability).First().ToList();

            return(bestChoices[_random.Next(bestChoices.Count)].Coordinate);
        }
Exemple #4
0
        public IEnumerable <Choice> GetChoices(IMineGame mineGame, MineDetector mineDetector)
        {
            var l = mineGame.AllCells()
                    .Where(c => !mineDetector.IsSureAMine(c) && mineGame.NeighboursMineCountAt(c) == null).ToList();
            var cellCoordinate = l[_random.Next(l.Count)];

            return(new[] { new Choice(cellCoordinate, 1) });
        }
Exemple #5
0
        public MineGameCache(IMineGame subject)
        {
            _subject = subject;

            ColumnCount = subject.ColumnCount;
            LineCount   = subject.LineCount;

            _cache = new MineArray <int?>(LineCount, ColumnCount);
        }
Exemple #6
0
 public static IEnumerable <CellCoordinate> AllCells(this IMineGame mineGame)
 {
     for (var l = 0; l < mineGame.LineCount; l++)
     {
         for (var c = 0; c < mineGame.ColumnCount; c++)
         {
             yield return(new CellCoordinate(l, c));
         }
     }
 }
Exemple #7
0
        private static IEnumerable <CellCoordinate> GetCandidates(IMineGame mineGame)
        {
            var c = mineGame.ColumnCount - 1;
            var l = mineGame.LineCount - 1;

            yield return(new CellCoordinate(0, 0));

            yield return(new CellCoordinate(l, 0));

            yield return(new CellCoordinate(l, c));

            yield return(new CellCoordinate(0, c));
        }
Exemple #8
0
 public MineDetector(IMineGame mineGame)
 {
     _mines = new MineArray <bool>(mineGame.LineCount, mineGame.ColumnCount);
     foreach (var point in mineGame.AllCells())
     {
         var undigedNeighbours = mineGame.GetNeighbours(point)
                                 .Where(p => mineGame.NeighboursMineCountAt(p) == null).ToList();
         if (undigedNeighbours.Count == mineGame.NeighboursMineCountAt(point))
         {
             foreach (var undigedNeighbour in undigedNeighbours)
             {
                 _mines[undigedNeighbour] = true;
             }
         }
     }
 }
Exemple #9
0
 private static IEnumerable <Choice> FindSafePlace(IMineGame mineGame, MineDetector mineDetector)
 {
     foreach (var point in mineGame.AllCells())
     {
         var neighbours  = mineGame.GetNeighbours(point).ToList();
         var minesCount1 = neighbours.Count(mineDetector.IsSureAMine);
         var minesCount2 = mineGame.NeighboursMineCountAt(point);
         if (minesCount1 == minesCount2)
         {
             foreach (var coordinate in neighbours.Where(
                          p => mineGame.NeighboursMineCountAt(p) == null && !mineDetector.IsSureAMine(p)))
             {
                 yield return(new Choice(coordinate, 0));
             }
         }
     }
 }
Exemple #10
0
        public static IEnumerable <CellCoordinate> GetNeighbours(this IMineGame mineGame, CellCoordinate coordinate)
        {
            for (var dl = -1; dl <= 1; dl++)
            {
                for (var dc = -1; dc <= 1; dc++)
                {
                    if (dl == 0 && dc == 0)
                    {
                        continue;
                    }

                    var l = coordinate.Line + dl;
                    var c = coordinate.Column + dc;

                    if (l >= 0 && l < mineGame.LineCount && c >= 0 && c < mineGame.ColumnCount)
                    {
                        yield return(new CellCoordinate(l, c));
                    }
                }
            }
        }
Exemple #11
0
 public IEnumerable <Choice> GetChoices(IMineGame mineGame, MineDetector mineDetector)
 {
     return(GetCandidates(mineGame)
            .Where(c => mineGame.NeighboursMineCountAt(c) == null && !mineDetector.IsSureAMine(c))
            .Select(c => new Choice(c, 0.1)).ToList());
 }
Exemple #12
0
 public Core(IMineGame mineGame, MineDetector mineDetector)
 {
     Choices = FindSafePlace(mineGame, mineDetector).ToList();
 }
Exemple #13
0
 public IEnumerable <Choice> GetChoices(IMineGame mineGame, MineDetector mineDetector)
 {
     return(new Core(mineGame, mineDetector).Choices);
 }