ships,
abilities,
ship pieces,
map generator,
ship placer

separate pid writer to another module
This commit is contained in:
Bruno Rybársky 2022-01-19 18:07:16 +01:00
parent bad420e8ba
commit 440e0e2ecf
4 changed files with 168 additions and 14 deletions

23
main.py

@ -1,14 +1,9 @@
#import os
#always use absolute paths
import os
import time
#get pid
pid = os.getpid()
#write pid to file /tmp/battleship_server.pid
with open('/tmp/battleship_server.pid', 'w') as f:
f.write(str(pid))
print('Hello my(i \'m battleship server) pid is ' + str(pid))
while True:
time.sleep(2)
from pider import write_pid
from map import place_ship, generate_maps
#from ships_abilities import abilities, ships
main_map_size = [10, 10]
aux_map_size = [5, 5]
maps = {}
generate_maps(maps, main_map_size, aux_map_size)
place_ship("aircraft_carrier", "L", maps, 0, 0, "H")
write_pid()

78
map.py Normal file

@ -0,0 +1,78 @@
#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

8
pider.py Normal file

@ -0,0 +1,8 @@
#always use absolute paths
import os
def write_pid():
pid = os.getpid()
with open('/tmp/battleship_server.pid', 'w') as f:
f.write(str(pid))
print('Hello my(i \'m battleship server) pid is ' + str(pid))

73
ships_abilities.py Normal file

@ -0,0 +1,73 @@
ships = [
{"name": "aircraft_carrier", "size": 5, "lives": 1},
{"name": "bomber_ship", "size": 4, "lives": 1},
{"name": "sonar_ship", "size": 3, "lives": 1},
{"name": "submarine", "size": 2, "lives": 1},
{"name": "small_ship", "size": 1, "lives": 2}
]
abilities = [
{"name": "air strike", "description": "A strike that deals damage to all enemy's ships except submarines.", "ship_name": "aircraft_carrier", "round_period": 3, "ship": None},
{"name": "bomber", "description": "A bomber that allows you to shoot twice.", "round_period": 1, "ship_name": "bomber_ship", "ship": None},
{"name": "sonar", "description": "A sonar scan that reveals the location of enemy's ships.", "round_period": 5, "ship_name": "sonar_ship", "ship": None},
{"name": "torpedo", "description": "A torpedo that deals damage to all enemy's ships.", "round_period": 10, "ship_name": "submarine", "ship": None},
{"name": "spy", "description": "A spy that reveals the location of enemy's ships.", "round_period": 20, "ship_name": None, "ship": None},
{"name": "nuke", "description": "A nuclear strike that deals damage to all ships and terminates the game.", "round_period": 100, "ship_name": None, "ship": None},
]
#for all ships in abilities add ship_name to the description
def add_ship_name_to_abilities(abilities):
for ability in abilities:
if ability["ship_name"] is not None:
ability["description"] = ability["description"] + " It is fired from a " + ability["ship_name"].replace("_", " ") + "."
for ship in ships:
if ship["name"] == ability["ship_name"]:
ability["ship"] = ship
class Ability:
def __init__(self, name, description, round_period, ship_name):
self.name = name
self.description = description
self.round_period = round_period
self.ship_name = ship_name
self.current_round_period = 0
class Ship:
def __init__(self, type, direction, x, y):
self.type = type
self.direction = direction
self.x = x
self.y = y
for ship in ships:
if ship["name"] == type:
self.size = ship["size"]
self.lives = ship["lives"]
break
self.alive = True
self.pieces = []
class Ship_Piece:
def __init__(self, ship:Ship, x:int, y:int, index:int, orientation:str):
if ship == None:
raise Exception("Ship is None")
self.ship = ship
self.x = x
self.y = y
self.index = index
self.orientation = orientation
self.alive = False
self.ship.pieces.append(self)
def hit(self):
self.ship.lives -= 1
if self.ship.lives == 0:
self.ship.alive = False
ship_alive = False
for piece in self.ship.pieces:
if piece.alive:
ship_alive = True
break
self.ship.alive = ship_alive
return self.alive