forked from cantabcapital/maze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
74 lines (57 loc) · 2.11 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'''
example.py
Some examples of creating mazes, running games, collecting statistics, and opening the GUI.
'''
from __future__ import print_function
import sys
import time
from collections import defaultdict
from PyQt5.QtWidgets import QApplication
from maze import Maze, Game, game_repeater
from goodies import RandomGoody
from baddies import RandomBaddy
from gui import GameViewer
# a test
EXAMPLE_MAZE = Maze(10, 10, "0001010000"
"0111010101"
"0100000011"
"0110100010"
"0000100110"
"1111100000"
"0000001000"
"1000111010"
"0010001010"
"1100101010")
def text_example():
''' Prints the state of the game to stdout after each round of turns '''
goody0 = RandomGoody()
goody1 = RandomGoody()
baddy = RandomBaddy()
game = Game(EXAMPLE_MAZE * (2, 2), goody0, goody1, baddy)
def hook(game):
print(game, "\n")
time.sleep(0.1) # Max speed of 10 updates per second
game.play(hook=hook)
def stats_example(total_games):
''' Plays many games, printing cumulative and final stats '''
results = defaultdict(int)
for game_number, game in enumerate(game_repeater(EXAMPLE_MAZE, RandomGoody, RandomGoody, RandomBaddy)):
if game_number == total_games:
break
result, _rounds = game.play()
results[result] += 1
if game_number % 10 == 0:
print(game_number, "/", total_games, ":", dict(results))
print(dict(results))
def gui_example():
''' Opens a GUI, allowing a games to be stepped through or quickly played one after another '''
app = QApplication.instance() or QApplication(sys.argv)
gv = GameViewer()
gv.show()
gv.set_game_generator(game_repeater(EXAMPLE_MAZE * (3, 3), RandomGoody, RandomGoody, RandomBaddy))
app.exec_()
if __name__ == "__main__":
# Uncomment whichever example you want to run
text_example()
# stats_example(1000)
# gui_example()