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

Update

parent 028e93d0
......@@ -39,6 +39,9 @@ class Case():
self.final = True
self.player = 'X'
def is_free(self):
return self.player == ' ' or self.player == 'X'
def is_final(self):
return self.final
......
......@@ -31,6 +31,7 @@ class Grid():
# On créé la grille
self.__grid = self.__create_grid()
self.list_player = []
def get_grid_dimension(self):
return self.__width, self.__height
......@@ -56,7 +57,18 @@ class Grid():
def set_case_wall(self, coord, wall):
x, y = coord
self.__grid[y][x].set_wall(wall)
# Si on doit placer un mur EST
if wall & EAST_WALL:
self.__grid[y][x].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)
# Si on doit placer un mur SUD
if wall & SOUTH_WALL:
self.__grid[y][x].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)
# 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
......@@ -67,14 +79,16 @@ class Grid():
if wall & NORTH_WALL:
self.set_case_wall((x, y-1), SOUTH_WALL)
def set_player(self, coord, num):
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):
......@@ -99,7 +113,7 @@ class Grid():
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.set_player(coord_player, _)
grid.add_player(coord_player, _)
while buff != []:
wall = buff[0].split(',')
......@@ -109,6 +123,113 @@ class Grid():
return grid
def move_player_north(self, num):
coord = self.list_player[num]
x, y = coord
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
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 move_player_south(self, num):
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
def move_player_west(self, num):
coord = self.list_player[num]
x, y = coord
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)
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 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}
bPossible = True
while bPossible:
bPossible = move[direction](num)
def move_player_at_coord(self, num, coord):
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)
def is_over(self):
x, y = self.list_player[0]
return self.__grid[y][x].is_final()
def __str__(self):
grid_to_str = '+'+'-+'*self.__width+'\n' # Ligne du haut
......@@ -158,7 +279,4 @@ class Grid():
# 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)
g = Grid.from_file('datas/grid06.txt')
print(g)
\ No newline at end of file
# print(g)
\ No newline at end of file
from Grid import Grid
class IceWalker():
def __init__(self, grid):
self.grid = grid
self.played = [] # PILE pour la gestion des courps
def undo(self):
if self.played != []:
anti_coup = {'N' : 'S', 'E' : 'W', 'S' : 'N', 'W' : 'E'}
coup = self.played.pop()
self.grid.move_player_at_coord(coup[0], anti_coup[coup[1]])
def play(self, name = 'David'):
self.name_player = name
bContinue = True
while bContinue and not(self.grid.is_over()):
print(self.grid)
if len(self.played) > 0:
txt = input("{}, entrez votre mouvement 'num,direction', 'undo', ou 'q' (quit) : ".format(self.name_player))
else:
txt = input("{}, entrez votre mouvement 'num,direction' ou 'q' (quit) : ".format(self.name_player))
if txt == 'q':
bContinue = False
elif txt == 'undo':
self.undo()
else:
buff = txt.split(',')
num = int(buff[0])
direction = buff[1]
# On sauvegarde les anciennes coordonnées du joueur
self.played.append((num, self.grid.list_player[num]))
self.grid.move_player(num, direction)
if bContinue:
print(self.grid)
print('Bravoooooooooo !!!')
\ No newline at end of file
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