-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsim.py
68 lines (53 loc) · 1.8 KB
/
sim.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
from blocks import BLOCKS
from game import Piece, GameState, PieceRotation
import random
import time
import argparse
def algorithm(
name: str, game: GameState, moves: list[tuple[PieceRotation, tuple[int, int]]]
) -> tuple[PieceRotation, tuple[int, int]]:
if len(moves) == 0:
raise ValueError("No moves available")
if name == "random":
return random.choice(moves)
elif name == "greedy":
# random sort so that the chosen move is not always the first one
random.shuffle(moves)
return max(moves, key=lambda move: game.raw_pieces[move[0].parent].count)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simulate the game")
parser.add_argument(
"--algorithm",
"-a",
type=str,
default="random",
help="Algorithm to use for the AI (random, greedy)",
)
args = parser.parse_args()
boardsize = 20
game = GameState(boardsize)
time_avg = 0
moves_avg = 0
nmoves = 0
while True:
fails = 0
for player in range(4):
nmoves += 1
before = time.time_ns()
moves = list(game.get_moves(player))
after = time.time_ns()
print(f"Player {player} has {len(moves)} moves, took {after - before} ns")
time_avg += after - before
moves_avg += len(moves)
if len(moves) == 0:
fails += 1
continue
move = algorithm(args.algorithm, game, moves)
game.place(player, *move)
print(game.debug_str(True, True))
if fails == 4:
break
print(
f"Average time per move: {time_avg / nmoves} ns, {time_avg / nmoves / 1_000_000} ms"
)
print(f"Average number of moves per turn: {moves_avg / nmoves}")