-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
146 lines (136 loc) · 4.49 KB
/
test.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
board = ["0000000", "0000000", "0000000", "0000000", "0000000", "0000000"]
def calculateconnections(board, value, notvalue, numberofconnections):
connection = 0
score = 0
bonus = blocked = 0
# for Rows
flag = 1
for i in range(0, 6):
tempbonus = 0
flag = 1
for j in range(0, 7):
# print(connection)
if board[i][j] == value:
connection += 1
if j == 3:
tempbonus += 0.4
if i == 2:
tempbonus += 0.4
elif j == 2 or 1 or 4 or 5:
tempbonus += 0.2
if connection == numberofconnections:
score += 1
connection = 0
elif board[i][j] == notvalue:
if connection < 4:
if connection >= 1:
blocked += 1
flag = 0
connection = 0
else:
connection = 0
if flag:
bonus += tempbonus
connection = 0
# for columns
for i in range(0, 7):
tempbonus = 0
flag = 1
for j in range(0, 6):
# print(connection)
if board[j][i] == value:
connection += 1
if i == 3:
tempbonus += 0.3
if j == 2:
tempbonus += 0.3
elif i == 2 or 1 or 4 or 5:
tempbonus += 0.2
if connection == numberofconnections:
score += 1
connection = 0
elif board[j][i] == notvalue:
if connection < 4:
if connection >= 1:
blocked += 1
flag = 0
connection = 0
else:
connection = 0
if flag:
bonus += tempbonus
connection = 0
# for digaonal
for i in range(0, 6):
tempbonus = 0
flag = 1
for j in range(0, 7):
row = i
column = j
while row < 6 and column < 7:
if board[row][column] == value:
connection += 1
if row == 2:
tempbonus += 0.4
if column == 3:
tempbonus += 0.4
elif i == 2 or 1 or 4 or 5:
tempbonus += 0.2
if connection == numberofconnections:
score += 1
connection = 0
elif board[row][column] == notvalue:
if connection < 4:
if connection >= 1:
blocked += 1
flag = 0
connection = 0
else:
connection = 0
row += 1
column += 1
if flag:
bonus += tempbonus
connection = 0
# for reversed digaonal
for i in range(0, 6):
tempbonus = 0
flag = 1
for j in range(0, 7):
row = i
column = j
while row < 6 and column < 7 and column > 0:
if board[row][column] == value:
connection += 1
if row == 2:
tempbonus += 0.4
if column == 3:
tempbonus += 0.4
elif i == 2 or 1 or 4 or 5:
tempbonus += 0.2
if connection == numberofconnections:
score += 1
connection = 0
elif board[row][column] == notvalue:
if connection < 4:
if connection >= 1:
blocked += 1
flag = 0
connection = 0
else:
connection = 0
row += 1
column -= 1
if flag:
bonus += tempbonus
connection = 0
return score, bonus, blocked
def evaluation(board, value, notvalue):
score = 0
x, y, z = calculateconnections(board, value, notvalue, 2)
score += x * 10 + y * 10 - z * 10
x, y, z = calculateconnections(board, value, notvalue, 3)
score += x * 100 + y * 100 - z * 100
x, y, z = calculateconnections(board, value, notvalue, 4)
score += x * 1000 + y * 100 - z * 1000
return score