Mentions légales du service

Skip to content
Snippets Groups Projects
Commit c3fa30dc authored by SZCZEPANSKI Marin's avatar SZCZEPANSKI Marin
Browse files

Base for the system to save inputs and replay them

parent 019639b0
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ levels/tutorial/04_stairs
levels/tutorial/05_key
levels/tutorial/05_real_key
levels/interrupteurs/main
**/main
mediumgame/local_struct
......
from sys import argv
from datetime import datetime
import os
out_dir = "saved_inputs"
def play_file(filename, display_state=True):
print("=== display_state is", display_state)
try:
with open(filename, "r") as f:
for line in f:
# give the command to the inferior here and display the result
# stop when exit_main_loop becomes true
# or maybe give all the file at once?
print(line, end="")
except OSError:
print("=== Can't read file", filename)
def record_mode():
# We assume students are note able to launch -r twice in the same second
filename = out_dir + "/" + datetime.now().strftime("%y-%m-%d_%H:%M:%S") + ".in"
print("=== Record mode\n=== Inputs will be saved in", filename)
try:
os.mkdir(out_dir)
except FileExistsError:
pass
with open(filename, "w") as f:
# just to test we cycle 5 times, in real we will use easytracker to watch exit_main_loop
i = 0
while i < 5:
c = input("=== Enter a command: ")
# give the command to the inferior and display the result
f.write(c + "\n")
i = i + 1
def play_mode(filename):
print("=== Play mode with", filename)
play_file(filename)
def all_mode():
print("=== All mode\n=== Reading files in", out_dir)
empty_folder = True
try:
for e in os.listdir(out_dir):
empty_folder = False
filename = out_dir + "/" + e
if os.path.isfile(filename) and e.endswith(".in"):
print("=== Playing", e)
play_file(filename, False)
except FileNotFoundError:
empty_folder = False
print("=== The folder", out_dir, "doesn't exist. Use -r mode to create it")
if empty_folder:
print("=== There is no input file in ", out_dir, ". Use -r mode to create some", sep="")
def main(arg):
if len(arg) < 2:
print("=== Usage: ", arg[0], " <mode>\n"
"=== <mode> can be:\n",
"=== -r records manual inputs and displays the game's state,\n",
"=== -p FILENAME plays inputs from file FILENAME and display the game's state,\n",
"=== -a plays all inputs files in the folder ", out_dir,
" and only displays the final result for each",
sep="")
return
if arg[1] == "-r":
record_mode()
elif arg[1] == "-p":
if len(arg) != 3:
print("=== Please enter a single FILENAME for play mode.")
return
else:
play_mode(arg[2])
elif arg[1] == "-a":
all_mode()
else:
print("=== Unknown mode. Valid modes are -r, -p FILENAME and -a")
if __name__ == "__main__":
main(argv)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment