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()); }
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))); }
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); }
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) }); }
public MineGameCache(IMineGame subject) { _subject = subject; ColumnCount = subject.ColumnCount; LineCount = subject.LineCount; _cache = new MineArray <int?>(LineCount, ColumnCount); }
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)); } } }
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)); }
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; } } } }
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)); } } } }
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)); } } } }
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()); }
public Core(IMineGame mineGame, MineDetector mineDetector) { Choices = FindSafePlace(mineGame, mineDetector).ToList(); }
public IEnumerable <Choice> GetChoices(IMineGame mineGame, MineDetector mineDetector) { return(new Core(mineGame, mineDetector).Choices); }