-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
41 lines (29 loc) · 1.17 KB
/
player.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
from abc import ABC, abstractmethod
import random
from games.game import Game, BoardGame
from utils.game_utils import action_to_cellstr, cellstr_to_action, mask_illegal_actions
class Player(ABC):
"""Player abstract class"""
@abstractmethod
def play(self, game: Game) -> int:
"""Select a move to play"""
class RandomPlayer(Player):
"""Player that plays moves randomly"""
def play(self, game: Game) -> int:
legal_actions = mask_illegal_actions(game.legal_actions())
return random.choice(legal_actions)
class HumanPlayer(Player):
"""Human player"""
def play(self, game: Game) -> int:
action = None
actions = mask_illegal_actions(game.legal_actions())
actions_display = list(map(lambda a: ''.join(
action_to_cellstr(a, game.size)), actions)
) if isinstance(game, BoardGame) else actions
while action not in actions:
action_str = input(f'Legal moves are {actions_display}, choose one: ')
if isinstance(game, BoardGame):
action = cellstr_to_action(action_str, game.size)
else:
action = int(action_str)
return action