-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
44 lines (33 loc) · 1.13 KB
/
server.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
from adversary import Adversary
from board import Board, Direction, Rotation, Shape
from constants import BOARD_HEIGHT, BOARD_WIDTH, PREFIX
from exceptions import UnknownInstructionException
from player import SelectedPlayer
class RemoteAdversary(Adversary):
def choose_block(self, board):
while True:
try:
command = input().strip()
except EOFError:
raise SystemExit
if command.startswith(PREFIX):
break
command = command[len(PREFIX)+1:]
if command == 'WON' or command == 'LOST':
# Game ended; stop cleanly.
raise SystemExit
try:
return Shape(command)
except ValueError:
pass
raise UnknownInstructionException
board = Board(BOARD_WIDTH, BOARD_HEIGHT)
player = SelectedPlayer()
adversary = RemoteAdversary()
for move in board.run(player, adversary):
if isinstance(move, Direction):
print(f'{PREFIX} {move.value}')
elif isinstance(move, Rotation):
print(f'{PREFIX} {move.value}')
elif move is None:
print(f'{PREFIX} SKIP')