From 63f27f918c8e0e6a0ae24e0b9315c2f2f7b49b6f Mon Sep 17 00:00:00 2001 From: Aineopintojen-harjoitustyo-Algoritmit-j Date: Sat, 3 Feb 2024 11:08:27 +0200 Subject: Implementing file reader. --- __main__.py | 58 ++++++++++++++++++++++++++++++++++++---------------------- cmdline.py | 11 +++++++++++ 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/__main__.py b/__main__.py index a6b3172..c2497ca 100644 --- a/__main__.py +++ b/__main__.py @@ -2,35 +2,49 @@ import sys from app import App -from tui import KEY_DESCRIPTIONS - from cmdline import args -if args.help: - parser.print_help() - sys.exit() - -if args.keys: - print(end=KEY_DESCRIPTIONS) - sys.exit() - -if args.count is None: +if args.count is None and args.file is None: app = App(args) is_win = app.run() del app sys.exit(not is_win) # Exit koodeissa 0 on onnistunut suoritus - win_count = 0 -run_count = args.count args.autoplay = 2 -for i in range(run_count): - print(end=f" \rSuoritus {i+1:>6}/{run_count} ") - print(end=f"({100*win_count/(i if i else 1):.1f}%)..") - if not args.quiet: - print() - app = App(args) - win_count+=app.run() - del app -print(f"\n## Voittoja {win_count}/{run_count} ({100*win_count/run_count:.1f}%)") +if args.file is None: + run_count = args.count + for i in range(run_count): + print(end=f" \rSuoritus {i+1:>6}/{run_count} ") + print(end=f"({100*win_count/(i if i else 1):.1f}%)..") + if not args.quiet: + print() + app = App(args) + win_count+=app.run() + del app +else: + run_count = 0 + print(f"Pelataan miinaharavat tiedostosta {args.file}") + with open(args.file, "r", encoding="utf-8") as bfile: + board = [] + while True: + line = bfile.readline() + if not line or (line[0]!='.' and line[0]!='@'): + if board: + args_dict = vars(args) + args_dict['board'] = board + app = App(args) + win_count += app.run() + run_count += 1 + del app + board = [] + if not line: + break + continue + board.append([x=='@' for x in line if x in ('.', '@')]) + +print( + f"\n## Voittoja {win_count}/{run_count} " + f"({(100*win_count/run_count) if run_count else 0:.1f}%)" +) diff --git a/cmdline.py b/cmdline.py index b75c49c..dd9e3a6 100644 --- a/cmdline.py +++ b/cmdline.py @@ -1,5 +1,9 @@ +""" cmdline.py - komentorivin käsittely argparse moduulilla """ +import sys from argparse import ArgumentParser +from tui import KEY_DESCRIPTIONS + parser = ArgumentParser( prog='miinaharava', description='Klassisen miinaharavapelin terminaali toteutus.', @@ -99,3 +103,10 @@ misc_group.add_argument( args = parser.parse_args() +if args.help: + parser.print_help() + sys.exit() + +if args.keys: + print(end=KEY_DESCRIPTIONS) + sys.exit() -- cgit v1.2.3