-
Notifications
You must be signed in to change notification settings - Fork 0
/
computer.py
59 lines (46 loc) · 1.39 KB
/
computer.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
# computer.py -- sample liar's dice robot
import random,logging
def get_play(me,hands,history) :
# figure out the previous call
#
if 0 == len(history) :
prev_quantity,prev_face = 0,0
else :
x = int(history.split(',')[-1].split(':')[1])
prev_quantity,prev_face = x // 10,x % 10
# showdown? if so, just ignore
#
if 0 == prev_quantity :
return 0
# count the total number of dice
#
num_dice = sum(map(lambda x : len(x.split(':')[1]),hands.split(',')))
# find my hand
#
my_hand = None
for i in hands.split(',') :
who,dice = i.split(':')
if who == me :
my_hand = dice
break
# try 4 times to find pick
# a random better play, if
# can't, just call
#
for i in range(4) :
# pick a random face from my dice
#
face = int(my_hand[random.randint(0,len(my_hand) - 1)])
# pick a random quantity, skew it low
#
quantity = 1 + int(abs(random.normalvariate(0.0,num_dice / 6.0)))
# is it a bigger call than the previous?
#
if (quantity > prev_quantity) or ((quantity == prev_quantity) and (face > prev_face)) :
# if so, return it
#
return (quantity * 10) + face
# nothing found, just call
#
logging.debug('nothing found, calling ...')
return 0