-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (71 loc) · 2.42 KB
/
main.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
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
87
88
89
90
91
92
93
from numerical_tic_tac_toe import Board
from numerical_tic_tac_toe import Player
def main():
human_vs_ai = prompt_user_for_game_type()
if human_vs_ai:
human_player_first = prompt_user_for_play_order()
players = create_players(human_player_first)
else:
players = [Player(is_human=0, is_max=1), Player(is_human=0, is_max=0)]
max_, min_ = get_max_and_min(players)
board = Board(4)
print(board)
while True:
max_move = max_.get_move(board)
board = max_.perform_move(*max_move, board)
print(board)
if board.has_winning_sum:
print('Max wins!')
break
if len(list(board.all_possible_moves)) == 0:
print('Draw!')
break
if not human_vs_ai:
input('Press enter to continue...')
min_move = min_.get_move(board)
board = min_.perform_move(*min_move, board)
print(board)
if board.has_winning_sum:
print('Min wins!')
break
if len(list(board.all_possible_moves)) == 0:
print('Draw!')
break
if not human_vs_ai:
input('Press enter to continue...')
def prompt_user_for_game_type():
human_vs_ai = -1
while human_vs_ai < 0:
human_vs_ai = input('Would you like to play against an AI [Y/n]?:').lower()
if human_vs_ai == '' or human_vs_ai == 'y':
human_vs_ai = 1
elif human_vs_ai == 'n':
human_vs_ai = 0
return human_vs_ai
def prompt_user_for_play_order():
human_player_first = -1
while human_player_first < 0:
human_player_first = input('Would you like to go first [Y/n]?:').lower()
if human_player_first == '' or human_player_first == 'y':
human_player_first = 1
elif human_player_first == 'n':
human_player_first = 0
return human_player_first
def create_players(human_player_first):
if human_player_first == 1:
human_player = Player(is_human=1, is_max=1)
ai_player = Player(is_human=0, is_max=0)
else:
human_player = Player(is_human=1, is_max=0)
ai_player = Player(is_human=0, is_max=1)
return [human_player, ai_player]
def get_max_and_min(players):
max_ = min_ = None
for player in players:
if player.is_max:
max_ = player
else:
min_ = player
return max_, min_
if __name__ == '__main__':
main()