forked from LplusKira/repository_AI_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminijudge.py
428 lines (388 loc) · 16.3 KB
/
minijudge.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# -*- coding: utf-8 -*-
_InitCardsPerPlayer_ = 5
_TotalPlayerNum_ = 4
_IamDead_ = -1
_MaxCombCardNum_ = 5
_Adding_ = -1
_Minus_ = -2
_ClockWise_ = 1
_MaxPoint_ = 99
_cardNum_ = 52
_MaxActionLength_ = 20
_MaxComb_ = 32
import random
import time
import math
import copy
import sys
import argparse
from action import *
from ab_agent import PlayerState
from ab_agent import HeuristicAgent, RandomAgent
from logger import Game, logger
class PossibleCombination:
def __init__(self, comb = list()):
self.combination = comb
iter_num = 1
class MiniJudge:
def __init__(self, playerList = None, h = None, c = None, m=None, p=0, cw=1, cp=1, small_h = None):
players = list()
players.append(HeuristicAgent(1))
players.append(HeuristicAgent(2))
players.append(HeuristicAgent(3))
players.append(HeuristicAgent(4))
self.player = players
# because this attribute is mutable, use this way
# http://stackoverflow.com/questions/2681243/how-should-i-declare-default-values-for-instance-variables-in-python
if h is None:
self.history = list()
else:
self.history = h #action list
if c is None:
self.card = [[0 for x in range(5)] for x in range(4)]
else:
self.card = c # need to sort by cardvalue,two dimension list
if m is None:
self.mountain = list()
else:
self.mountain = m
self.point = p
self.clock_wise = cw
self.current_player = cp
# TODO: adding small history for players to remember cards (in case of 9 or 7 is enforced on any player)
if small_h is None:
self.small_h = [[] for i in range(_TotalPlayerNum_)]
else:
self.small_h = small_h
self._possibleActions_ = list()
self.winner = 0 # Monte
self.initBoard()
self.rand4Cards()
#self.printBoard()
def GameStart(self):
while not self.isGameFinished():
self._possibleActions_ = self.getAction()
if len(self._possibleActions_) == 0:
self.setDead(self.current_player)
self.changeNextPlayer()
continue
state = PlayerState(self.history, self._possibleActions_, self.card[self.current_player-1], len(self.card[0]), len(self.card[1]), len(self.card[2]), len(self.card[3]), len(self.mountain), self.point, self.clock_wise, self.small_h[self.current_player-1]) #get playerstate
# TODO: call up the current player to generate move
if self.current_player == 1:
a = self.player[self.current_player-1].genmove(state)
else:
a = self.player[self.current_player-1].genmove(state)
# TODO: clean current player's small history
self.Empty_small_h(self.current_player-1)
self.doAction(a)
self.winner = 0
for i in range(4):
if self.isDead[i] == False:
self.winner = i
return self.winner
def rand4Cards(self):
original_cards = list()
for i in range(_cardNum_):
original_cards.append(i + 1)
for i in range(_TotalPlayerNum_):
for counter in range(_InitCardsPerPlayer_):
pick = random.randint(0,1024) % len(original_cards)
self.card[i][counter] = original_cards[pick]
original_cards.pop(pick)
# TODO: set mountain
for i in range(len(original_cards)):
pick = random.randint(0, 1024) % len(original_cards)
self.mountain.append(original_cards[pick])
original_cards.pop(pick)
def randMountain(self):
# TODO: collecting remained cards into cards_remained
cards_remained = list()
for player in range(_TotalPlayerNum_):
for which_card in range(len(self.card[player])):
cards_remained.append(self.card[player][which_card])
# TODO: collecting cards which should be the new moutain
cards_remained.append(53)
cards_remained.append(0)
cards_remained.sort()
original_cards = list()
for grid_num in range(len(cards_remained) - 1):
for between_grid in range(cards_remained[grid_num] + 1, cards_remained[grid_num + 1], 1):
original_cards.append(between_grid)
# TODO: set mountain
for i in range(len(original_cards)):
pick = random.randint(0, 1024) % len(original_cards)
self.mountain.append(original_cards[pick])
original_cards.pop(pick)
"""
# check mountain is correct or not
for c in self.mountain:
for i in range(4):
if c in self.card[i]:
print "illegal randmountain"
exit()
"""
def initBoard(self):
self.current_player = 1
self.clock_wise = 1 #1 and -1
self.playerNum = 4
self.isDead = [False]*self.playerNum
self.clock_wise = _ClockWise_ #1 and -1
def isGameFinished(self):
deadcount = 0
for i in range(self.playerNum):
if self.isDead[i]:
deadcount += 1
if deadcount == self.playerNum -1:
return True
return False
def printBoard(self):
print "mountnum = %d, point = %d" % (len(self.mountain), self.point) + ".Now %dth Move. Player %d" % (len(self.history), self.current_player) + "\n" \
+ "North(id = 1):" + (getCardsString(self.card[0])) + "\n" \
+ "East(id = 2):" + (getCardsString(self.card[1])) + "\n"\
+ "South(id = 3):" + (getCardsString(self.card[2])) + "\n"\
+ "West(id = 4):" + (getCardsString(self.card[3]))
def Empty_small_h(self, which_player):
while len(self.small_h[which_player]) > 0:
self.small_h[which_player].pop()
def Push_small_h(self, new_action, victim):
#for card in range(len(residual_card)):
# new_action.cards_used.append(residual_card[card])
self.small_h[victim].append(new_action)
"""if victim == 0:
print "==========in judge========"
print "victim == " + str(victim)
for i in range(len(self.small_h[victim])):
print "usr == " + str(self.small_h[victim][i].user) + ", cards == "
for j in range(len(self.small_h[victim][i].cards_used)):
print self.small_h[victim][i].cards_used[j]
print "operation == " + str(self.small_h[victim][i].victim)
#time.sleep(1)
print "========end judge========"
"""
def doAction(self, a):
# TODO: add effect by the returning action a
self.history.append(a)
isZero = False
if len(a.cards_used) == 1:
actual_card = a.cards_used[0] % 13
if actual_card == 0:
actual_card = 13
if a.cards_used[0] == 1:
isZero = True
else:
actual_card = 0
for i in range(0, len(a.cards_used), 1):
actual_card += a.cards_used[i] % 13
for c in a.cards_used:
self.card[a.user-1].remove(c)
if a.victim == _Adding_: # if the action is NO harmful: 10, 12(e.g. +- 10; +- 20)
if actual_card == 12:
self.point += 20
elif actual_card == 10:
self.point += 10
elif a.victim == _Minus_:
if actual_card == 12:
self.point -= 20
elif actual_card == 10:
self.point -= 10
elif isZero: # else if the action is Spade 1 or 4, 5, 11, 13
self.point = 0
elif actual_card % 13 == 4:
self.clock_wise *= -1
elif actual_card % 13 == 5:
self.current_player = a.victim
elif actual_card % 13 == 11:
pass
elif actual_card % 13 == 0:
self.point = _MaxPoint_
elif actual_card % 13 == 7: # else if the action is 7, 9
pick = random.randint(0, len(self.card[a.victim - 1])-1)
self.card[a.user - 1].append(self.card[a.victim - 1][pick])
# TODO: adding small history to the victim and sending him the hsitory; the last card is the one picked
take_card = [self.card[a.victim-1][pick]]
self.card[a.victim - 1].pop(pick)
#self.card[a.victim - 1].append(self.card[a.user - 1][len(self.card[a.user - 1]) - 1])
new_action = Action(a.user, take_card, (len(self.history)-1)*10+7)
self.Push_small_h(new_action, a.victim - 1)
#self.card[a.victim - 1].pop(len(self.card[a.victim - 1]) - 1)
elif actual_card % 13 == 9:
temp = list()
for i in range(0, len(self.card[a.user - 1]), 1):
temp.append(self.card[a.user - 1][i])
for i in range(0, len(self.card[a.user - 1]), 1):
self.card[a.user - 1].pop()
for i in range(0, len(self.card[a.victim - 1]), 1):
self.card[a.user - 1].append(self.card[a.victim - 1][i])
for i in range(0, len(self.card[a.victim - 1]), 1):
self.card[a.victim - 1].pop()
for i in range(0, len(temp), 1):
self.card[a.victim - 1].append(temp[i])
# TODO: adding small history to the victim and sending him the hsitory
new_action = Action(a.user, self.card[a.user - 1], (len(self.history)-1)*10+9) # i want other player's card...
self.Push_small_h(new_action, a.victim - 1)
else: # else, cards in {1(not spade), 2, 3, 6, 8}
self.point += actual_card
# TODO: pop mountain, assign the card to current user
if not(actual_card % 13 == 7 or actual_card % 13 == 9):
if len(self.mountain) == 0:# if mountain is empty, "0 list() 0" will be inserted first
self.randMountain()
index_action = Action(0,[],0)
self.history.append(index_action)
#print "randmountain, now len = %d" % len(self.mountain)
self.card[a.user - 1].append(self.mountain[len(self.mountain) - 1])
self.mountain.pop()
# check dead
for i in range(self.playerNum):
if len(self.card[i]) == 0 and not self.isDead[i]:
self.setDead(i+1) # id
if actual_card % 13 != 5:
self.changeNextPlayer()
def setDead(self, playerid):
self.isDead[playerid-1] = True
self.card[playerid-1] = []
def changeNextPlayer(self):
self.current_player += self.clock_wise
if self.current_player < 0:
self.current_player += self.playerNum
while self.isDead[self.current_player%self.playerNum - 1]:
self.current_player += self.clock_wise
if self.current_player < 0:
self.current_player += self.playerNum
self.current_player %= self.playerNum
if self.current_player == 0:
self.current_player += self.playerNum
#print "next player is %d" % self.current_player
def getAction(self): # get legal action list
card = self.card[self.current_player-1]
isuse = [False]*len(card)
av = list()
a_template = Action(self.current_player)
while nextbool(isuse, len(card)):
a_card = copy.deepcopy(a_template)
nowv = 0
a_card.cards_used = []
iszero = False
for i in range(len(card)):
if isuse[i]:
if card[i] == 1:
iszero = True
cv = 13 if (card[i]%13 == 0) else card[i]%13
nowv += cv
a_card.cards_used.append(card[i])
if len(a_card.cards_used) == 1 and iszero:#special case, space one
a = copy.deepcopy(a_card)
a.victim = 0
av.append(a)
continue
if nowv > 13:
continue
if nowv == 7 or nowv == 9:
for i in range(_TotalPlayerNum_):
if i == self.current_player-1 or self.isDead[i]:
continue
a = copy.deepcopy(a_card)
a.victim = i+1
av.append(a)
elif nowv == 5:
for i in range(_TotalPlayerNum_):
if self.isDead[i]:
continue
a = copy.deepcopy(a_card)
a.victim = i+1
av.append(a)
elif nowv == 10 or nowv == 12:
value = 10 if (nowv == 10) else 20
if self.point + value <= 99:
a = copy.deepcopy(a_card)
a.victim = -1
av.append(a)
if self.point - value >= 0:
a = copy.deepcopy(a_card)
a.victim = -2
av.append(a)
elif nowv == 4 or nowv == 11 or nowv == 13:
a = copy.deepcopy(a_card)
a.victim = 0
av.append(a)
else:
if self.point + nowv <= 99:
a = copy.deepcopy(a_card)
a.victim = 0
av.append(a)
random.shuffle(av)
return av
def checkRule(self, a):
if a.user != self.current_player:
return False
for c in a.cards_used:
for i in range(4):
if i == a.user-1:
if c not in self.card[i]:
return False
elif c in self.card[i]:
return False
if c in self.mountain:
return False
cardValue = 0
cards = 0
iszero = False
for i in range(len(a.cards_used)):
cardValue += a.cards_used[i]
if a.cards_used[i] % 13 != 0:
cards += a.cards_used[i] % 13
else:
cards += 13
if(a.cards_used[i] == 1):#space one
iszero = True
if cards > 13:
return False
if iszero and cardValue == 1:
return True
cardValue = cardValue % 13
if cardValue == 7:
if a.victim > 0 and a.victim <= self.playerNum and a.victim != a.user and len(self.card[a.victim-1]) >= 1:
return True
elif cardValue == 9:
if a.victim > 0 and a.victim <= self.playerNum and a.victim != a.user:
return True
elif cardValue == 5:
if a.victim > 0 and a.victim <= self.playerNum:
return True
elif cardValue == 12 or cardValue == 10:
value = 20 if (cardValue % 13 == 12) else 10
if a.victim == -1 and self.point + value <= 99 or a.victim == -2 and self.point - value >= 0:
return True
elif cardValue == 4 or cardValue == 11 or cardValue == 13:
return True
else:
if self.point+cardValue <= 99:
return True
return False
def nextbool(vb, n):
nowv = 0
n = len(vb)
for i in range(n):
nowv *= 2
nowv += 1 if (vb[i]) else 0
nowv = nowv +1
if nowv >= math.pow(2, n):
return False
for i in range(n-1, -1, -1):
vb[i] = True if (nowv%2) else False
nowv /= 2
return True
if __name__ == "__main__" :
parser = argparse.ArgumentParser(description='Bloody99 judge')
parser.add_argument("-p", help="number of games to run", type=int, default=_TestGameNum_)
parser.add_argument('-f', '--file', metavar="", help="logger file name", default="bloody99log.txt") # can use 'tail -f <file>' to see the result
args = parser.parse_args()
f = open(args.file, "w")#clear
f.close()
i = 1 # no iterate? # i dont know
log = logger(args.file)
for k in range(args.p):
j = Judge()
players, winner = j.GameStart()
g = Game(i, players, winner)
log.logGame(g)
print log