Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·70 lines (56 loc) · 2.05 KB
/
Copy pathmain.py
File metadata and controls
executable file
·70 lines (56 loc) · 2.05 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
#!/usr/bin/python3
import argparse
from state import *
import ui_ascii
from random_ai import RandomAI
def main():
# Entry point
flags_parser = argparse.ArgumentParser(description='Play hive: hotseat, online, browser against AI, etc.')
flags_parser.add_argument('--p0', action='store', default='hotseat', choices=['hotseat', 'online', 'ai'], dest='p0')
flags_parser.add_argument('--p1', action='store', default='hotseat', choices=['hotseat', 'online', 'ai'], dest='p1')
flags_parser.add_argument('--ai_ui', action='store_true')
flags = flags_parser.parse_args()
player_types = [flags.p0, flags.p1]
# Initialize AIs
ais = [create_ai(player_types[p]) for p in [0, 1]]
# TODO: check end of game.
board = Board()
while True:
# Check for end-of-game.
end_of_game, winner = board.end_of_game()
if end_of_game:
ui = ui_ascii.UI(board, list_moves=False)
ui.print()
if winner == -1:
print('\nDRAW!!!\n')
else:
print('\nPLAYER {} WINS'.format(winner))
break
player = board.next_player
if player_types[player] == 'hotseat':
ui = ui_ascii.UI(board)
ui.print()
valid = next(board.valid_moves(), None)
if valid is None:
_ = input('No valid move, press enter to continue')
move = None
else:
move = ui.read()
board.move(move)
elif player_types[player] == 'ai':
if flags.ai_ui:
ui = ui_ascii.UI(board)
ui.print()
move = ais[player].play(board)
if flags.ai_ui:
print("Selected move: {}".format(move))
board.move(move)
else:
raise ValueError('player of type \"{}\" not implemented'.format(player_types[player]))
def create_ai(player_type):
"""Creates AI player."""
if player_type != 'ai':
return None
return RandomAI()
if __name__ == '__main__':
main()