我正在做一艘战舰游戏。我创建了Ship类来给船只定位。
创建完类之后,我不得不创建所有实例,我想知道是否有一种方法可以使它自动化。
大多数程序都无关紧要,但是我留给它是为了防止它影响是否可以自动化。
import random
class Ship(object):
def __init__(self, length):
self.length = length
def direction(self):
vh = random.choice(['v','h'])
return vh
def location(self):
space = []
row = random.randint(0, 10-self.length)
column = random.randint(0, 10-self.length)
if self.direction == 'v':
for x in range(self.length):
space.append(f'{column}{row+x}')
else:
for x in range(self.length):
space.append(f'{column}{row+x}')
return space
ships_amount = {
'carrier' : 1,
'battleship' : 2,
'cruiser' : 3,
'destroyer' : 4
}
ships_length = {
'carrier' : 5,
'battleship' : 4,
'cruiser' : 3,
'destroyer' : 2
}
我想这样做:
carrier1 = Ship(ships_length['carrier'])
battleship1 = Ship(ships_length['battleship'])
battleship2 = Ship(ships_length['battleship'])
cruiser1 = Ship(ships_length['cruiser'])
cruiser2 = Ship(ships_length['cruiser'])
cruiser3 = Ship(ships_length['cruiser'])
destroyer1 = Ship(ships_length['destroyer'])
destroyer2 = Ship(ships_length['destroyer'])
destroyer3 = Ship(ships_length['destroyer'])
destroyer4 = Ship(ships_length['destroyer'])
但自动
答案 0 :(得分:2)
您可以遍历所需的船只并查询其长度以制成它们:
ships = []
for type_of_ship in ships_amount:
ships.append(Ship(ships_length[type_of_ship]))
甚至
ships = [Ship(ships_length[k]) for k in ships_amount]
(在第二个示例中,k
y是key
的简写,或现在在for循环中称为type_of_ship
的缩写)
这将为您提供每种类型的飞船之一。
这不会为您提供名为'carrier1'等的变量,但是您将能够对ships
中的每一项进行处理。
例如
for ship in ships:
print(ship.length)
要获得指定的数量或每种船只的数量,您需要在循环中添加其他船只。
通过遍历items()
,您可以获得一个键和一个值,尽管我们应有更好的名称,但我将它们称为k
和v
。
字典中的值告诉您有多少:
ships = []
for k, v in ships_amount.items():
ships.extend([Ship(ships_length[k]) for _ in range(v)])
这为您提供了您要求的10艘船。
答案 1 :(得分:1)
如果为所需的每种舰船模型创建舰船的子类,则可以将它们分组为舰队,然后直接在一行代码中创建舰队...
也许是这样的:
import random
class Ship: # this becomes an abstract class, not to be instanciated
# you could have it inherit from ABC (Abstract Base Class)
def __init__(self):
self.length = self.__class__.length
self.heading = None
self.set_heading()
self.location = None
self.set_location()
def set_heading(self):
self.heading = random.choice(['v','h'])
def set_location(self): # this method needs more work to prevent
# ships to occupy the same spot and overlap
space = []
row = random.randint(0, 10 - self.length)
column = random.randint(0, 10 - self.length)
if self.heading == 'v':
for c in range(self.length):
space.append((row, column + c))
elif self.heading == 'h':
for r in range(self.length):
space.append((row + r, column))
self.location = space
def __str__(self):
return f'{self.__class__.__name__} at {self.location}'
class AircraftCarrier(Ship): # every type of ship inherits from the base class Ship
length = 5 # Each class of ship can have its own specifications
# here, length, but it could be firepower, number of sailors, cannons, etc...
class BattleShip(Ship):
length = 4
class Cruiser(Ship):
length = 3
class Destroyer(Ship):
length = 2
class Fleet:
ships_number = {AircraftCarrier : 1,
BattleShip: 2,
Cruiser: 3,
Destroyer: 4}
def __init__(self):
self.ships = [ship() for ship, number in Fleet.ships_number.items()
for _ in range(number)]
def __str__(self):
return '\n'.join([str(ship) for ship in self.ships])
if __name__ == '__main__':
fleet = Fleet() # <-- the creation of the entire Fleet of Ships
print(fleet) # takes now one line of code now
(位置是随机分配的,并且每次运行都会有所不同。)
AircraftCarrier at [(1, 2), (2, 2), (3, 2), (4, 2), (5, 2)]
BattleShip at [(5, 3), (6, 3), (7, 3), (8, 3)]
BattleShip at [(5, 1), (6, 1), (7, 1), (8, 1)]
Cruiser at [(4, 7), (5, 7), (6, 7)]
Cruiser at [(0, 5), (0, 6), (0, 7)]
Cruiser at [(6, 6), (7, 6), (8, 6)]
Destroyer at [(4, 8), (5, 8)]
Destroyer at [(3, 5), (4, 5)]
Destroyer at [(1, 5), (1, 6)]
Destroyer at [(2, 1), (2, 2)]
添加新型船舶非常容易:创建一个继承于抽象基类Ship
的新类别,并将新船舶的数量添加到舰队组成中就足够了:
class Submarine(Ship):
length = 1
Fleet.ships_number[Submarine] = 5 # or add this entry directly in the class Fleet data
舰队现在有5艘潜水艇:
AircraftCarrier at [(4, 1), (5, 1), (6, 1), (7, 1), (8, 1)]
BattleShip at [(5, 5), (6, 5), (7, 5), (8, 5)]
BattleShip at [(0, 0), (1, 0), (2, 0), (3, 0)]
Cruiser at [(5, 2), (5, 3), (5, 4)]
Cruiser at [(2, 0), (3, 0), (4, 0)]
Cruiser at [(7, 7), (8, 7), (9, 7)]
Destroyer at [(4, 3), (5, 3)]
Destroyer at [(2, 1), (2, 2)]
Destroyer at [(0, 8), (1, 8)]
Destroyer at [(3, 6), (3, 7)]
Submarine at [(8, 8)]
Submarine at [(0, 7)]
Submarine at [(3, 4)]
Submarine at [(5, 9)]
Submarine at [(9, 3)]