/// <summary> /// Generate a new Matrix /// </summary> /// <returns>Returns the matrix</returns> public static List<Tile> GenerateMatrix() { List<Tile> tilesMatrix = new List<Tile>(); int maxPosition = MatrixSize * MatrixSize - 1; for (int tilePosition = 0; tilePosition < maxPosition; tilePosition++) { string tileLabel = (tilePosition + 1).ToString(); Tile currentTile = new Tile(tileLabel, tilePosition); tilesMatrix.Add(currentTile); } Tile emptyTile = new Tile(string.Empty, maxPosition); tilesMatrix.Add(emptyTile); return tilesMatrix; }
/// <summary> /// Check if tile is a valid neighbour to empty tile. /// </summary> /// <param name="emptyTile">The empty tile in the matrix</param> /// <param name="currentTile">The tile that is checked if it is a valid neighbour</param> /// <returns>Returns boolean value</returns> public static bool AreValidNeighbours(Tile emptyTile, Tile currentTile) { int tilesDistance = emptyTile.Position - currentTile.Position; int tilesAbsoluteDistance = Math.Abs(tilesDistance); bool areValidHorizontalNeighbours = false; if (tilesAbsoluteDistance == HorizontalNeighbourDistance) { areValidHorizontalNeighbours = true; if (((currentTile.Position + 1) % MatrixSize == 1 && tilesDistance == -1) || ((currentTile.Position + 1) % MatrixSize == 0 && tilesDistance == 1)) { areValidHorizontalNeighbours = false; } } bool areValidVerticalNeighbours = false; if (tilesAbsoluteDistance == VerticalNeighbourDistance) { areValidVerticalNeighbours = true; } bool areValidNeigbours = areValidHorizontalNeighbours || areValidVerticalNeighbours; return areValidNeigbours; }