summaryrefslogtreecommitdiff
path: root/__main__.py
diff options
context:
space:
mode:
authorAineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi>2024-02-03 11:08:27 +0200
committerAineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi>2024-02-03 11:08:27 +0200
commit63f27f918c8e0e6a0ae24e0b9315c2f2f7b49b6f (patch)
tree7ba015dd3310bdf5d79e20867ffeddb48d4198d3 /__main__.py
parent4253cf1292a40e6c5c7a6a7729c278da4bf69186 (diff)
Implementing file reader.
Diffstat (limited to '__main__.py')
-rw-r--r--__main__.py58
1 files changed, 36 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}%)"
+)