Commit 9c6a2e37 authored by Formation DIU's avatar Formation DIU
Browse files

Update

parent e7762bc8
......@@ -19,7 +19,7 @@ class Grid():
self.__width, self.__height = dim
# On créé la grille
self.__grid = self.__create_grid()
self.list_player = []
self.list_player = ()
def get_grid_dimension(self):
"""
......@@ -80,7 +80,7 @@ class Grid():
self.get_cell((x, y)).set_wall(EAST_WALL)
if x < self.__width-1: # Il y a aussi un mur à l'OUEST
self.get_cell((x+1, y)).set_wall(WEST_WALL)
w, h = self.get_grid_dimension()
# Si on doit placer un mur SUD
if wall & SOUTH_WALL:
self.get_cell((x, y)).set_wall(SOUTH_WALL)
......@@ -98,30 +98,34 @@ class Grid():
def get_number_of_player(self):
return len(self.list_player)
def add_player(self, p):
"""
Met à jour la liste des joueurs
p : tuple de coordonnées (représentant le joueur)
"""
# On met à jour la cellule correspondante
self.get_cell(p).set_player(self.get_number_of_player())
# On ajoute le joueur à la liste
self.list_player += (p,)
def set_config(self, list_player):
"""
list_player : tuple de coordonnées
"""
for p in list_player:
self.add_player(p)
self.list_player = list_player
num = 0
for p in list_player:
self.get_cell(p).set_player(num)
num += 1
def set_player_pos(self, num, coord):
self.list_player = self.list_player[:num] + (coord, ) + self.list_player[num+1:]
def get_config(self):
return tuple(self.list_player)
return self.list_player
def set_final_case(self, coord):
self.get_cell(coord).set_final()
def get_final_case(self):
w, h = self.get_grid_dimension()
for y in range(h):
for x in range(w):
if self.get_cell((x,y)).is_final():
return (x,y)
"""
Gestion des déplacements.
......@@ -143,14 +147,14 @@ class Grid():
if not(self.get_cell(new_coord).is_final()):
self.get_cell(coord).set_player(' ')
self.get_cell(new_coord).set_player(num)
self.list_player[num] = new_coord
self.set_player_pos(num, new_coord)
return True
else:
return False
else:
self.get_cell(coord).set_player(' ')
self.get_cell(new_coord).set_player(num)
self.list_player[num] = new_coord
self.set_player_pos(num, new_coord)
return True
else:
return False
......@@ -176,7 +180,7 @@ class Grid():
x_act, y_act = self.list_player[num]
x, y = coord
self.list_player[num] = coord
self.set_player_pos(num, coord)
self.get_cell((x_act,y_act)).set_player(' ')
self.get_cell((x,y)).set_player(num)
......@@ -202,10 +206,15 @@ class Grid():
grid_to_str += '|' # Nouvelle ligne
for x in range(w):
if (x,y) in self.list_player:
num = self.list_player.index((x,y))
else:
num = ' '
if self.get_cell((x,y)).get_wall() & EAST_WALL:
grid_to_str += '{}|'.format(self.get_cell((x,y)).get_player())
grid_to_str += '{}|'.format(num)
else:
grid_to_str += '{} '.format(self.get_cell((x,y)).get_player())
grid_to_str += '{} '.format(num)
grid_to_str += '\n+'
......
from Grid import Grid
from Case import ALL_WALL, NORTH_WALL, EAST_WALL, SOUTH_WALL, WEST_WALL
class IceWalker():
......@@ -46,3 +47,53 @@ class IceWalker():
print(self.grid)
print('Bravoooooooooo !!!')
print('Vous avez réussi en réalisant {} coups et {} retour(s) arrière.'.format(self.score, self.back))
"""
Pour la résolution automatique
"""
def get_possible_direction(self):
"""
Retourne une liste du type
[(0, 'N'), (0, 'E'), ..., (3, 'E')]
"""
res = []
wall = [NORTH_WALL, EAST_WALL, SOUTH_WALL, WEST_WALL]
player = self.grid.get_config()
num = 0
for p in player:
cell = self.grid.get_cell(p)
free = ALL_WALL ^ cell.get_wall() # On récupère les possibilités de mouvement
for m in wall:
if free & m:
res.append((num, m))
num += 1
return res
def play_auto(self):
depart = self.grid.get_config()
deja_vu = {depart : None}
a_traiter = self.get_possible_direction()
resolu = False
while a_traiter != [] and not resolu:
s = a_traiter[0]
self.grid.move_player(s[0], s[1])
if self.grid.is_over():
resolu = True
else:
voisins = [self.grid.move_player(p[0], p[1]).get_config() for p in self.get_possible_direction()]
for v in voisins:
if v not in deja_vu:
deja_vu[v] = s
a_traiter = a_traiter[1:]+v
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment