Commit e7762bc8 authored by Formation DIU's avatar Formation DIU
Browse files

Update

parent 3dffdc24
......@@ -16,28 +16,34 @@ class Grid():
"""
dim : tuple (width, height)
"""
self.__width, self.__height = dim
# Dico des coins
self.__WALL = {(0, 0) : NW_CORNER,
(dim[0]-1, 0) : NE_CORNER,
(0, dim[1]-1) : SW_CORNER,
(dim[0]-1, dim[1]-1) : SE_CORNER}
self.__WALL.update({(x, 0) : NORTH_WALL for x in range(1, dim[0]-1)})
self.__WALL.update({(dim[0]-1, y) : EAST_WALL for y in range(1, dim[1]-1)})
self.__WALL.update({(x, dim[1]-1) : SOUTH_WALL for x in range(1, dim[0]-1)})
self.__WALL.update({(0, y) : WEST_WALL for y in range(1, dim[1]-1)})
self.__width, self.__height = dim
# On créé la grille
self.__grid = self.__create_grid()
self.list_player = []
def get_grid_dimension(self):
"""
Retourne les dimensions de la grille sous forme d'un tuple
"""
return self.__width, self.__height
def __create_grid(self):
"""
Initialise une grille vide aux bonnes dimensions
"""
w, h = self.get_grid_dimension()
# Dico des coins
WALL = {(0, 0) : NW_CORNER,
(w-1, 0) : NE_CORNER,
(0, h-1) : SW_CORNER,
(w-1, h-1) : SE_CORNER}
WALL.update({(x, 0) : NORTH_WALL for x in range(1, w-1)}) # BORD NORD
WALL.update({(w-1, y) : EAST_WALL for y in range(1, h-1)}) # BORD EST
WALL.update({(x, h-1) : SOUTH_WALL for x in range(1, w-1)}) # BORD SUD
WALL.update({(0, y) : WEST_WALL for y in range(1, h-1)}) # BORD OUEST
grid = []
for y in range(h):
......@@ -46,7 +52,7 @@ class Grid():
# Si la case est un coin ou un bord
# On récupère le coin ou le bord associé avec le dico
try:
line.append(Case(self.__WALL[(x,y)]))
line.append(Case(WALL[(x,y)]))
except KeyError:
line.append(Case(NONE_WALL))
......@@ -54,22 +60,33 @@ class Grid():
return grid
def get_cell(self, coord):
"""
Accesseur d'une cellule
"""
x, y = coord
return self.__grid[y][x]
def set_case_wall(self, coord, wall):
"""
Création d'un mur
coord : tuple
wall : Mot binaire
"""
x, y = coord
# Si on doit placer un mur EST
if wall & EAST_WALL:
self.__grid[y][x].set_wall(EAST_WALL)
self.get_cell((x, y)).set_wall(EAST_WALL)
if x < self.__width-1: # Il y a aussi un mur à l'OUEST
self.__grid[y][x+1].set_wall(WEST_WALL)
self.get_cell((x+1, y)).set_wall(WEST_WALL)
# Si on doit placer un mur SUD
if wall & SOUTH_WALL:
self.__grid[y][x].set_wall(SOUTH_WALL)
self.get_cell((x, y)).set_wall(SOUTH_WALL)
if y < self.__height-1: # Il y a aussi un mur au NORD
self.__grid[y+1][x].set_wall(NORTH_WALL)
self.get_cell((x, y+1)).set_wall(NORTH_WALL)
# Un mur au nord = un mur au sud de la case du dessus
# Un mur à l'ouest = un mur à l'est de la case de gauche
if 0 < x:
......@@ -78,159 +95,104 @@ class Grid():
if 0 < y:
if wall & NORTH_WALL:
self.set_case_wall((x, y-1), SOUTH_WALL)
def add_player(self, coord, num):
x, y = coord
self.__grid[y][x].set_player(num)
self.list_player.append(coord)
def set_final_case(self, coord):
x, y = coord
self.__final = coord
self.__grid[y][x].set_final()
def from_file(filename):
dico_temp = {'N' : NORTH_WALL, 'E' : EAST_WALL,
'S' : SOUTH_WALL, 'W' : WEST_WALL}
with open(filename) as f:
buff = [line[:-1] for line in f.readlines() if line[0] != '#']
dim = tuple([int(el) for el in buff[0].split(',')])
buff = buff[1:] #On retire les dimensions
grid = Grid(dim)
coord_final = tuple([int(el) for el in buff[0].split(',')])
buff = buff[1:] # On retire les coordonnées de la case finale
grid.set_final_case(coord_final)
number_of_player = int(buff[0])
buff = buff[1:] # On retire le nombre de joueur
for _ in range(number_of_player):
coord_player = tuple([int(el) for el in buff[0].split(',')])
buff = buff[1:] # On retire les coordonnées de chaque joueur
grid.add_player(coord_player, _)
while buff != []:
wall = buff[0].split(',')
coord_wall = tuple([int(el) for el in wall[:2]])
grid.set_case_wall(coord_wall, dico_temp[wall[2]])
buff = buff[1:]
return grid
def move_player_north(self, num):
coord = self.list_player[num]
x, y = coord
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())
if not(self.__grid[y][x].get_wall() & NORTH_WALL) and self.__grid[y-1][x].is_free():
if num != 0: #Si on ne joue avec le joueur principal
if not(self.__grid[y-1][x].is_final()):
self.__grid[y][x].set_player(' ')
self.__grid[y-1][x].set_player(num)
self.list_player[num] = (x, y-1)
return True
else:
return False
else:
self.__grid[y][x].set_player(' ')
self.__grid[y-1][x].set_player(num)
self.list_player[num] = (x, y-1)
return True
else:
return False
# On ajoute le joueur à la liste
self.list_player += (p,)
def move_player_east(self, num):
coord = self.list_player[num]
x, y = coord
if not(self.__grid[y][x].get_wall() & EAST_WALL) and self.__grid[y][x+1].is_free():
if num != 0: #Si on ne joue avec le joueur principal
if not(self.__grid[y][x+1].is_final()):
self.__grid[y][x].set_player(' ')
self.__grid[y][x+1].set_player(num)
self.list_player[num] = (x+1, y)
return True
else:
return False
else:
self.__grid[y][x].set_player(' ')
self.__grid[y][x+1].set_player(num)
self.list_player[num] = (x+1, y)
return True
else:
return False
def set_config(self, list_player):
"""
list_player : tuple de coordonnées
"""
for p in list_player:
self.add_player(p)
def get_config(self):
return tuple(self.list_player)
def move_player_south(self, num):
def set_final_case(self, coord):
self.get_cell(coord).set_final()
"""
Gestion des déplacements.
"""
def move_player_one_time_by_direction(self, num, direction):
"""
Déplace d'une seule case le joueur dans la bonne direction
"""
coord = self.list_player[num]
x, y = coord
if not(self.__grid[y][x].get_wall() & SOUTH_WALL) and self.__grid[y+1][x].is_free():
if num != 0: #Si on ne joue avec le joueur principal
if not(self.__grid[y+1][x].is_final()):
self.__grid[y][x].set_player(' ')
self.__grid[y+1][x].set_player(num)
self.list_player[num] = (x, y+1)
return True
else:
return False
else:
self.__grid[y][x].set_player(' ')
self.__grid[y+1][x].set_player(num)
self.list_player[num] = (x, y+1)
return True
else:
return False
dico_wall = {'N' : NORTH_WALL, 'E' : EAST_WALL, 'S' : SOUTH_WALL, 'W' : WEST_WALL}
dico_new_coord = {'N' : (x,y-1), 'E' : (x+1,y), 'S' : (x,y+1), 'W' : (x-1,y)}
def move_player_west(self, num):
coord = self.list_player[num]
x, y = coord
new_coord = dico_new_coord[direction]
if not(self.__grid[y][x].get_wall() & WEST_WALL) and self.__grid[y][x-1].is_free():
if num != 0: #Si on ne joue avec le joueur principal
if not(self.__grid[y][x-1].is_final()):
self.__grid[y][x].set_player(' ')
self.__grid[y][x-1].set_player(num)
self.list_player[num] = (x-1, y)
if not(self.get_cell(coord).get_wall() & dico_wall[direction]) and self.get_cell(new_coord).is_free():
if num != 0:
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
return True
else:
return False
else:
self.__grid[y][x].set_player(' ')
self.__grid[y][x-1].set_player(num)
self.list_player[num] = (x-1, y)
self.get_cell(coord).set_player(' ')
self.get_cell(new_coord).set_player(num)
self.list_player[num] = new_coord
return True
else:
return False
def move_player(self, num, direction):
move = {'N' : self.move_player_north, 'E' : self.move_player_east, 'S' : self.move_player_south, 'W' : self.move_player_west}
"""
Déplace le joueur sur la glace
"""
bPossible = True
while bPossible:
bPossible = move[direction](num)
bPossible = self.move_player_one_time_by_direction(num, direction)
if num == 0 :
x, y = self.list_player[num]
if self.get_cell((x,y)).is_final():
bPossible = False
def move_player_at_coord(self, num, coord):
"""
Utile pour le undo
"""
x_act, y_act = self.list_player[num]
x, y = coord
self.list_player[num] = coord
self.__grid[y_act][x_act].set_player(' ')
self.__grid[y][x].set_player(num)
self.get_cell((x_act,y_act)).set_player(' ')
self.get_cell((x,y)).set_player(num)
def is_over(self):
"""
A-t-on gagné ?
"""
x, y = self.list_player[0]
return self.__grid[y][x].is_final()
return self.get_cell((x,y)).is_final()
def __str__(self):
"""
Affichage d'une grille
"""
grid_to_str = '+'+'-+'*self.__width+'\n' # Ligne du haut
w, h = self.get_grid_dimension()
......@@ -240,23 +202,23 @@ class Grid():
grid_to_str += '|' # Nouvelle ligne
for x in range(w):
if self.__grid[y][x].get_wall() & EAST_WALL:
grid_to_str += '{}|'.format(self.__grid[y][x].get_player())
if self.get_cell((x,y)).get_wall() & EAST_WALL:
grid_to_str += '{}|'.format(self.get_cell((x,y)).get_player())
else:
grid_to_str += '{} '.format(self.__grid[y][x].get_player())
grid_to_str += '{} '.format(self.get_cell((x,y)).get_player())
grid_to_str += '\n+'
for x in range(w):
bMod = False
if self.__grid[y][x].get_wall() & SOUTH_WALL:
if self.get_cell((x,y)).get_wall() & SOUTH_WALL:
grid_to_str = grid_to_str[:-1] + '+-+'
elif self.__grid[y][x].get_wall() & EAST_WALL:
elif self.get_cell((x,y)).get_wall() & EAST_WALL:
grid_to_str += ' +'
else:
if y < self.__height-1 and x != w-1:
if self.__grid[y+1][x].get_wall() & EAST_WALL:
if y < self.__height-1:
if self.get_cell((x,y+1)).get_wall() & EAST_WALL:
grid_to_str += ' +'
bMod = True
......@@ -266,17 +228,48 @@ class Grid():
grid_to_str += '\n'
return grid_to_str
def from_file(filename):
"""
Charger une grille depuis un fichier
"""
dico_temp = {'N' : NORTH_WALL, 'E' : EAST_WALL,
'S' : SOUTH_WALL, 'W' : WEST_WALL}
# g = Grid((16,4))
# g.set_player((5,1), 0)
# g.set_player((8,2), 1)
# g.set_player((0,3), 2)
# g.set_player((15,2), 4)
# g.set_case_wall((0,1), NORTH_WALL)
# g.set_case_wall((2,1), EAST_WALL)
# g.set_case_wall((10, 2), SOUTH_WALL)
# g.set_case_wall((5,3), WEST_WALL)
# g.set_case_wall((5,1), NE_CORNER)
# g.set_case_wall((15,2), SW_CORNER)
# g.set_case_wall((3,2), NW_CORNER)
# print(g)
\ No newline at end of file
with open(filename) as f:
# On retire les lignes avec des commentaires
buff = [line[:-1] for line in f.readlines() if line[0] != '#']
# Dimension de la grille
dim = tuple([int(el) for el in buff[0].split(',')])
buff = buff[1:] # On retire les dimensions
grid = Grid(dim)
# Case finale
coord_final = tuple([int(el) for el in buff[0].split(',')])
buff = buff[1:] # On retire les coordonnées de la case finale
grid.set_final_case(coord_final)
# Nombre de joueurs
number_of_player = int(buff[0])
buff = buff[1:] # On retire le nombre de joueur
list_player = ()
for _ in range(number_of_player):
coord_player = tuple([int(el) for el in buff[0].split(',')])
buff = buff[1:] # On retire les coordonnées de chaque joueur
list_player = list_player + (coord_player,)
# On ajoute les joueurs
grid.set_config(list_player)
# Reste les murs
while buff != []:
wall = buff[0].split(',')
coord_wall = tuple([int(el) for el in wall[:2]])
grid.set_case_wall(coord_wall, dico_temp[wall[2]])
buff = buff[1:]
return grid
\ No newline at end of file
......@@ -5,12 +5,14 @@ class IceWalker():
def __init__(self, grid):
self.grid = grid
self.played = [] # PILE pour la gestion des courps
self.score = 0
self.back = 0
def undo(self):
if self.played != []:
coup = self.played.pop()
self.grid.move_player_at_coord(coup[0], coup[1])
self.back += 1
def play(self, name = 'David'):
self.name_player = name
......@@ -38,7 +40,9 @@ class IceWalker():
self.played.append((num, self.grid.list_player[num]))
self.grid.move_player(num, direction)
self.score += 1
if bContinue:
print(self.grid)
print('Bravoooooooooo !!!')
print('Vous avez réussi en réalisant {} coups et {} retour(s) arrière.'.format(self.score, self.back))
class Player():
def __init__(self, num):
self.num = num
self.pos = None
def get_num(self):
return self.num
def set_pos(self, pos):
self.pos = pos
def get_pos(self):
return self.pos
\ No newline at end of file
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