-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Sujet ariane participant
- Loading branch information
Showing
6 changed files
with
333 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import tkinter as tk | ||
|
||
SQUARE_SIZE = 20 | ||
COLORS = { | ||
"x": "black", | ||
"-": "white", | ||
"o": "green", | ||
".": "yellow", | ||
"P": "blue" | ||
} | ||
|
||
|
||
def load_from_file(path) -> (list[list[str]], int, int): | ||
map: list[list[str]] = [] | ||
|
||
with open(path) as file: | ||
for line in file.readlines(): | ||
map.append(list(line.rstrip())) | ||
for i, row in enumerate(map): | ||
for j, char in enumerate(row): | ||
if char == "-": | ||
return map, i, j | ||
return None, -1, -1 | ||
|
||
|
||
def display_grid(canvas, map, PosY: int, PosX: int): | ||
for i in range(len(map)): | ||
for j in range(len(map[0])): | ||
if PosY == i and PosX == j: | ||
color = COLORS["P"] | ||
else: | ||
color = COLORS[map[i][j]] | ||
x0 = j * SQUARE_SIZE | ||
y0 = i * SQUARE_SIZE | ||
x1 = x0 + SQUARE_SIZE | ||
y1 = y0 + SQUARE_SIZE | ||
canvas.create_rectangle(x0, y0, x1, y1, fill=color) | ||
canvas.update() | ||
|
||
|
||
def update_grid(canvas, X, Y, color): | ||
x0 = X * SQUARE_SIZE | ||
y0 = Y * SQUARE_SIZE | ||
x1 = x0 + SQUARE_SIZE | ||
y1 = y0 + SQUARE_SIZE | ||
|
||
canvas.create_rectangle(x0, y0, x1, y1, fill=color) | ||
canvas.update() | ||
|
||
|
||
def init_graphics(map: list[list[str]], PosY, PosX): | ||
width = len(map[0]) * SQUARE_SIZE | ||
height = len(map) * SQUARE_SIZE | ||
window = tk.Tk() | ||
canvas = tk.Canvas(window, width=width, height=height) | ||
|
||
window.title("Participant: Fil d'Ariane") | ||
window.geometry(f"{width}x{height}") | ||
canvas.pack() | ||
display_grid(canvas, map, PosY, PosX) | ||
return window, canvas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,105 @@ | ||
from tkinter import * | ||
from time import sleep | ||
import cc_maze | ||
import sys | ||
|
||
incr = 2 | ||
size = incr * 10 | ||
posX = 1 | ||
posY = 1 | ||
map = [[]] * 100 | ||
DELAY = 0.08 # in seconds | ||
|
||
for value in range(0, 100): | ||
map[value] = [0] * 100 | ||
if len(sys.argv) != 2: | ||
print("Invalid arguments:\nPlease enter: python3 dedale.py <map>") | ||
exit(1) | ||
|
||
def checkered(canvas, line_distance): | ||
# vertical lines at an interval of "line_distance" pixel | ||
for x in range(line_distance,canvas_width,line_distance): | ||
canvas.create_line(x * (incr), 0, x * (incr), canvas_height, fill="#476042") | ||
# horizontal lines at an interval of "line_distance" pixel | ||
for y in range(line_distance,canvas_height,line_distance): | ||
canvas.create_line(0, y * (incr), canvas_width, y * (incr), fill="#476042") | ||
map, PosY, PosX = cc_maze.load_from_file(sys.argv[1]) | ||
if map is None: | ||
print("No possible starting point found.") | ||
exit(1) | ||
|
||
def right(): | ||
global posX | ||
global posY | ||
if (map[posY][posX + 1] == 1): | ||
return | ||
setACaseXY(posX, posY, 'grey') | ||
map[posY][posX] = -1 | ||
posX += 1 | ||
setACaseXY(posX, posY, 'yellow') | ||
if (map[posY][posX] == 2): | ||
exit(0) | ||
|
||
def left(): | ||
global posX | ||
global posY | ||
if (map[posY][posX - 1] == 1): | ||
return | ||
setACaseXY(posX, posY, 'grey') | ||
map[posY][posX] = -1 | ||
posX -= 1 | ||
setACaseXY(posX, posY, 'yellow') | ||
if (map[posY][posX] == 2): | ||
exit(0) | ||
|
||
# Movement | ||
def up(): | ||
global posY | ||
global posX | ||
if (map[posY - 1][posX] == 1): | ||
return | ||
setACaseXY(posX, posY, 'grey') | ||
map[posY][posX] = -1 | ||
posY -= 1 | ||
|
||
setACaseXY(posX, posY, 'yellow') | ||
if (map[posY][posX] == 2): | ||
exit(0) | ||
|
||
def down(): | ||
global posY | ||
global posX | ||
if (map[posY + 1][posX] == 1): | ||
return | ||
setACaseXY(posX, posY, 'grey') | ||
map[posY][posX] = -1 | ||
posY += 1 | ||
setACaseXY(posX, posY, 'yellow') | ||
if (map[posY][posX] == 2): | ||
exit(0) | ||
global canvas, map, PosY, PosX | ||
if PosY != 0 and map[PosY - 1][PosX] != "x": | ||
cc_maze.update_grid(canvas, PosX, PosY, "yellow") | ||
map[PosY][PosX] = '.' | ||
PosY -= 1 | ||
cc_maze.update_grid(canvas, PosX, PosY, "blue") | ||
sleep(DELAY) | ||
|
||
|
||
path = [] | ||
|
||
def algo(value): | ||
if (map[posY][posX + 1] == 0 or map[posY][posX + 1] == 2): | ||
right() | ||
path.append("right") | ||
elif (map[posY + 1][posX] == 0 or map[posY + 1][posX] == 2): | ||
down() | ||
path.append("down") | ||
elif (map[posY][posX - 1] == 0 or map[posY][posX - 1] == 2): | ||
left() | ||
path.append("left") | ||
elif (map[posY - 1][posX] == 0 or map[posY - 1][posX] == 2): | ||
up() | ||
path.append("up") | ||
else: | ||
if (path[-1] == "up"): | ||
down() | ||
elif (path[-1] == "left"): | ||
right() | ||
elif (path[-1] == "down"): | ||
up() | ||
elif (path[-1] == "right"): | ||
left() | ||
path.pop() | ||
|
||
|
||
def setACaseXY(X, Y, color): | ||
points = [X * size, Y * size, size * (X + 1), Y * size, size * (X + 1), size * (Y + 1), X * size, size * (Y + 1)] | ||
w.create_polygon(points, outline="#476042", fill=color, width=4) | ||
def down(): | ||
global canvas, map, PosY, PosX | ||
if PosY != len(map) - 1 and map[PosY + 1][PosX] != "x": | ||
cc_maze.update_grid(canvas, PosX, PosY, "yellow") | ||
map[PosY][PosX] = '.' | ||
PosY += 1 | ||
cc_maze.update_grid(canvas, PosX, PosY, "blue") | ||
sleep(DELAY) | ||
|
||
|
||
master = Tk() | ||
master.title("Le Fil d'Ariane") | ||
canvas_width = 1280 | ||
canvas_height = 620 | ||
w = Canvas(master, | ||
width=canvas_width, | ||
height=canvas_height) | ||
def left(): | ||
global canvas, map, PosY, PosX | ||
if PosX != 0 and map[PosY][PosX - 1] != "x": | ||
cc_maze.update_grid(canvas, PosX, PosY, "yellow") | ||
map[PosY][PosX] = '.' | ||
PosX -= 1 | ||
cc_maze.update_grid(canvas, PosX, PosY, "blue") | ||
sleep(DELAY) | ||
|
||
|
||
def right(): | ||
global canvas, map, PosY, PosX | ||
if PosX != len(map[PosY]) - 1 and map[PosY][PosX + 1] != "x": | ||
cc_maze.update_grid(canvas, PosX, PosY, "yellow") | ||
map[PosY][PosX] = '.' | ||
PosX += 1 | ||
cc_maze.update_grid(canvas, PosX, PosY, "blue") | ||
sleep(DELAY) | ||
|
||
|
||
filepath = 'map.txt' | ||
with open(filepath) as fp: | ||
line = fp.readline() | ||
cnt = 0 | ||
while line: | ||
x = line.find('x') | ||
while x != -1: | ||
setACaseXY(x, cnt, 'red') | ||
map[cnt][x] = 1 | ||
x = line.find('x', x + 1) | ||
x = line.find('o') | ||
if (x != -1): | ||
setACaseXY(x, cnt, 'green') | ||
map[cnt][x] = 2 | ||
line = fp.readline() | ||
cnt += 1 | ||
path = [] | ||
|
||
w.pack() | ||
|
||
for value in range(0, 1000): | ||
master.after(value * 100, algo, value) | ||
def algorithm(map): | ||
# Votre algorithme va ici. | ||
# Vous pouvez utiliser les fonctions de mouvement top, down, right, left. | ||
# Ces fonctions intéragissent directement avec l'affichage. | ||
# Une case déjà visitée sera transformée en '.' et affichée | ||
# en jaune sur l'écran. | ||
# Good luck! | ||
|
||
checkered(w,10) | ||
map_width = len(map[0]) | ||
map_height = len(map) | ||
|
||
mainloop() | ||
while map[PosY][PosX] != 'o': | ||
print(map) | ||
if PosX + 1 < map_width and (map[PosY][PosX + 1] == '-' | ||
or map[PosY][PosX + 1] == 'o'): | ||
path.append("right") | ||
right() | ||
elif PosY + 1 < map_height and (map[PosY + 1][PosX] == '-' | ||
or map[PosY + 1][PosX] == 'o'): | ||
path.append("down") | ||
down() | ||
elif PosX - 1 >= 0 and (map[PosY][PosX - 1] == '-' | ||
or map[PosY][PosX - 1] == 'o'): | ||
path.append("left") | ||
left() | ||
elif PosY - 1 >= 0 and (map[PosY - 1][PosX] == '-' | ||
or map[PosY - 1][PosX] == 'o'): | ||
path.append("up") | ||
up() | ||
else: | ||
last_move = path.pop() | ||
if last_move == "right": | ||
left() | ||
elif last_move == "down": | ||
up() | ||
elif last_move == "left": | ||
right() | ||
elif last_move == "up": | ||
down() | ||
|
||
|
||
if __name__ == "__main__": | ||
window, canvas = cc_maze.init_graphics(map, PosY, PosX) | ||
algorithm(map) | ||
window.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tkinter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import tkinter as tk | ||
|
||
SQUARE_SIZE = 20 | ||
COLORS = { | ||
"x": "black", | ||
"-": "white", | ||
"o": "green", | ||
".": "yellow", | ||
"P": "blue" | ||
} | ||
|
||
|
||
def load_from_file(path) -> (list[list[str]], int, int): | ||
map: list[list[str]] = [] | ||
|
||
with open(path) as file: | ||
for line in file.readlines(): | ||
map.append(list(line.rstrip())) | ||
for i, row in enumerate(map): | ||
for j, char in enumerate(row): | ||
if char == "-": | ||
return map, i, j | ||
return None, -1, -1 | ||
|
||
|
||
def display_grid(canvas, map, PosY: int, PosX: int): | ||
for i in range(len(map)): | ||
for j in range(len(map[0])): | ||
if PosY == i and PosX == j: | ||
color = COLORS["P"] | ||
else: | ||
color = COLORS[map[i][j]] | ||
x0 = j * SQUARE_SIZE | ||
y0 = i * SQUARE_SIZE | ||
x1 = x0 + SQUARE_SIZE | ||
y1 = y0 + SQUARE_SIZE | ||
canvas.create_rectangle(x0, y0, x1, y1, fill=color) | ||
canvas.update() | ||
|
||
|
||
def update_grid(canvas, X, Y, color): | ||
x0 = X * SQUARE_SIZE | ||
y0 = Y * SQUARE_SIZE | ||
x1 = x0 + SQUARE_SIZE | ||
y1 = y0 + SQUARE_SIZE | ||
|
||
canvas.create_rectangle(x0, y0, x1, y1, fill=color) | ||
canvas.update() | ||
|
||
|
||
def init_graphics(map: list[list[str]], PosY, PosX): | ||
width = len(map[0]) * SQUARE_SIZE | ||
height = len(map) * SQUARE_SIZE | ||
window = tk.Tk() | ||
canvas = tk.Canvas(window, width=width, height=height) | ||
|
||
window.title("Participant: Fil d'Ariane") | ||
window.geometry(f"{width}x{height}") | ||
canvas.pack() | ||
display_grid(canvas, map, PosY, PosX) | ||
return window, canvas |
Oops, something went wrong.