-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (47 loc) · 1.69 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
import sys
from copy import deepcopy
from alphabeta import alphabeta
from connect4 import State, done, score, successor
from constants import (EMPTY_SLOT, GAME_DRAW, GAME_MAX_WINNER, GAME_MIN_WINNER,
GAME_NO_WINNER, MAX_PLAYER)
def get_play_options(state):
options = []
for j in range(0, state.width):
for i in range(state.height - 1, -1, -1):
if state.board[i][j] != EMPTY_SLOT: continue
options.append((i, j))
break
return options
def end_game(status, current_state):
if status == GAME_MIN_WINNER:
sys.stdout.write('Human Win!\n')
elif status == GAME_MAX_WINNER:
sys.stdout.write('Computer Win!\n')
elif status == GAME_DRAW:
sys.stdout.write('Computer Win!\n')
sys.exit(1)
def update_game(current_state):
status = done(current_state)
sys.stdout.write(str(current_state) + '\n')
if status != GAME_NO_WINNER:
end_game(status, current_state)
def human_vs_computer():
current_state = State(width=7, height=6)
while True:
# Human Turn
sys.stdout.write('\n')
print(current_state)
options = get_play_options(current_state)
for i in range(0, current_state.width):
sys.stdout.write(' ' + str(i) + ' ')
sys.stdout.write('\n-> ')
col = int(input())
for i, j in options:
if col != j: continue
current_state.board[i][j] = -1
update_game(current_state)
# AI Turn
current_state, _score = alphabeta(current_state, 5, MAX_PLAYER, score, done, successor)
update_game(current_state)
if __name__ == '__main__':
human_vs_computer()