Saving and loading maze - now better

This commit is contained in:
adithyagenie 2022-11-22 16:05:23 +05:30
parent 3fdfd5bc6e
commit 640dbd9e8a
5 changed files with 53 additions and 33 deletions

View file

@ -1,11 +1,21 @@
Oh hello there. Oh hello there.
To run this project, basically theres two things you need to do. To run this project, basically theres two things you need to do.
1. To open this readme file which you have already done 1. To open this readme file which you have already done
2. Open command prompt (just type it in start menu) 2. Open command prompt (just type it in start menu)
3. Type in 'cd' + the path name to where this readme is 3. Type in 'cd' + the path name to where this readme is
4. Type in 'python starter.py initsql' to initialise your mysql credentials 4. Type in 'python starter.py initsql' to initialise your mysql credentials
5. Type in 'python starter.py' and enjoy the game! 5. Type in 'python starter.py' and enjoy the game!
6. If you want to test the database functionalities, just type in 'python starter.py dumpsample' to dump a sample database 6. If you want to test the database functionalities, just type in 'python starter.py dumpsample' to dump a sample database
Enjoy! Enjoy!

Binary file not shown.

View file

@ -174,16 +174,16 @@ def path(maze, start, finish): # Not used
nodes_to_explore.append(neighbour) nodes_to_explore.append(neighbour)
def draw_path(path, screen, delay=0.15, head=None, trail=None, skip_first=True): def draw_path(path, screen, delay=0, head=None, trail=None, skip_first=True, calledby=None):
if not head: if not head:
head = ("", curses.color_pair(1)) head = ("", curses.color_pair(2))
if not trail: if not trail:
trail = ("", curses.color_pair(1)) trail = ("", curses.color_pair(2))
current_cell = next(path) current_cell = next(path)
old_cell = current_cell old_cell = current_cell
for idx, next_cell in enumerate(path): for idx, next_cell in enumerate(path):
first = (not idx) and skip_first first = (not idx) and skip_first
if screen.getch() == ord(" "): if calledby != "reset" and screen.getch() == ord(" "):
break break
screen.refresh() screen.refresh()
for last, cell in enumerate( for last, cell in enumerate(
@ -213,34 +213,41 @@ def coords(node):
def construction_demo(maze, screen): def construction_demo(maze, screen):
head = (".", curses.color_pair(2)) head = (".", curses.color_pair(3))
trail = (".", curses.color_pair(1)) trail = (".", curses.color_pair(2))
draw_path( draw_path(
maze.track(), screen, delay=0.01, head=head, trail=trail, skip_first=False maze.track(), screen, delay=0.01, head=head, trail=trail, skip_first=False
) )
screen.nodelay(False) screen.nodelay(False)
def pathfinding_demo(maze, screen, start_ts, won_coords): def pathfinding_demo(maze, screen, start_ts, won_coords, loadedcoords=None, loadedtime=0):
start = [] start = []
finish = [] finish = []
solution = None solution = None
old_solution = None old_solution = None
""" def reset(start_or_finish, cell, colour): def reset(start_or_finish, cell, colour):
nonlocal solution, old_solution nonlocal solution, old_solution
if start_or_finish: if start_or_finish:
screen.addstr(*coords(start_or_finish.pop()), " ") screen.addstr(*coords(start_or_finish.pop()), " ")
screen.addstr(*coords(cell), "", colour) screen.addstr(*coords(cell), "", colour)
screen.refresh() screen.refresh()
if old_solution: if old_solution:
draw_path(old_solution, screen, head=" ", trail=" ") draw_path(old_solution, screen, head=" ", trail=" ", calledby="reset")
start_or_finish.append(cell) start_or_finish.append(cell)
if start and finish: if start and finish:
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, calledby="reset")
maxy, maxx = screen.getmaxyx() maxy, maxx = screen.getmaxyx()
current_coords = [maxy - 5, maxx - 27]
# current_coords = [1, 1] if loadedcoords:
current_coords = list(loadedcoords)
cell = (int(current_coords[0] / 2), int(current_coords[1] / 2))
reset(finish, cell, curses.color_pair(2))
reset(start, (0,0), curses.color_pair(2))
else:
#current_coords = [maxy - 5, maxx - 27]
current_coords = [1, 1]
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 = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
pause_elapsed = 0 pause_elapsed = 0
@ -258,8 +265,8 @@ def pathfinding_demo(maze, screen, start_ts, won_coords):
screen.addstr(4, maxx - 17, " ") screen.addstr(4, maxx - 17, " ")
PAUSED = False PAUSED = False
break break
pause_elapsed = int(end_paused_ts - start_paused_ts) pause_elapsed += int(end_paused_ts - start_paused_ts)
actual_elapsed = str(int(time.time() - start_ts) - pause_elapsed) actual_elapsed = str(int(time.time() - start_ts - -1*loadedtime) - pause_elapsed)
screen.addstr(5, maxx - 17, actual_elapsed + " sec") screen.addstr(5, maxx - 17, actual_elapsed + " sec")
screen.refresh() screen.refresh()
key = screen.getch() key = screen.getch()
@ -270,7 +277,7 @@ def pathfinding_demo(maze, screen, start_ts, won_coords):
PAUSED = True PAUSED = True
continue continue
elif key == ord("m"): elif key == ord("m"):
sl.save(screen, maze, current_coords) sl.save(screen, maze, current_coords, float(actual_elapsed))
elif current_coords[0] == won_coords[0] and current_coords[1] == won_coords[1]: elif current_coords[0] == won_coords[0] and current_coords[1] == won_coords[1]:
screen.clear() screen.clear()
screen.refresh() screen.refresh()
@ -294,7 +301,7 @@ def pathfinding_demo(maze, screen, start_ts, won_coords):
WON = WON + 1 WON = WON + 1
time.sleep(3) time.sleep(3)
break break
# Dota stuff not to be used now # Mouse hacks
# elif key == curses.KEY_MOUSE: # elif key == curses.KEY_MOUSE:
# _, x, y, _, state = curses.getmouse() # _, x, y, _, state = curses.getmouse()
# cell = (int(y / 2), int(x / 2)) # cell = (int(y / 2), int(x / 2))
@ -439,7 +446,7 @@ def menu(screen):
if present: if present:
maze = sl.load(screen) maze = sl.load(screen)
if maze: if maze:
play(screen, maze[0]) play(screen, maze[0], maze[1], maze[2])
return return
else: else:
screen.addstr( screen.addstr(
@ -452,7 +459,7 @@ def menu(screen):
break break
def play(screen, loadedmaze=None): def play(screen, loadedmaze=None, loadedcoords=None, loadedtime=0):
y, x = screen.getmaxyx() y, x = screen.getmaxyx()
height, width = int((y - 2) / 2), int((x - 2) / 2) height, width = int((y - 2) / 2), int((x - 2) / 2)
screen.clear() screen.clear()
@ -480,7 +487,7 @@ def play(screen, loadedmaze=None):
screen.addstr(15, sx, "m - save") screen.addstr(15, sx, "m - save")
screen.refresh() screen.refresh()
start_ts = time.time() start_ts = time.time()
pathfinding_demo(maze, screen, start_ts, won_coords) pathfinding_demo(maze, screen, start_ts, won_coords, loadedcoords, loadedtime)
end_ts = time.time() end_ts = time.time()
came_out = 1 came_out = 1
while True: while True:

View file

@ -7,7 +7,7 @@ import maze.modules.maze as m
from .PlayerBase_func import input, screenwipe from .PlayerBase_func import input, screenwipe
def save(screen, maze, coords): def save(screen, maze, coords, elapsed):
y, x = screen.getmaxyx() y, x = screen.getmaxyx()
if "saves" not in os.listdir(): if "saves" not in os.listdir():
os.mkdir("saves") os.mkdir("saves")
@ -30,6 +30,7 @@ def save(screen, maze, coords):
f = open("saves//" + filename, "wb") f = open("saves//" + filename, "wb")
pickle.dump(maze, f) pickle.dump(maze, f)
pickle.dump(coords, f) pickle.dump(coords, f)
pickle.dump(elapsed, f)
f.close() f.close()
screen.addstr(19, x - 17, "Maze saved!") screen.addstr(19, x - 17, "Maze saved!")
screen.refresh() screen.refresh()
@ -84,5 +85,6 @@ def load(screen):
f = open("saves//" + mazes[num], "rb") f = open("saves//" + mazes[num], "rb")
maze = pickle.load(f) maze = pickle.load(f)
coords = pickle.load(f) coords = pickle.load(f)
elapsed = pickle.load(f)
f.close() f.close()
return maze, coords return maze, coords, elapsed

View file

@ -3,11 +3,11 @@ import pickle
import subprocess import subprocess
import sys import sys
import mysql.connector
from maze.modules import bruh from maze.modules import bruh
from maze.modules.PlayerBase_func import databaseinit from maze.modules.PlayerBase_func import databaseinit
user = password = None
with open("credentials.pickle", "rb") as f: with open("credentials.pickle", "rb") as f:
try: try:
while True: while True:
@ -21,7 +21,7 @@ with open("credentials.pickle", "rb") as f:
def getcreds(): def getcreds():
if user and password: if user:
return user, password return user, password
else: else:
print( print(
@ -52,11 +52,12 @@ else:
getcreds() getcreds()
databaseinit() databaseinit()
subprocess.call( subprocess.call(
f"mysql -u root --password=root -D labyrinth < {os.path.abspath('dbdump.sql')}", f"mysql -u {user} --password={password} -D labyrinth < {os.path.abspath('dbdump.sql')}",
shell=True, shell=True#,
stdout=subprocess.DEVNULL, #stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, #stderr=subprocess.DEVNULL,
) )
print("Successfully dumped sample data")
elif sys.argv[1] == "initsql": elif sys.argv[1] == "initsql":
user = input("Enter MySQL username: ") user = input("Enter MySQL username: ")
password = input("Enter MySQL password: ") password = input("Enter MySQL password: ")