Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check adjacent indices in a multidimensional array having into account the borders

I did this so I can know how many asterisks appear in the adjacent squares.

private int CheckAdjacents(Coordinate cord)
{
    List<Coordinate> coordinates = new List<Coordinate>()
    {
        new Coordinate(cord.X - 1, cord.Y - 1), 
        new Coordinate(cord.X, cord.Y-1),
        new Coordinate(cord.X + 1, cord.Y -1),  
        new Coordinate(cord.X + 1, cord.Y),
        new Coordinate(cord.X + 1, cord.Y + 1), 
        new Coordinate(cord.X, cord.Y + 1),
        new Coordinate(cord.X - 1, cord.Y + 1), 
        new Coordinate(cord.X - 1, cord.Y)
    };

    return coordinates.Count(x => _matrix.At(x).Value == '*');
}

The thing here is that obviously it returns an exception because is checking indexes that wouldn’t be checked. What’d be the best way to skip those kind of indexes? Using a try/catch could be kinda tricky? Thanks!

EDIT:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Matrix class

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace MineSweeper
{
    public record Coordinate (int X, int Y);
    public record Size(int M, int N);

    public class Matrix
    {
        private readonly Size _size;
        private readonly Cell[,] _matrix;
        private const char InitValue = '.';

        public Matrix(Size size)
        {
            _size = size;
            _matrix = new Cell[size.M, size.N];
            Initialize();
        }

        private void Initialize()
        { 
            for (int m = 0; m < _size.M; m++)
                for (int n = 0; n < _size.N; n++)
                    _matrix[m, n] = new Cell(InitValue);
        }

        public Size GetSize()
            => _size;

        public Cell At(Coordinate coordinate) 
            => _matrix[coordinate.X, coordinate.Y];
        
        public void SetMine(Coordinate coordinate) 
            => _matrix[coordinate.X, coordinate.Y] = new Cell('*');

        public void ChangeValue(Coordinate coordinate, char value)
            => _matrix[coordinate.X, coordinate.Y] = new Cell(value);
        
        public Cell Open(Coordinate coordinate)
            => _matrix[coordinate.X, coordinate.Y];

        public IEnumerable ToList()
            => _matrix.Cast<Cell>().ToList();

        private string CellsAsString()
            => string.Concat(_matrix.OfType<Cell>().Select(c => c.Value));
        
        public override bool Equals(object other)
            => this.CellsAsString().Equals((other as Matrix)?.CellsAsString());
        
        public override int GetHashCode()
            => this.CellsAsString().GetHashCode();
    }
}

>Solution :

You could add a method to the Matrix class, that returns true if both x and y are within their allowed ranges (0 <= x < M, 0 <= y < N) for a given Coordinate:

public bool IsValid(Coordinate coordinate) =>
         0 <= coordinate.X && coordinate.X < _size.M &&
         0 <= coordinate.Y && coordinate.Y < _size.N;

Then you can count only the valid cells:

return coordinates.Count(x => _matrix.IsValid(x) && _matrix.At(x).Value == '*');

Additionally, it makes sense to use this method for validation purpose in all other public methods of the Matrix class accepting Coordinate as an argument.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading