public char Guess(Position position) { Console.WriteLine("Guessing: {0}, {1}", position.ColFromLeft, position.RowFromTop); var info = Reveal(position); RenderAll(info); return info; }
public bool CanGo(Position position) { return position.ColFromLeft >= 1 && position.ColFromLeft <= Width && position.RowFromTop >= 1 && position.RowFromTop <= Height; }
private static IEnumerable<Position> Get360Positions(Position position) { yield return position.Left(); yield return position.Up().Left(); yield return position.Up(); yield return position.Up().Right(); yield return position.Right(); yield return position.Down().Right(); yield return position.Down(); yield return position.Down().Left(); }
public void AddMine(Position position) { SetField(_hiddenField, position, BOMB); }
private int TryLook(Position position) { if (CanGo(position)) return Look(position); return 0; }
private bool ShouldStopLooking(Position position) { if (!CanGo(position)) return true; return IsVisible(position); }
private void SetField(char[][] field, Position position, char value) { field[position.RowFromTop - 1][position.ColFromLeft - 1] = value; }
private char RevealPosition(Position position) { char info = CalcWarning(position); SetField(_visibleField, position, info); return info; }
private int CalcWarningSide(Position position, int warning) { warning += TryLook(position); warning = CalcWarningUpAndDown(position, warning); return warning; }
private char Reveal(Position position) { var info = RevealPosition(position); if (info == NOTHING) RevealAdjacent(position); return info; }
private int Look(Position position) { return IsMineAt(position) ? 1 : 0; }
private bool IsVisible(Position position) { return GetField(_visibleField, position) != UNKNOWN; }
private bool IsMineAt(Position position) { return GetField(_hiddenField, position) == BOMB; }
private char GetField(char[][] field, Position position) { return field[position.RowFromTop - 1][position.ColFromLeft - 1]; }
private int CalcWarningUpAndDown(Position position, int warning) { warning += TryLook(position.Up()); warning += TryLook(position.Down()); return warning; }
private void RevealAdjacent(Position position) { foreach (var pos in Get360Positions(position)) { if (ShouldStopLooking(pos)) continue; if (RevealPosition(pos) == NOTHING) RevealAdjacent(pos); } }
private char CalcWarning(Position position) { if (IsMineAt(position)) return BOMB; var warning = Get360Positions(position).Sum(pos => TryLook(pos)); return ConvertWarningToString(warning); }