Add a side-help panel

This commit is contained in:
adithyagenie 2022-11-03 02:10:09 +05:30
parent c749cf56c8
commit 331764e528

View file

@ -34,7 +34,7 @@ VISITED = 16
class Maze: class Maze:
def __init__(self, height, width, start=(0, 0)): def __init__(self, height, width, start=(0, 0)):
self.height = height self.height = height
self.width = width self.width = width - 12
self.stack = [] self.stack = []
self.cells = {(y, x): 0 for y in range(height) for x in range(width)} self.cells = {(y, x): 0 for y in range(height) for x in range(width)}
self.build(start) self.build(start)
@ -91,18 +91,6 @@ class Maze:
current_cell = next_cell current_cell = next_cell
def __repr__(self): def __repr__(self):
log = open("log.txt", "w")
log.write(
str(self.cells)
+ "\n"
+ str(self.stack)
+ "\n\n\n\n\n"
+ "Height: "
+ str(self.height)
+ "Width: "
+ str(self.width)
+ "\n\n\n\n\n\n"
)
buffer = [ buffer = [
[0 for _ in range(2 * self.width + 1)] for _ in range(2 * self.height + 1) [0 for _ in range(2 * self.width + 1)] for _ in range(2 * self.height + 1)
] ]
@ -124,8 +112,6 @@ class Maze:
| (buffer[2 * row + 1][2 * col] << 1) | (buffer[2 * row + 1][2 * col] << 1)
) )
log.write("FIRST ITERATION GIVES: " + str(buffer) + "\n\n\n\n\n")
for row in range(1, 2 * self.height): for row in range(1, 2 * self.height):
buffer[row][0] = CONNECTED["N"] | CONNECTED["S"] | (buffer[row][1] >> 1) buffer[row][0] = CONNECTED["N"] | CONNECTED["S"] | (buffer[row][1] >> 1)
buffer[row][2 * self.width] = ( buffer[row][2 * self.width] = (
@ -141,9 +127,7 @@ class Maze:
buffer[2 * self.height][0] = CONNECTED["N"] | CONNECTED["E"] buffer[2 * self.height][0] = CONNECTED["N"] | CONNECTED["E"]
buffer[2 * self.height][2 * self.width] = CONNECTED["N"] | CONNECTED["W"] buffer[2 * self.height][2 * self.width] = CONNECTED["N"] | CONNECTED["W"]
finalstr = "\n".join(["".join(WALL[cell] for cell in row) for row in buffer]) finalstr = "\n".join(["".join(WALL[cell] for cell in row) for row in buffer])
# log.write("\n\n"+str(finalstr))
log.write(str(buffer))
log.close()
return finalstr return finalstr
@ -228,7 +212,7 @@ def construction_demo(maze, screen):
screen.getch() screen.getch()
def pathfinding_demo(maze, screen): def pathfinding_demo(maze, screen, start_ts):
start = [] start = []
finish = [] finish = []
solution = None solution = None
@ -246,15 +230,17 @@ def pathfinding_demo(maze, screen):
solution, old_solution = tee(path(maze, start[0], finish[0])) solution, old_solution = tee(path(maze, start[0], finish[0]))
draw_path(solution, screen) """ draw_path(solution, screen) """
maxy, maxx = screen.getmaxyx() maxy, maxx = screen.getmaxyx()
current_coords = [maxy - 5, maxx - 5] current_coords = [maxy - 5, maxx - 35]
screen.addstr(current_coords[0], current_coords[1], "", curses.color_pair(2)) screen.addstr(current_coords[0], current_coords[1], "", curses.color_pair(2))
WALL = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""] WALL = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
while True: while True:
screen.addstr(2, maxx - 17, str(round(time.time()-start_ts, 0)) + " sec")
screen.refresh()
key = screen.getch() key = screen.getch()
# print("Max=",maxy, maxx, "Current=", current_coords[0], current_coords[1]) # print("Max=",maxy, maxx, "Current=", current_coords[0], current_coords[1])
if key == ord("q"): if key == 27:
break break
elif current_coords[0] == maxy - 3 and current_coords[1] == maxx - 3: elif current_coords[0] == maxy - 3 and current_coords[1] == maxx - 27:
screen.clear() screen.clear()
screen.refresh() screen.refresh()
screen.addstr( screen.addstr(
@ -414,14 +400,22 @@ def play(screen):
came_out = 0 came_out = 0
start_ts = 0 start_ts = 0
end_ts = 0 end_ts = 0
height, width = screen.getmaxyx() y, x = screen.getmaxyx()
height, width = int((height - 2) / 2), int((width - 2) / 2) height, width = int((y - 2) / 2), int((x - 2) / 2)
screen.clear() screen.clear()
maze = Maze(height, width) maze = Maze(height, width)
screen.addstr(0, 0, str(maze)) screen.addstr(0, 0, str(maze))
screen.refresh() screen.refresh()
screen.addstr(0, x - 23, "LABYRINTH")
screen.addstr(2, x - 23, "Time:")
screen.addstr(5, x - 23, "esc - Quit")
screen.addstr(6, x - 23, "Right - Move right")
screen.addstr(7, x - 23, "Left - Move left")
screen.addstr(8, x - 23, "Up - Move up")
screen.addstr(9, x - 23, "Down - Move down")
screen.refresh()
start_ts = time.time() start_ts = time.time()
pathfinding_demo(maze, screen) pathfinding_demo(maze, screen, start_ts)
end_ts = time.time() end_ts = time.time()
came_out = 1 came_out = 1
while True: while True:
@ -436,7 +430,7 @@ def play(screen):
if WON != 0: if WON != 0:
tt = (start_ts - end_ts) / 300 tt = (start_ts - end_ts) / 300
score = fabs(cos(tt * pi)) score = fabs(cos(tt * pi))
score *= 90237 score *= 10000
WON = 0 WON = 0
else: else:
score = 0 score = 0