public void SetRecursiveSurroundingHitCount(CoOrdinate coOrd) { // var gridCell = Grid[coOrd.XCord, coOrd.YCord]; // if (gridCell.IsBomb) // { // return; // } var hitCount = GetSurroundingHitCount(coOrd); SetAsDug(coOrd, hitCount); if (hitCount > 0) { return; } foreach (var surround in AllVaidBorderCoordinates(coOrd)) { var surroundGridCell = Grid[surround.XCord, surround.YCord]; if (surroundGridCell.IsBomb == false && surroundGridCell.hasBeenDug == false && surroundGridCell.ProximityCount == 0) { var subHitcount = GetSurroundingHitCount(surroundGridCell.CoOrd); SetAsDug(surroundGridCell.CoOrd, subHitcount); if (subHitcount == 0) { SetRecursiveSurroundingHitCount(surroundGridCell.CoOrd); } } } }
private void SetAsDug(CoOrdinate coOrd, int hitCount) { var cell = Grid[coOrd.XCord, coOrd.YCord]; cell.hasBeenDug = true; cell.ProximityCount = SetProximityCount(hitCount); }
public bool DigAndCheckIsBomb(CoOrdinate coOrd) { var gridCell = Grid[coOrd.XCord, coOrd.YCord]; if (gridCell.IsBomb) { gridCell.IsExplosion = true; return(true); } SetRecursiveSurroundingHitCount(coOrd); return(false); }
public int GetSurroundingHitCount(CoOrdinate coOrd) { var hitCount = 0; foreach (var surround in AllVaidBorderCoordinates(coOrd)) { if (Grid[surround.XCord, surround.YCord].IsBomb == true) { hitCount++; } } return(hitCount); }
private IEnumerable <CoOrdinate> AllVaidBorderCoordinates(CoOrdinate coOrd) { for (var xCord = coOrd.XCord - 1; xCord <= coOrd.XCord + 1; xCord++) { for (var yCord = coOrd.YCord - 1; yCord <= coOrd.YCord + 1; yCord++) { var surroundCoord = new CoOrdinate(xCord, yCord); if (IsCurrentPosition(coOrd, surroundCoord)) { continue; } if (IsInsideGrid(surroundCoord) == false) { continue; } yield return(surroundCoord); } } }
private bool IsInsideGrid(CoOrdinate coOrd) { return(coOrd.XCord >= 0 && coOrd.XCord < Width && coOrd.YCord >= 0 && coOrd.YCord < Height); }
private static bool IsCurrentPosition(CoOrdinate currCoOrd, CoOrdinate targetCoOrd) { return(currCoOrd.XCord == targetCoOrd.XCord && currCoOrd.YCord == targetCoOrd.YCord); }