/// <summary> /// This method is handling the movement of the tiles in-game. /// </summary> /// <exception cref="TilePositionOutOfRangeException"> /// Thrown when the position of the tile is a negative number or exceeds the scope of the matrix /// </exception> /// <exception cref="InvalidTileNeighbourException"> /// Thrown when two given tiles are not valid neighbours. /// </exception> public static List <ITile> MoveTiles(List <ITile> tiles, int tileValue) { if (tileValue < 0 || tileValue > 15) { throw new TilePositionOutOfRangeException(Messages.TileOutOfRangeExceptionMessage); } List <ITile> resultMatrix = tiles; ITile freeTile = tiles[GetFreeTilePosition(tiles)]; ITile tile = tiles[GetDestinationTilePosition(tiles, tileValue)]; bool areValidNeighbourTiles = MatrixGenerator.AreValidNeighbourTiles(freeTile, tile); if (!areValidNeighbourTiles) { throw new InvalidTileNeighbourException(Messages.InvalidTileNeighbourExceptionMessage); } int targetTilePosition = tile.Position; resultMatrix[targetTilePosition].Position = freeTile.Position; resultMatrix[freeTile.Position].Position = targetTilePosition; resultMatrix.Sort(); return(resultMatrix); }
public void InvalidNeighbourTilesTest() { IList <ITile> tiles = new List <ITile>(); for (int index = 1; index < 16; index++) { tiles.Add(new Tile(index.ToString(), index - 1)); } tiles.Add(new Tile(string.Empty, 15)); Assert.IsFalse(MatrixGenerator.AreValidNeighbourTiles(tiles[0], tiles[5])); }