-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_game.py
More file actions
executable file
·86 lines (65 loc) · 2.13 KB
/
test_game.py
File metadata and controls
executable file
·86 lines (65 loc) · 2.13 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""
Playing through a game, provoking all error cases.
"""
from game_server_api import GameServerAPI, GameServerError, IllegalMove
import threading
import traceback
SERVER = '127.0.0.1'
PORT = 4711
end = threading.Event()
sync = threading.Event()
error = False
def fail(msg):
global error
error = True
print(f'ERROR: {msg}')
end.set()
def synchronize_threads():
sync.set()
sync.clear()
sync.wait()
moves = [(1, True), (2, True), (4, True), (4, False), (42, False), (5, True), (7, True)]
def play():
try:
game = GameServerAPI(SERVER, PORT, 'TicTacToe', 'test', 2)
my_id = game.join()
state = game.state()
if my_id not in state['current']:
try:
game.move(position=1)
fail('no exception although not players turn')
except GameServerError:
pass
synchronize_threads()
while len(moves):
if state['gameover']: fail('gameover although game has not ended')
if state['winner'] is not None: fail('winner although game still active')
if my_id in state['current']:
try:
move, legal = moves.pop(0)
game.move(position=move)
if not legal: fail('no exception despite illegal move')
except IllegalMove:
if legal: fail('exception despite legal move')
synchronize_threads()
state = game.state()
if not state['gameover']: fail('no gameover after final move')
try:
game.move(position=1)
fail('no exception when performing move after game has ended')
except GameServerError:
pass
if state['winner'] is None: fail('no winner despite win')
if len(state['current']): fail('current player list not empty although game has ended')
except:
fail('unexpected exception:\n' + traceback.format_exc())
end.set()
for _ in range(2):
threading.Thread(target=play, daemon=True).start()
end.wait()
if error:
exit(1)
else:
print('OK')
exit(0)