[英]Battleship: How to automate instances creation?
我正在制作战舰游戏。 我创建了一个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'])
但自动化
您可以迭代所需的船只并查找它们的长度来制作它们:
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艘船。
如果您为所需的每个Ship模型创建一个子类Ship,您可以将它们分组到Fleet中,然后直接在一行代码中创建机群...
也许是这样的:
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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.