BRNSystems
440e0e2ecf
ships, abilities, ship pieces, map generator, ship placer separate pid writer to another module
78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
#constants
|
|
from ships_abilities import Ship, Ship_Piece
|
|
|
|
class Map_Cell:
|
|
def __init__(self, x:int, y:int, ship:Ship):
|
|
self.x = x
|
|
self.y = y
|
|
self.ship = ship
|
|
self.alive = True
|
|
self.hit = False
|
|
def hit(self):
|
|
self.hit = True
|
|
if self.ship is not None:
|
|
self.alive = self.ship.hit()
|
|
|
|
|
|
#generate maps
|
|
def generate_maps(maps:dict, main_map_size:list, aux_map_size:list):
|
|
maps["L"] = []
|
|
maps["La"] = []
|
|
maps["R"] = []
|
|
maps["Ra"] = []
|
|
for i in range(main_map_size[0]):
|
|
maps["L"].append([])
|
|
maps["R"].append([])
|
|
for j in range(main_map_size[1]):
|
|
maps["L"][i].append(Map_Cell(i, j, None))
|
|
maps["R"][i].append(Map_Cell(i, j, None))
|
|
for i in range(aux_map_size[0]):
|
|
maps["La"].append([])
|
|
maps["Ra"].append([])
|
|
for j in range(aux_map_size[1]):
|
|
maps["La"][i].append(Map_Cell(i, j, None))
|
|
maps["Ra"][i].append(Map_Cell(i, j, None))
|
|
|
|
def inside_map(x:int, y:int, map_name:str, maps):
|
|
if map_name == "R" or map_name == "L":
|
|
mapsize = [maps["L"].__len__(), maps["L"][0].__len__()]
|
|
elif map_name == "Ra" or map_name == "La":
|
|
mapsize = [maps["La"].__len__(), maps["La"][0].__len__()]
|
|
else:
|
|
raise Exception("Invalid map type")
|
|
if x < 0 or x >= mapsize[0] or y < 0 or y >= mapsize[1]:
|
|
return False
|
|
return True
|
|
|
|
def can_place_ship(x:int, y:int, map_name:str, maps):
|
|
if not inside_map(x, y, map_name, maps):
|
|
return False
|
|
if maps[map_name][y][x].ship is not None:
|
|
return False
|
|
return True
|
|
|
|
#place ship on map
|
|
def place_ship(ship_type:str, map_name:str, maps, x:int, y:int, orientation:str):
|
|
ship = Ship(ship_type, orientation, x, y)
|
|
for i in range(ship.size):
|
|
ship_coord_h = [x + i, y]
|
|
ship_coord_v = [x, y + i]
|
|
ship_coord_dl = [x + i, y + i]
|
|
ship_coord_dr = [x - i, y + i]
|
|
if orientation == "H":
|
|
ship_coord = ship_coord_h
|
|
elif orientation == "V":
|
|
ship_coord = ship_coord_v
|
|
elif orientation == "DL":
|
|
ship_coord = ship_coord_dl
|
|
elif orientation == "DR":
|
|
ship_coord = ship_coord_dr
|
|
else:
|
|
raise Exception("Invalid orientation")
|
|
|
|
if not can_place_ship(ship_coord[0], ship_coord[1], map_name, maps):
|
|
return False
|
|
else:
|
|
ship_piece = Ship_Piece(ship, ship_coord[0], ship_coord[1], i, orientation)
|
|
maps[map_name][ship_coord[0]][ship_coord[1]].ship = ship_piece
|
|
return True |