26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
"""
|
|
Represents a set of maps in the game. At initialization, it loads the desired maps.
|
|
It provides a method to get a number of maps with the difficulty level as close as possible to the desired difficulty level.
|
|
"""
|
|
|
|
from game.mapReader import MapReader
|
|
|
|
class MapSet:
|
|
def __init__(self, mapReader: MapReader, mapNames: list):
|
|
self.mapReader = mapReader
|
|
self.maps = [self.mapReader.read_map(name) for name in mapNames]
|
|
|
|
def get_map(self, index: int):
|
|
if index < 0 or index >= len(self.maps):
|
|
raise IndexError("Map index out of range.")
|
|
return self.maps[index]
|
|
|
|
def get_maps_by_difficulty(self, desired_difficulty: int, num_maps: int):
|
|
"""
|
|
Returns a list of maps with difficulty levels as close as possible to the desired difficulty level.
|
|
If there are not enough maps, it returns all available maps.
|
|
"""
|
|
if len(self.maps) == 0:
|
|
raise ValueError("No maps available in the MapSet.")
|
|
sorted_maps = sorted(self.maps, key=lambda m: abs(m.difficulty - desired_difficulty))
|
|
return sorted_maps[:num_maps] |