TP3 est_placable

This commit is contained in:
Geoffrey Frogeye 2015-02-13 18:56:03 +01:00
parent d548aaef10
commit 6e183d89de

View file

@ -195,6 +195,15 @@ def cree_plateau(l, h, l_nav):
CU : les dimensions doivent permettre le placement de tous les navires
"""
def est_case_vide(esp, pos):
"""
dict, tuple -> bool
indique si la case aux coordonnées pos est incluse et vide
dans l'espace maritime esp.
"""
return pos[0] <= esp['larg'] and pos[0] > 0 and pos[1] > 0 and pos[1] <= esp['haut'] \
and pos not in esp
def est_placable(esp, nav, pos, disp):
"""
@ -203,11 +212,24 @@ def est_placable(esp, nav, pos, disp):
CU : disp = 'H' ou 'V'
"""
assert(disp == 'H' or disp == 'V'), 'disp = \'H\' ou \'V\''
assert(disp in DISPOSITIONS), 'disp = \'H\' ou \'V\''
if pos[0] > esp['larg'] or pos[0] < 0 or pos[1] < 0 or pos[1] > esp['haut']:
return False
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
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'))
def placer(esp, nav):
"""