public async Task <(Room[, ], int?)> BuildMazeLayoutAsync(int size, Dimension dimension) { try { if (size <= 1) { return(null, null); } var layoutArray = new Room[size, size]; var enumerableListOfRoomTypes = await _roomTypeRepository.GetAllRoomTypesAsync(); var listOfRoomTypes = enumerableListOfRoomTypes.ToList(); //build the layout with rooms types other than treasure room //creating a list of all room types excluding the treasure room type to fill the initial layout var listOfRoomTypesWithoutTreasureRoom = listOfRoomTypes.Where(rType => (rType.BehaviourType != null && !rType.BehaviourType.IsTreasureThere) || rType.BehaviourType == null).ToArray(); CreateInitialLayout(listOfRoomTypesWithoutTreasureRoom, size, ref layoutArray); //use the row and column of the entry room passed as parameters to find the entry room cell from the //layout and then replace it with an empty room and use the id of the initial room for the new //room as well SetupStartRoom(listOfRoomTypes.Find(rType => rType.BehaviourType == null), dimension, ref layoutArray); //set the treasure room var entryRoomId = layoutArray[dimension.Row, dimension.Column].Id; var treasureRoomType = listOfRoomTypes.Find(rType => rType.BehaviourType != null && rType.BehaviourType.IsTreasureThere); SetupTreasureRoom(treasureRoomType, size, entryRoomId, ref layoutArray); return(layoutArray, entryRoomId); } catch (Exception) { return(null, null); } }