TP3 placer
This commit is contained in:
parent
6e183d89de
commit
4a9f252e67
|
@ -195,41 +195,45 @@ def cree_plateau(l, h, l_nav):
|
|||
CU : les dimensions doivent permettre le placement de tous les navires
|
||||
"""
|
||||
|
||||
def est_case_vide(esp, pos):
|
||||
def cases_du_navire(nav, pos, disp):
|
||||
"""
|
||||
dict, tuple -> bool
|
||||
indique si la case aux coordonnées pos est incluse et vide
|
||||
dans l'espace maritime esp.
|
||||
tuple, tuple, str -> list
|
||||
fournit une liste des cases occupées par un navire nav
|
||||
aux coordonnées pos dans la disposition disp
|
||||
CU : disp = 'H' ou 'V'
|
||||
"""
|
||||
assert(disp in DISPOSITIONS), 'disp = \'H\' ou \'V\''
|
||||
|
||||
return pos[0] <= esp['larg'] and pos[0] > 0 and pos[1] > 0 and pos[1] <= esp['haut'] \
|
||||
and pos not in esp
|
||||
cases = list()
|
||||
if disp == 'H':
|
||||
for i in range(pos[0], pos[0]+nav[1]):
|
||||
cases.append((i, pos[1]))
|
||||
elif disp == 'V':
|
||||
for i in range(pos[1], pos[1]+nav[1]):
|
||||
cases.append((pos[0], i))
|
||||
return cases
|
||||
|
||||
def est_placable(esp, nav, pos, disp):
|
||||
"""
|
||||
dict, tuple, tuple, str -> bool
|
||||
|
||||
indique si le navire nav aux coordonnées pos peut être posé dans la
|
||||
disposition disp dans l'espace maritime esp
|
||||
CU : disp = 'H' ou 'V'
|
||||
"""
|
||||
|
||||
assert(disp in DISPOSITIONS), 'disp = \'H\' ou \'V\''
|
||||
|
||||
if disp == 'H':
|
||||
for i in range(pos[0], pos[0]+nav[1]):
|
||||
if not est_case_vide(esp, (i, pos[1])):
|
||||
return False
|
||||
elif disp == 'V':
|
||||
for i in range(pos[1], pos[1]+nav[1]):
|
||||
if not est_case_vide(esp, (pos[0], i)):
|
||||
return False
|
||||
for c in cases_du_navire(nav, pos, disp):
|
||||
if c[0] > esp['larg'] or c[0] <= 0 or c[1] <= 0 or c[1] > esp['haut'] or c in esp:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Tests
|
||||
# espTest = {'larg': 4, 'haut': 3, (1, 2): 'grand', (2, 2): 'grand', (4, 1): 'petit'}
|
||||
# print(est_placable(espTest, ('Poutre', 3), (1, 1), 'H'))
|
||||
# print(est_placable(espTest, ('Poutre', 3), (1, 1), 'V'))
|
||||
# print(est_placable(espTest, ('Poutre', 3), (3, 1), 'V'))
|
||||
# print(est_placable(espTest, ('Poutre', 3), (3, 2), 'V'))
|
||||
# print(est_placable(espTest, ('Poutre', 3), (1, 1), 'H')) # True
|
||||
# print(est_placable(espTest, ('Poutre', 3), (1, 1), 'V')) # False
|
||||
# print(est_placable(espTest, ('Poutre', 3), (3, 1), 'V')) # True
|
||||
# print(est_placable(espTest, ('Poutre', 3), (3, 2), 'V')) # False
|
||||
|
||||
def placer(esp, nav):
|
||||
"""
|
||||
|
@ -240,6 +244,21 @@ def placer(esp, nav):
|
|||
CU : il doit rester de la place
|
||||
"""
|
||||
|
||||
while True:
|
||||
pos = (randint(0, esp['larg']-1), randint(0, esp['haut']-1))
|
||||
disp = DISPOSITIONS[randint(0, 1)]
|
||||
if est_placable(esp, nav, pos, disp):
|
||||
for c in cases_du_navire(nav, pos, disp):
|
||||
esp[c] = nav[0]
|
||||
return
|
||||
|
||||
# Test
|
||||
# espTest2 = {'larg': 4, 'haut': 3}
|
||||
# placer(espTest2, ('grand', 2))
|
||||
# placer(espTest2, ('petit', 1))
|
||||
# print(espTest2) # Aléatoire
|
||||
|
||||
|
||||
|
||||
###############################################
|
||||
# Procédures de déroulement du jeu
|
||||
|
|
Reference in a new issue