-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_four.py
155 lines (139 loc) · 3.54 KB
/
connect_four.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from agents import *
import numpy as np
def is_legal(game_state,choice):
return not (choice<0 or choice>6 or game_state[choice][0]!=0)
def main():
game_state = np.zeros((7,6),dtype=np.int32)
agent1 = Smart_Agent(1)
agent1.set_max_depth(4)
agent2 = Alpha_Beta_Agent(-1)
agent2.set_max_depth(4)
game_over = False
turn =1
# game_state[0]=[1, 1 , 1 , -1 ,-1, 1]
# game_state[1]=[ 0 ,0, 0, 0, 0, 0]
# game_state[2]=[ -1, 1, 1, -1, 1, -1]
# game_state[3]=[ -1,1, 1, -1, 1, 1]
# game_state[4]=[-1 , 1,-1 ,-1 ,1,-1]
# game_state[5]=[ 0 , 0 , 0, 0, 0 ,0]
# game_state[6]=[ 0 ,0 , 0 , 0, 0, -1]
# print(game_state)
# print(agent2.move(game_state,33))
# exit()
while(not game_over):
choice = agent1.move(np.copy(game_state),turn)
if not is_legal(game_state,choice):
print("A2 WINS due to illegal move. A1 tried to play %d "%choice)
game_over=True
break
y = make_move(game_state,choice,agent1.order)
if is_win(game_state,choice,y):
print("A1 WINS")
game_over=True
break
turn+=1
choice = agent2.move(np.copy(game_state),turn)
if not is_legal(game_state,choice):
print("A1 WINS due to illega move. A2 tried to play %d "%choice)
game_over=True
break
y = make_move(game_state,choice,agent2.order)
if is_win(game_state,choice,y):
print("A2 WINS")
game_over=True
break
turn+=1
if turn >=42:
game_over=True
print("DRAW")
break
# print(game_state)
print(game_state)
def make_move(game_state,choice,order):
i = 0
while i<6 and game_state[choice][i]==0:
i=i+1
game_state[choice][i-1]=order
return i-1
def is_win(game_state,x,y):
order = game_state[x][y]
#downwards check:
count = 0
i = y
while i<6:
if game_state[x][i]!=order:
break
else:
count+=1
i+=1
if count>=4:
print(25)
return True
#Sideways check:
count = 0
i = x
while i<7:
if game_state[i][y]!=order:
break
else:
count+=1
i+=1
i = x-1
while i>=0:
if game_state[i][y]!=order:
break
else:
count+=1
i-=1
if count>=4:
print(45)
return True
#Top-left to bottom-right check:
count = 0
i = x
j = y
while i<7 and j<6:
if game_state[i][j]!=order:
break
else:
count+=1
i+=1
j+=1
i = x-1
j = y-1
while i>=0 and j>=0:
if game_state[i][j]!=order:
break
else:
count+=1
i-=1
j-=1
if count>=4:
print(69)
return True
#Bottom-left to top-right check:
count = 0
i = x
j = y
while i<7 and j>=0:
if game_state[i][j]!=order:
break
else:
count+=1
i+=1
j-=1
i = x-1
j = y+1
while i>=0 and j<6:
if game_state[i][j]!=order:
break
else:
count+=1
i-=1
j+=1
if count>=4:
print(93)
return True
return False
if __name__=="__main__":
main()