-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscores.py
127 lines (108 loc) · 2.98 KB
/
scores.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
############ Create scoring functions ############
# Get dictionary of distinct counts
def getFreqDic(dice):
freqDic = {}
for i in dice:
if i in freqDic:
freqDic[i] += 1
else:
freqDic[i] = 1
return freqDic
# Score 1s
def scoreOnes(dice):
n=1
return sum([i for i in dice if i == n])
# Score 2s
def scoreTwos(dice):
n=2
return sum([i for i in dice if i == n])
# Score 3s
def scoreThrees(dice):
n=3
return sum([i for i in dice if i == n])
# Score 4s
def scoreFours(dice):
n=4
return sum([i for i in dice if i == n])
# Score 5s
def scoreFives(dice):
n=5
return sum([i for i in dice if i == n])
# Score 6s
def scoreSixes(dice):
n=6
return sum([i for i in dice if i == n])
# 3 of a kind
def scoreThreeOfKind(dice):
freqDic = getFreqDic(dice)
freqList = list(freqDic.values())
if 3 in freqList:
return sum(dice)
else:
return 0
# 4 of a kind
def scoreFourOfKind(dice):
freqDic = getFreqDic(dice)
freqList = list(freqDic.values())
if 4 in freqList:
return sum(dice)
else:
return 0
# Full house (2 pair and 3 pair)
def scoreFullHouse(dice):
freqDic = getFreqDic(dice)
freqList = list(freqDic.values())
if 2 in freqList and 3 in freqList:
return 25
else:
return 0
# Small straight (3 in a row)
def scoreSmStraight(dice):
for i in range(0,3):
if dice[i] == dice[i+1]-1 == dice[i+2]-2:
return 30
else:
return 0
# Large straight (4 in a row)
def scoreLgStraight(dice):
for i in range(0,2):
if dice[i] == dice[i+1]-1 == dice[i+2]-2 == dice[i+3]-3:
return 40
else:
return 0
# Yahtzee (5 of the same)
def scoreYahtzee(dice):
if len(set(dice)) == 1:
return 50
else:
return 0
# Chance (add everything)
def scoreChance(dice):
return sum(dice)
# Get potential scores
def getScores(dice):
d = {}
d['1s'] = scoreOnes(dice)
d['2s'] = scoreTwos(dice)
d['3s'] = scoreThrees(dice)
d['4s'] = scoreFours(dice)
d['5s'] = scoreFives(dice)
d['6s'] = scoreSixes(dice)
d['Three of a Kind'] = scoreThreeOfKind(dice)
d['Four of a Kind'] = scoreFourOfKind(dice)
d['Full House'] = scoreFullHouse(dice)
d['Small Straight'] = scoreSmStraight(dice)
d['Large Straight'] = scoreLgStraight(dice)
d['Yahtzee'] = scoreYahtzee(dice)
d['Chance'] = scoreChance(dice)
return d
# sort a dictionary based on priority keys
def specialSort(dic, priority):
# sort priority scores
priorityDic = sorted(priority, key=lambda key: dic[key], reverse=True)
# sort non-prioirty scores
otherKeys = [key for key in dic if key not in priority]
nonPriorityKeys = sorted(otherKeys, key=lambda key: dic[key], reverse=True)
# combine score dictionaries
comboDic = dict([(key, dic[key]) for key in priorityDic] + [(key, dic[key]) for key in nonPriorityKeys])
return comboDic