-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSAIbackup.txt
218 lines (179 loc) · 8.66 KB
/
SAIbackup.txt
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 28 23:07:21 2017
@author: Yale
"""
#import all the functions from the game engine
#from mainwithai import * #Lesson: don't make circular references to other files, won't run; obvious when you realize it
import random
sys_random = random.SystemRandom()
#functions copied from main with ai as needed:
def casx(x):
if(x < 150 or x > 650):
return -1
else:
return int((x-150)/50)
def casy(y):
if(y < 50 or y > 550):
return -1
else:
return int((y-50)/50)
class TwoWayDict(dict): #I needed a two way dictionary for my number/board position pairs, so...
def __setitem__(self, key, value):
# Remove any previous connections with these values
if key in self:
del self[key]
if value in self:
del self[value]
dict.__setitem__(self, key, value)
dict.__setitem__(self, value, key)
def __delitem__(self, key):
dict.__delitem__(self, self[key])
dict.__delitem__(self, key)
def __len__(self):
"""Returns the number of connections"""
return dict.__len__(self) // 2
class AI_Player():
def __init__(self,color,board):
self.color = color
self.board = board
self.totalAllies = {'11':6,'10':1,'9':1,'8':1,'7':1,'6':2,'5':2,'4':2,'3':5,'2':7,'1':1,'0':1}
self.totalEnemies = {'11':6,'10':1,'9':1,'8':1,'7':1,'6':2,'5':2,'4':2,'3':5,'2':7,'1':1,'0':1}
self.myboardsred=[]
self.myboardsblue=[]
self.pieceident=TwoWayDict()
if self.color == 'red':
self.othercolor = 'blue'
elif self.color == 'blue':
self.othercolor = 'red'
self.scoreline = 0
self.allyrankvalues = dict()
self.enemyrankvalues = dict()
self.alliedhidden = dict()
self.enemyhidden = dict()
def initializepiecelist(self,piecelist,board): #construct initial two way dict
#put first board into board list
self.truepiecelist = dict() #need a way to get actual piece from hash
self.uniquecount = 0
self.piecelist = piecelist
self.enemyhidden = dict()
for piece in piecelist:
self.truepiecelist[hash(piece)] = piece
self.uniquecount -= 1
#Give each enemy unit a unique negative number identifier
self.pieceident[self.uniquecount] = hash(piece)
#set the lists of allies and enemies to hidden first
self.alliedhidden[self.uniquecount] = 'h'
if piece.team != self.color:
self.enemyhidden[self.uniquecount] = 'h'
else:
self.alliedhidden[self.uniquecount] = 'h'
self.myboard = self.boardconvert(piecelist,board)
if self.color == 'red':
self.myboardsred.append(self.myboard)
elif self.color == 'blue':
self.myboardsblue.append(self.myboard)
def rank_value_calc(self,totalAllies,totalEnemies):
#Our pieces
basevalue=dict()
rankdifffactor=1.45
for i in range(12):
basevalue[str(i)]= 0.0
basevalue[str(1)] = .05
for i in range(2,11):
if (self.totalAllies[str(i-1)]+self.totalAllies[str(i)] > 0) and (self.totalEnemies[str(i-1)]+self.totalEnemies[str(i)]>0):
basevalue[str(i)] = basevalue[str(i-1)]*rankdifffactor
else:
basevalue[str(i)] = basevalue[str(i-1)]
#gets the highest value of basevalue for scaling
highest = max(basevalue.values())
#scale all the values and then add a value
for i in range(11):
basevalue[str(i)] = float(basevalue[str(i)])/float(highest)+(.5/sum(basevalue.values()))
#specific pieces with alternate values
#if there's an enemy spy, reduce the 10's importance
if self.totalEnemies[str(1)] != 0:
basevalue[str(10)] = basevalue[str(10)]*.8
#highest = max(basevalue.itervalues(), key=(lambda key: basevalue[key]))
highest = max(basevalue.values())
basevalue[str(1)] = basevalue[str(10)]/2
basevalue[str(0)] = highest+.5
basevalue[str(11)] = highest/2
#if there are fewer than 3 miners, make them more valuable
if self.totalAllies[str(3)] >= 3:
basevalue[str(3)] = basevalue[str(3)]*((4-self.totalAllies[str(3)])/2)
#if there are fewer than 3 scouts, make them more valuable
if self.totalAllies[str(2)] >= 3:
basevalue[str(2)] = basevalue[str(2)]*((4-self.totalAllies[str(2)])/3)
#if enemy unit is hidden and has moved, make its value higher than the 2
#if its hidden and has moved, make its value
return basevalue
def boardconvert(self,piecelist,boardd): #convert a board passed from the engine to here
#assign the unique identifier to the board position along with the piece
w, h = 8, 10;
i_board = [['-1' for x in range(h)] for y in range(w)] #internal function board
for x in range(len(boardd)):
for y in range(10):
for piece in piecelist:
if (x,y) == (casy(piece.rect.centery)-1,casx(piece.rect.centerx)):
i_board[x][y] = boardd[x][y]+"Q"+str(self.pieceident[hash(piece)])
return i_board
#def parsemove(self,boardd,previous_move):
#use previous move to determine if the piece is a two
def read_in_go(self,board_version_moves,turncount,previous_move,board):
#this function houses the main AI logic
#convert the given board to one the AI can use
self.myboard = self.boardconvert(self.piecelist,board)
if self.color == 'red':
self.myboardsred.append(self.myboard)
elif self.color == 'blue':
self.myboardsblue.append(self.myboard)
#todo: make way to check if unique identifer has been discovered or not, if not, replace board info with -2
#bring unique identifier along with piece
#count number of friendly pieces, if it went down by one, we lost someone to combat and know who they are
#need to implement the logic; find place that's different, and then see what piece is there and such
friendlycount = 0
if self.color == 'red':
for x in range(len(self.board)):
for y in range(10):
if 'r' in self.myboard[x][y]:
friendlycount+=1
if 'r' in self.myboardsred[-1]:
friendlycount+=1
elif self.color == 'blue':
for x in range(len(self.board)):
for y in range(10):
if 'r' in self.myboard[x][y]:
friendlycount+=1
if 'r' in self.myboardsblue[-1]:
friendlycount+=1
if self.color == 'red':
print(len(self.myboardsblue))
try:
if self.myboardsblue[-1] == self.myboardsblue[-2]:
print("They are the same, fuck you.")
else:
print("they are different.")
except IndexError:
gg=2
elif self.color == 'blue':
print(len(self.myboardsblue))
try:
if self.myboardsblue[-1] == self.myboardsblue[-2]:
print("They are the same, fuck you.")
else:
print("they are different.")
except IndexError:
gg=2
self.allyrankvalues = self.rank_value_calc(self.totalAllies,self.totalEnemies)
self.enemyrankvalues = self.rank_value_calc(self.totalEnemies,self.totalAllies)
#print(self.allyrankvalues)
#print(self.enemyrankvalues)
print(board)
#reverse it for the enemy calculations
#create a function that does the following:
#updates the enemy or allied piece lists from the last move
#for the objective function, sum the number of allied pieces that are hidden and subtract the number of enemy pieces that are
move = sys_random.choice(board_version_moves)
return move
#output turn count and board to a text file