private Coordinates GenerateMinePosition() { Coordinates position; do { position = new Coordinates(random.Next(this.field.Rows), random.Next(this.field.Cols)); } while (this.field[position] != Field.Empty); return position; }
public static bool TryParse(string input, out Coordinates result) { try { result = Coordinates.Parse(input); return true; } catch { result = new Coordinates(0, 0); return false; } }
public static Coordinates Parse(string input) { if (input == null) { throw new ArgumentNullException("input", "Value can not be null."); } Match match = Regex.Match(input, @"^\s*(\d+)\s+(\d+)\s*$"); if (!match.Success) { throw new FormatException("Input string was not in a correct format."); } int row = int.Parse(match.Groups[1].Value); int col = int.Parse(match.Groups[2].Value); Coordinates result = new Coordinates(row, col); return result; }
public void DetonateMine(Coordinates position) { Mine mine = this.field.GetMine(position); string[] area = detonationAreaOfMine[mine]; Coordinates center = new Coordinates(area.GetLength(0) / 2, area[0].Length / 2); Coordinates topLeft = position - center; Coordinates bottomRight = position + center; this.field.ForEach( topLeft, bottomRight, currentPosition => { Coordinates areaPosition = currentPosition - topLeft; if (area[areaPosition.Row][areaPosition.Col] != Detonator.Empty) { this.field.Destroy(currentPosition); } }); }
public void ForEach(Coordinates topLeft, Coordinates bottomRight, Action<Coordinates> action) { Coordinates innerTopLeft = new Coordinates( Math.Max(topLeft.Row, 0), Math.Max(topLeft.Col, 0)); Coordinates innerBottomRight = new Coordinates( Math.Min(bottomRight.Row, this.Rows - 1), Math.Min(bottomRight.Col, this.Cols - 1)); for (int row = innerTopLeft.Row; row <= innerBottomRight.Row; row++) { for (int col = innerTopLeft.Col; col <= innerBottomRight.Col; col++) { action(new Coordinates(row, col)); } } }
public void Destroy(Coordinates coordinates) { this[coordinates] = Field.Detonated; }
public string this[Coordinates coordinates] { get { return this[coordinates.Row, coordinates.Col]; } set { this[coordinates.Row, coordinates.Col] = value; } }
public bool IsMine(Coordinates coordinates) { bool result = true; result = result && this[coordinates] != Field.Empty; result = result && this[coordinates] != Field.Detonated; return result; }
public Mine GetMine(Coordinates position) { if (!IsMine(position)) { throw new ArgumentException("There is no mine at this position."); } Mine mine = (Mine)int.Parse(this[position]); return mine; }