-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_players.py
More file actions
73 lines (55 loc) · 1.91 KB
/
test_players.py
File metadata and controls
73 lines (55 loc) · 1.91 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
from random import randint
import random
class Player():
def __init__(self, name="Player"):
self.name = name
def move(self, game, time_left):
pass
def get_name(self):
return self.name
class RandomPlayer(Player):
"""Player that chooses a move randomly."""
def __init__(self, name="RandomPlayer"):
super().__init__(name)
def move(self, game, time_left):
if not game.get_player_moves(self):
return None
else:
return random.choice(game.get_player_moves(self))
def get_name(self):
return self.name
class HumanPlayer(Player):
"""
Player that chooses a move according to user's input.
(Useful if you play in the terminal)
"""
def __init__(self, name="HumanPlayer"):
super().__init__(name)
def move(self, game, time_left):
legal_moves = game.get_player_moves(self)
choice = {}
if not len(legal_moves):
print("No more moves left.")
return None, None
counter = 1
for move in legal_moves:
choice.update({counter: move})
print('\t'.join(['[%d] (%d,%d)' % (counter, move[0], move[1])]))
counter += 1
print("-------------------------")
print(game.print_board(legal_moves))
print("-------------------------")
print(">< - impossible, o - valid move")
print("-------------------------")
valid_choice = False
while not valid_choice:
try:
index = int(input('Select move index [1-' + str(len(legal_moves)) + ']:'))
valid_choice = 1 <= index <= len(legal_moves)
if not valid_choice:
print('Illegal move of queen! Try again.')
except Exception:
print('Invalid entry! Try again.')
return choice[index]
def get_name(self):
return self.name