-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNCGT-TexasHoldemPoke.py
354 lines (289 loc) · 12 KB
/
NCGT-TexasHoldemPoke.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import socket
import re
import random
import time
AIname = "Alice"
ServerAddress= "127.0.0.1"
ServerPort=10001
class TexCore:
def __init__(self):
self.totalChip = 200000 # 当前筹码数
self.expectRate = 0.8 # 允许下注的筹码
self.gameStatus = 0 # 游戏阶段 第n轮下注
self.isBigblind = 0 # 1-大盲注 0小盲注
self.nowRaise = 0 # 当前加注金额
self.receiveFlag = 1 # 是否在接收状态
self.respone = ''
self.currentScore = 0
def NameHandel(self):
self.receiveFlag = 0
self.respone = AIname
return
def PrefHandel(self,posi,cards):
self.gameStatus=1
self.isBigblind=posi
self.handCards=cards
self.GetCardValue()
if self.isBigblind== 0:#小盲注直接从call开始
self.receiveFlag=0
self.respone = 'call'
else : #玩家是大盲注
self.receiveFlag = 1 # 等待对手
self.nowRaise = 200
self.respone = 'raise ' + str(self.nowRaise)
return
#这里是给牌进行估值
def FlopHandel(self,cards):
self.gameStatus = 2
self.nowRaise = 0
self.flopCards = cards
self.GetCardValue()
if self.isBigblind == 1: #大盲注 先默认过牌
self.receiveFlag = 0
if self.currentScore == 0:
self.respone = 'check'
else:
self.nowRaise = 400
self.respone = 'raise ' + str(self.nowRaise)
else:
self.receiveFlag = 1 # 等待对手
def TurnHandel(self,cards):
self.gameStatus = 3
self.nowRaise=0
self.turnCards=cards
self.GetCardValue()
if self.isBigblind == 1: #大盲注 过牌或者默认来两百
self.receiveFlag = 0
if self.currentScore>3 and self.nowRaise<8000:
self.nowRaise = 200
self.respone='raise ' + str(self.nowRaise)
else:
self.respone='check'
else:
self.receiveFlag = 1 # 等待对手
def RiverHandel(self,cards):
self.gameStatus = 4
self.nowRaise = 0
self.riverCards = cards
self.GetCardValue()
if self.isBigblind == 1: # 大盲注 过牌或者默认来两百
self.receiveFlag = 0
if self.currentScore>2 and self.nowRaise < 8000:
self.nowRaise = 200
self.respone = 'raise ' + str(self.nowRaise)
else:
self.respone = 'check'
else:
self.receiveFlag = 1 # 等待对手
def CallHandel(self):
#收到盲注之后就会收到他,所以这里要干两件事
#respone在之前prefhandl已经配置完了
self.receiveFlag = 0
return
def RaiseHandel(self, count):
#精髓
self.receiveFlag = 0
self.nowRaise=count
if game.gameStatus==1: #盲注阶段就默认跟
#self.receiveFlag = 0
self.respone='call'
else:
if self.currentScore>4 :
#if random.random() > 0.5:
if self.nowRaise<6000:
self.nowRaise = self.nowRaise*2+random.randint(1,10)
self.respone='raise ' + str(self.nowRaise) # 准备一个默认回复
else:
self.respone = 'allin'
elif self.currentScore>1:
if self.nowRaise<5000:
self.nowRaise = self.nowRaise*2+random.randint(1,10)
self.respone='raise ' + str(self.nowRaise) # 准备一个默认回复
else:
self.respone = 'call'
elif self.currentScore<2:
if self.nowRaise<5000:
self.respone='call'
else:
self.respone = 'fold'
else:
self.respone = 'call'
def CheckHandel(self):
self.receiveFlag = 1
self.nowRaise = 0
if self.gameStatus == 1:
self.receiveFlag = 0
self.respone = 'call'
elif ((self.currentScore > 0) and self.nowRaise<5000):
self.receiveFlag = 0
self.nowRaise = 200
self.respone = 'raise ' + str(self.nowRaise) # 准备一个默认回复
else:
self.receiveFlag = 0
self.respone = 'call'
def AllinHandel(self):
self.receiveFlag = 0
if self.currentScore>3:
self.respone = 'call'
else:
self.respone = 'fold'
def GetRespone(self):
self.receiveFlag = 1
return self.respone
def GetCardValue(self):
#每阶段只调用一次
self.currentScore = 0
if self.gameStatus == 1:
if self.handCards[0][1]==self.handCards[1][1]:
self.currentScore += 1
if self.handCards[0][1]>7 or self.handCards[0][1]>7:
self.currentScore += 1
if self.gameStatus == 2:
for each in self.flopCards:
if each[1] == self.handCards[0][1]:
self.currentScore += 2#潜在的两条 两对
if each[1] == self.handCards[1][1]:
self.currentScore += 4#潜在的三条 两对
flopMax = max(self.flopCards[0][1],self.flopCards[1][1],self.flopCards[2][1])
handMax = max(self.handCards[0][1],self.handCards[1][1])
if handMax>flopMax:#手中最大大于公共最大
self.currentScore += 1
if self.flopCards[0][0] == self.flopCards[1][0] == self.flopCards[2][0]:
if self.handCards[0][0] == self.flopCards[0][0]:
self.currentScore+=1
if self.handCards[1][0] == self.flopCards[0][0]:
self.currentScore += 1
if any([self.flopCards[0][0] == self.flopCards[1][0],
self.flopCards[0][0] == self.flopCards[2][0],
self.flopCards[1][0] == self.flopCards[2][0]]):
self.currentScore += 1
if self.gameStatus == 3:
for each in self.flopCards:
if each[1] == self.handCards[0][1]:
self.currentScore += 1#潜在的两条 两对
if each[1] == self.handCards[1][1]:
self.currentScore += 2#潜在的三条 两对
for each in self.turnCards:
if each[1] == self.handCards[0][1]:
self.currentScore += 1#潜在的两条 两对
if each[1] == self.handCards[1][1]:
self.currentScore += 2#潜在的三条 两对
Max = max(self.turnCards[0][1],self.flopCards[0][1],self.flopCards[1][1],self.flopCards[2][1])
handMax = max(self.handCards[0][1],self.handCards[1][1])
if handMax>Max:#手中最大大于公共最大
self.currentScore += 1
if self.flopCards[0][0] == self.flopCards[1][0] == self.flopCards[2][0]:
if self.handCards[0][0] == self.flopCards[0][0]:
self.currentScore+=1
if self.handCards[1][0] == self.flopCards[0][0]:
self.currentScore += 1
if any([self.flopCards[0][0] == self.flopCards[1][0],
self.flopCards[0][0] == self.flopCards[2][0],
self.flopCards[1][0] == self.flopCards[2][0]]):
self.currentScore += 1
if self.gameStatus == 4:
for each in self.flopCards:
if each[1] == self.handCards[0][1]:
self.currentScore += 1#潜在的两条 两对
if each[1] == self.handCards[1][1]:
self.currentScore += 2#潜在的三条 两对
for each in self.turnCards:
if each[1] == self.handCards[0][1]:
self.currentScore += 1#潜在的两条 两对
if each[1] == self.handCards[1][1]:
self.currentScore += 2#潜在的三条 两对
for each in self.riverCards:
if each[1] == self.handCards[0][1]:
self.currentScore += 1#潜在的两条 两对
if each[1] == self.handCards[1][1]:
self.currentScore += 2#潜在的三条 两对
Max = max(self.turnCards[0][1],self.flopCards[0][1],self.flopCards[1][1],self.flopCards[2][1])
handMax = max(self.handCards[0][1],self.handCards[1][1])
if handMax>Max:#手中最大大于公共最大
self.currentScore += 1
if self.flopCards[0][0] == self.flopCards[1][0] == self.flopCards[2][0]:
if self.handCards[0][0] == self.flopCards[0][0]:
self.currentScore+=1
if self.handCards[1][0] == self.flopCards[0][0]:
self.currentScore += 1
if any([self.flopCards[0][0] == self.flopCards[1][0],
self.flopCards[0][0] == self.flopCards[2][0],
self.flopCards[1][0] == self.flopCards[2][0]]):
self.currentScore += 1
else:
pass
print(str(self.gameStatus)+'估值'+str(self.currentScore))
return
def MsgHandel(msg):
respone = ''
#patternCard = re.compile(r'<.*?,.*?>')#匹配<,>
patternSuit = re.compile(r'<.*?,')#匹配<,
patternRank = re.compile(r',.*?>')#匹配,>
patternRaise = re.compile(r'raise.*')
#patternCheck = re.compile(r'check.*')
#patternAllin = re.compile(r'allin.*')
if(msg[0:4] == 'name'):
game.NameHandel()
elif(msg[0:4] == 'pref'):
#cards = patternCard.findall(msg)
cardsInArray = [
[int(patternSuit.findall(msg)[0][1:-1]),int(patternRank.findall(msg)[0][1:-1])],
[int(patternSuit.findall(msg)[1][1:-1]),int(patternRank.findall(msg)[1][1:-1])]]
game.PrefHandel(0 if 'SMALLBLIND'in msg else 1 ,cardsInArray)
#respone = game.GetRespone()
elif(msg[0:4] == 'flop'):
cardsInArray = [
[int(patternSuit.findall(msg)[0][1:-1]), int(patternRank.findall(msg)[0][1:-1])],
[int(patternSuit.findall(msg)[1][1:-1]), int(patternRank.findall(msg)[1][1:-1])],
[int(patternSuit.findall(msg)[2][1:-1]), int(patternRank.findall(msg)[2][1:-1])]]
game.FlopHandel(cardsInArray)
elif(msg[0:4] == 'turn'):
cardsInArray = [
[int(patternSuit.findall(msg)[0][1:-1]), int(patternRank.findall(msg)[0][1:-1])]]
game.TurnHandel(cardsInArray)
elif(msg[0:4] == 'rive'):
cardsInArray = [
[int(patternSuit.findall(msg)[0][1:-1]), int(patternRank.findall(msg)[0][1:-1])]]
game.RiverHandel(cardsInArray)
elif(msg[0:4] == 'earn'):
game.__init__()
pass
elif(msg[0:4] == 'oppo'):
pass
#需要回复的命令
if('call'in msg):
#理论上只有盲注阶段会收到这个命令
game.CallHandel()
#respone = game.GetRespone()
if ('raise' in msg):
msg = patternRaise.findall(msg)[0]
count = int(msg[6:])
game.RaiseHandel(count)
if ('check' in msg):
#msg = patternCheck.findall(msg)[0]
game.CheckHandel()
if ('allin' in msg):
#msg = patternAllin.findall(msg)[0]
game.AllinHandel()
#respone = game.GetRespone()
else:
pass
return
if __name__=='__main__':
print("晚自习系统开始啦啊啊啊啊啊啊啊啊啊啊")
connectTex = socket.socket()
connectTex.connect((ServerAddress, ServerPort))
game = TexCore()
print("链接创建成功,游戏实例化")
while 1:
while game.receiveFlag :
msg = str(connectTex.recv(1024),encoding="utf-8")
if msg != '':
print("RX:"+msg)
respone = MsgHandel(msg)
if game.receiveFlag == 0 :
respone = game.GetRespone()
print("TX:" + respone)
time.sleep(0.3)
connectTex.send(respone.encode('utf-8'))
#connctTex.close()不太确定啥时候加