Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
 public static bool TryParse(string input, out Coordinates result)
 {
     try
     {
         result = Coordinates.Parse(input);
         return true;
     }
     catch
     {
         result = new Coordinates(0, 0);
         return false;
     }
 }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        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);
                    }
                });
        }
Exemplo n.º 5
0
        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));
                }
            }
        }
Exemplo n.º 6
0
 public void Destroy(Coordinates coordinates)
 {
     this[coordinates] = Field.Detonated;
 }
Exemplo n.º 7
0
 public string this[Coordinates coordinates]
 {
     get { return this[coordinates.Row, coordinates.Col]; }
     set { this[coordinates.Row, coordinates.Col] = value; }
 }
Exemplo n.º 8
0
        public bool IsMine(Coordinates coordinates)
        {
            bool result = true;

            result = result && this[coordinates] != Field.Empty;
            result = result && this[coordinates] != Field.Detonated;

            return result;
        }
Exemplo n.º 9
0
        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;
        }