-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAgent.py
300 lines (266 loc) · 12.2 KB
/
Agent.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#-*- coding: utf-8 -*-
from BasicDefinition import CardIndex
import mahjong
class Agent :
def __init__(self,player_number):
self.playerNumber = player_number
self.reset()
def reset(self):
self.handcard = None
self.cardsOnBoard = [[],[],[],[]]
self.cardsThrowed = [[],[],[],[]]
self.cardsThrowedNoTaken = []
self.cardOpened = []
self.wind =None
def setAction(self,action):
self.action = action
def goalTest(self):
s1 = [] #distinguish 4 color
s2 = []
s3 = []
s4 = []
self.handcard.sort()
combination = []
for card in self.handcard:
if 1 <= card <= 9:
s1.append(card)
elif 11 <= card <=19:
s2.append(card)
elif 21 <= card <= 29:
s3.append(card)
else:
s4.append(card)
s = [s1,s2,s3]
twoPair = 0
while len(s4) != 0:
count = s4.count(s4[0])
if count == 3:
combination.append([s4[0]]*3)
s4 = s4[3:]
elif count == 2:
if twoPair == 0:
twoPair += 1
combination.append([s4[0]]*2)
s4 = s4[2:]
else:
return False,None
else:
return False,None
for color in s:
if len(color) != 0:
## 整除
if len(color) % 3 == 0:
while len(color) != 0:
first = color[0]
## 檢查碰
if color[1] == first:
if color[2] == first:
color = color[3:]
combination.append([first]*3)
else:
return False,None
## 檢查順子
elif color[1] == first + 1:
if first + 2 in color:
color.remove(first + 2)
color = color[2:]
combination.append([first,first+1,first+2])
else:
return False,None
else:
return False,None
## 可能有pair
elif len(color) % 3 == 2:
if twoPair != 0:
return False,None
else:
i = 0
correct1 = False
while i < len(color):
count = color.count(color[i])
## 某類牌有2 3 4張在手上
if count > 1:
temp = color[:]
tempCombination = [[temp[i]]*2]
del temp[i:i+2]
correct2 = True
while len(temp) != 0:
first = temp[0]
if temp[1] == first:
if temp[2] == first:
temp = temp[3:]
tempCombination.append([first]*3)
else:
correct2 = False
break
elif temp[1] == first + 1:
if first + 2 in temp:
temp.remove(first + 2)
temp = temp[2:]
tempCombination.append([first,first+1,first+2])
else:
correct2 = False
break
else:
correct2 = False
break
if correct2:
correct1 = True
break
i +=count
if not correct1:
return False,None
combination = combination + tempCombination
else:
return False,None
return True,combination
################################
### 設定門風 和 取得門風 ###
################################
def setWind(self,wind):
assert self.wind == None
self.wind = wind
def getWind(self):
return self.wind
##########################################################
### initial the first 13 hand card in the begining ###
##########################################################
def initialHandCard(self,newHandcard):
self.handcard = newHandcard
#######################################################################
### Get a new card ###
### case 1 : Throw ###
### return state 'Throw' and a card the agent doesn't want ###
### case 2 : 胡 ###
### return state '胡' and None ###
#######################################################################
def takeAction(self,newCard,table):
if newCard != None :
self.handcard.append(newCard)
result,cardCombination = self.goalTest()
assert result or cardCombination == None
if result:
return '自摸',[cardCombination,self.cardsOnBoard[self.playerNumber]]
else:
return 'Throw',self.action(self.handcard,table)####
#####################################################################################
### check if agent can '吃' or '槓' by card and the agent who throw it ###
### if return state '吃' or '槓', should return cards that fit this situation ###
### if return state None, then return cards of None ###
### [[1,2,3],'吃',2] ###
#####################################################################################
def check(self,agentNum,card):
self.handcard.append(card)
result,cardCombination = self.goalTest()
if result:
return [[cardCombination,self.cardsOnBoard[self.playerNumber]], '胡', card]
self.handcard.remove(card)
subtract = self.playerNumber - agentNum
if subtract != 1 and subtract != -3 and self.handcard.count(card) == 3:
return [[card,card,card,card], '槓', card]
if self.handcard.count(card) == 2:
return [[card,card,card], '碰', card]
if subtract == 1 or subtract == -3:
if 1 <= card <= 9 or 11 <= card <= 19 or 21 <= card <=29:
if (card - 1 in self.handcard) and (card + 1 in self.handcard) and ((card - 1)%10 != 0) and ((card + 1)%10 != 0):
return [[card - 1,card,card + 1], '吃', card]
elif (card - 2 in self.handcard) and (card - 1 in self.handcard) and ((card - 2)%10 != 0) and ((card - 1)%10 != 0):
return [[card -2,card -1,card], '吃', card]
elif (card + 1 in self.handcard) and (card + 2 in self.handcard) and ((card + 1)%10 != 0) and ((card + 2)%10 != 0):
return [[card,card + 1,card +2], '吃', card]
return [[], '過', card]
#############################################
### update other player's information ###
### Example: ###
### 1 throw 東(30) ###
### 2 get 東 say "碰" ###
### update(1,2,[30,30,30],30) ###
#############################################
def update(self,throwAgent,takeAgent,cards,throwCard,verbose=False):
self.cardsThrowed[throwAgent].append(throwCard)
if takeAgent!=None:
self.cardsOnBoard[takeAgent].append(cards)
if takeAgent==self.playerNumber:
self.handcard.append(throwCard)
for card in cards:
self.handcard.remove(card)
else:
self.cardsThrowedNoTaken.append(throwCard)
if takeAgent != None:
for card in cards:
self.cardOpened.append(card)
else:
self.cardOpened.append(throwCard)
if verbose :
print ('Agent ',self.playerNumber)
print (" throwAgent ",throwAgent, "throwcard ", throwCard)
print (" 過去丟出的牌: ",self.cardsThrowed[self.playerNumber])
print (" 過去丟出的牌(沒被拿走):",self.cardsThrowedNoTaken)
print (" takeAgent, ",takeAgent)
print (" 打開的組合 : ", self.cardsOnBoard[self.playerNumber])
###########################
### print hand card ###
###########################
def printHandCard(self):
sortedList = self.handcard[:]
sortedList.sort()
print ('Agent ',self.playerNumber,end =' :')
for card in sortedList :
print (CardIndex[card],end=' ')
print ()
################################################################
### get cards on board of this agent (這個agent翻開的牌) ###
################################################################
def getCardsOnBoard(self):
cardsOnBoard = self.cardsOnBoard[self.playerNumber][:]
return cardsOnBoard
##################################
### calculate xiangtingshu ###
##################################
def cardTransform(self, handcard,ingroup =False):
outputStr = ''
transform = { 0:'5z', 10:'6z', 20:'7z',
1:'1m', 11:'1s', 21:'1p',
2:'2m', 12:'2s', 22:'2p',
3:'3m', 13:'3s', 23:'3p',
4:'4m', 14:'4s', 24:'4p',
5:'5m', 15:'5s', 25:'5p',
6:'6m', 16:'6s', 26:'6p',
7:'7m', 17:'7s', 27:'7p',
8:'8m', 18:'8s', 28:'8p',
9:'9m', 19:'9s', 29:'9p',
30:'1z', 31:'3z', 32:'2z', 33:'4z'
}
if not ingroup:
for card in handcard:
outputStr += transform[card]
if ingroup:
cardsincomb =[]
for cards in handcard:
comb = []
for card in cards:
comb.append(transform[card])
cardsincomb.append(comb)
outputStr = cardsincomb
return outputStr
def xiangtingshu(self, handcard , e = False):
p = self.playerNumber
xiangtingshuInfo,valuelist = mahjong.xiangtingshu_output(self.cardTransform(handcard),self.cardTransform(self.cardsOnBoard[p],True),evaluate=e)#新增12/27 將明牌tranform之後丟入
#[[11, 0, [15]], [14, 0, [16, 19]], ...] means [打1條,向聽數0,有效牌5條],[打4條,向聽數0,有效牌6條9條],...
xiangtingshuInfo_tuple = (xiangtingshuInfo,valuelist)
for i,case in enumerate(xiangtingshuInfo):
youxiaopaiNum = 0
for card in case[2]:
youxiaopaiNum += (4 - self.cardOpened.count(card) - handcard.count(card))
assert youxiaopaiNum >= 0,(handcard)
case.append(youxiaopaiNum)
if valuelist:
case.append(valuelist[i])
#print ('xiangtingshuInfo',xiangtingshuInfo)
return xiangtingshuInfo
######################################
### 一局遊戲結束呼叫的function ###
### 給learning Agent用的 ###
######################################
def gameEnd(self,win,lose,score):
pass