-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathki.py
More file actions
173 lines (150 loc) · 5.14 KB
/
ki.py
File metadata and controls
173 lines (150 loc) · 5.14 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# board = [zeile, spalte]
def find_drops ( board):
result = {}
for s in range(0,7):
for z in range(0,6):
if z<5 and board[z][s] == 0 and board[z+1][s] != 0:
result[s] = z
break
elif z == 5 and board[z][s] == 0:
result[s] = z
break
# if board [z][s] != 0 and z > 0:
# result[s] = z -1
# break
# elif z == 5:
# result[s] = z
# break
print("possible drops: "+str(result))
return result
#
# 5 moegliche dimensionen auf leerfelder prüfen
#
def rate_drop (board, spalte, zeile ):
#print("Rate drop: ["+str(zeile)+","+str(spalte)+"]")
rating = 0
oben = False
links = False
rechts = False
# oben
if zeile == 0:
oben = True
# links
if spalte == 0:
links = True
# rechts
if spalte == 6:
rechts = True
if oben and board[zeile][spalte] != 0:
print(" -> 0")
return 0
for case in range(0,4):
if case == 0 and not links and board [zeile][spalte-1] == 0 :
rating = rating +1
if case == 1 and not links and not oben and board [zeile -1][spalte-1] == 0 :
rating = rating +1
if case == 2 and not oben and board [zeile -1][spalte] == 0 :
rating = rating +1
if case == 3 and not rechts and not oben and board [zeile -1][spalte +1] == 0 :
rating = rating +1
if case == 4 and not rechts and board [zeile][spalte + 1 ] == 0 :
rating = rating +1;
#print(" -> "+str(rating))
return rating
def rate_neighbour (board, spalte, zeile, myid ):
rating = 0
oben = False
links = False
rechts = False
unten = False
# oben
if zeile == 0:
oben = True
# unten
if zeile == 5:
unten = True
# links
if spalte == 0:
links = True
# rechts
if spalte == 6:
rechts = True
for case in range(0,6):
if case == 0 and not links and board [zeile][spalte-1] == myid :
rating = rating +1
if case == 1 and not links and not oben and board [zeile -1][spalte-1] == myid :
rating = rating +1
if case == 2 and not oben and board [zeile -1][spalte] == myid :
rating = rating +1
if case == 3 and not rechts and not oben and board [zeile -1][spalte +1] == myid :
rating = rating +1
if case == 4 and not rechts and board [zeile ][spalte + 1 ] == myid :
rating = rating +1
if case == 5 and not unten and not rechts and board [zeile - 1][spalte + 1 ] == myid:
rating = rating +1
if case == 6 and not unten and board [zeile - 1][spalte ] == myid :
rating = rating +1
if case == 7 and not unten and not links and board [zeile - 1 ][spalte - 1 ] == myid :
rating = rating +1
return rating
def rate_enemy (board, spalte, zeile, myid ):
rating = 0
oben = False
links = False
rechts = False
unten = False
# oben
if zeile == 0:
oben = True
# unten
if zeile == 5:
unten = True
# links
if spalte == 0:
links = True
# rechts
if spalte == 6:
rechts = True
for case in range(0,6):
if case == 0 and not links and board [zeile][spalte-1] != myid :
rating = rating +1
if case == 1 and not links and not oben and board [zeile -1][spalte-1] != myid :
rating = rating +1
if case == 2 and not oben and board [zeile -1][spalte] != myid :
rating = rating +1
if case == 3 and not rechts and not oben and board [zeile -1][spalte +1] != myid :
rating = rating +1
if case == 4 and not rechts and board [zeile ][spalte + 1 ] != myid :
rating = rating +1
if case == 5 and not unten and not rechts and board [zeile - 1][spalte + 1 ] != myid:
rating = rating +1
if case == 6 and not unten and board [zeile - 1][spalte ] != myid :
rating = rating +1
if case == 7 and not unten and not links and board [zeile - 1 ][spalte - 1 ] != myid :
rating = rating +1
return rating
def keywithmaxval(d):
""" a) create a list of the dict's keys and values;
b) return the key with the max value"""
v=list(d.values())
k=list(d.keys())
return k[v.index(max(v))] if len(v)!= 0 else None
def calculate_drop ( board , myid):
possible_drops = find_drops( board)
myrates = {}
neighbour_rates = {}
enemy_rates = {}
result = {}
# calculate rates
for key, element in possible_drops.items():
myrates[key] = rate_drop (board , key, element)
neighbour_rates[key] = rate_neighbour(board , key, element, myid)
enemy_rates[key] = rate_enemy(board , key, element, myid)
result[key] = myrates[key] + neighbour_rates[key] + enemy_rates[key]
#print(myrates.values())
#print(neighbour_rates.values())
print(result.values())
# find best for us
spalte = keywithmaxval(result)
zeile = possible_drops[spalte]
return zeile * 7 + spalte