dice.html
001| '''Dice rolls and odds.
001|
001| D odds_% winning_rolls possible_rolls
001| --------------------------------------------
001| 1 16.667 1 6
001| 2 30.556 11 36
001| 3 42.130 91 216
001| 4 51.775 671 1296
001| 5 59.812 4651 7776
001| 6 66.510 31031 46656
001| 7 72.092 201811 279936
001| 8 76.743 1288991 1679616
001| 9 80.619 8124571 10077696
001| 10 83.849 50700551 60466176
001| 11 86.541 313968931 362797056
001| 12 88.784 1932641711 2176782336
001| 13 90.654 11839990891 13060694016
001| 14 92.211 72260648471 78364164096
001| 15 93.509 439667406451 470184984576
001| 16 94.591 2668522016831 2821109907456
001| 17 95.493 16163719991611 16926659444736
001| 18 96.244 97745259402791 101559956668416
001| 19 96.870 590286253682371 609359740010496
001| 20 97.392 3560791008422351 3656158440062976
001|
001| commands:
001| main - Main function (normal use)
001| demo - Demo function (show off and test app)
001| test - Test function (development)
001|
001| usage:
001| module.dispatch({command}, args={arguments}) -- calling module from code
001|
001| $PYTHON module_name {command} {arguments} -- executing module externally
001|
001| '''
002|
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path
006| from random import random
007| from logger import logger, info
008|
009| BasePath = r'C:\CJS\prj\Python\app\maths'
010| Log = logger('dice')
011|
012| BASE = 6
013|
014|
015|
016| def generate_all_possible_rolls (dice=6):
017| rs = []
018| r = [1] * dice
019| while r:
020| rs.append(tuple(r))
021| r = generate_next_roll(r)
022| return rs
023|
024| def generate_next_roll (roll):
025| ix = len(roll) - 1
026| while 0 <= ix:
027| roll[ix] += 1
028| if BASE < roll[ix]:
029| roll[ix] = 1
030| ix -= 1
031| else:
032| break
033| return None if ix < 0 else roll
034|
035|
036|
037| def roll_six_stats (roll, dice=6):
038| '''Returns rolls stats on a roll of six dice. {roll: list of six dice}'''
039| zs = map(lambda x: x+1, range(dice))
040| stats = {'roll':roll, 'zs':zs}
041| stats['1s'] = sum(map(lambda x: 1 if x==1 else 0, roll))
042| stats['5s'] = sum(map(lambda x: 1 if x==5 else 0, roll))
043| stats['str8'] = (0 == sum(map(lambda x,y: abs(x-y), sorted(roll), zs)))
044| of_a_kind = [0] * BASE
045| for d in roll:
046| of_a_kind[d-1] += 1
047| stats['oak'] = of_a_kind
048| stats['2oak'] = sum(map(lambda x: 1 if x==2 else 0, of_a_kind))
049| stats['3oak'] = sum(map(lambda x: 1 if x==3 else 0, of_a_kind))
050| stats['4oak'] = sum(map(lambda x: 1 if x==4 else 0, of_a_kind))
051| stats['5oak'] = sum(map(lambda x: 1 if x==5 else 0, of_a_kind))
052| stats['6oak'] = sum(map(lambda x: 1 if x==6 else 0, of_a_kind))
053| stats['4+2'] = ((stats['4oak'] == 1) and (stats['2oak'] == 1))
054| stats['3+3'] = (stats['3oak'] == 2)
055| stats['3P'] = (stats['2oak'] == 3)
056| return stats
057|
058|
059| def print_roll_stats (stats):
060| print()
061| print('Roll: %s' % str(stats['roll']))
062| print('1s:%d, 5s:%d' % (stats['1s'], stats['5s']))
063| print('2oak:%d, 3oak:%d, 4oak:%d, 5oak:%d, 6oak:%d' % (stats['2oak'],stats['3oak'],stats['4oak'],stats['5oak'],stats['6oak']))
064| print('3+3:%s, 4+2:%s, 3P:%s' % (stats['3+3'], stats['4+2'], stats['3P']))
065| print('straight: %s' % str(stats['str8']))
066| print('OAK: %s' % str(stats['oak']))
067| print()
068|
069|
070|
071| def odds_of_a_number (dice=6):
072| '''Implements: SUM:dice-1:i=0:[(B^i)*((B-1)^((dice-1)-i))] where: B=6; dice=#-of-dice.'''
073| m = BASE ** dice
074| ix = range(dice)
075| ls = [BASE ** n for n in ix]
076| rs = [(BASE-1) ** ((dice-1)-n) for n in ix]
077| ss = map(lambda a,b: a*b, ls, rs)
078| n = sum(ss)
079| return [100.0 * (float(n)/float(m)), n, m, ss, rs, ls]
080|
081|
082|
083|
084| def do_odds (*args):
085| Log.info('odds/parameters: %d' % len(args))
086|
087| odds = []
088| print()
089| for n in range(20):
090| g = odds_of_a_number(n+1)
091| print('%8.3f %16d %16d' % tuple(g[:3]))
092| odds.append(g)
093| return 'Done'
094|
095|
096| def do_list (*args):
097| Log.info('list/parameters: %d' % len(args))
098| rolls = generate_all_possible_rolls()
099| fn = path.join(BasePath, 'dice.lst')
100| fp = open(fn, mode='w', encoding='utf-8')
101| try:
102| print('Possible Rolls (%d):' % len(rolls), file=fp)
103| for ix,r in enumerate(rolls):
104| g = roll_six_stats(r)
105| t = (g['1s'],g['5s'], g['2oak'],g['3oak'],g['4oak'],g['5oak'],g['6oak'], g['3+3'],g['4+2'],g['3P'],g['str8'])
106| s = '%d %d [%d %d %d %d %d] %5s %5s %5s %5s'
107| print('%5d: %s %s' % (ix, r, (s % t)), file=fp)
108| print(file=fp)
109| except:
110| raise
111| finally:
112| fp.close()
113| return 'Done'
114|
115|
116| def do_demo (*args):
117| Log.info('demo/parameters: %d' % len(args))
118| d1 = int(args[0]) if 0 < len(args) else int((random()*BASE)+1)
119| d2 = int(args[1]) if 1 < len(args) else int((random()*BASE)+1)
120| d3 = int(args[2]) if 2 < len(args) else int((random()*BASE)+1)
121| d4 = int(args[3]) if 3 < len(args) else int((random()*BASE)+1)
122| d5 = int(args[4]) if 4 < len(args) else int((random()*BASE)+1)
123| d6 = int(args[5]) if 5 < len(args) else int((random()*BASE)+1)
124| roll = [d1, d2, d3, d4, d5, d6]
125| g = roll_six_stats(roll)
126| print_roll_stats(g)
127| return 'Done'
128|
129|
130| def do_test (*args):
131| Log.info('test/parameters: %d' % len(args))
132| rolls = generate_all_possible_rolls()
133| stats = map(lambda x: roll_six_stats(x), rolls)
134| possible_rolls = float(len(rolls))
135| ones = 0
136| fives = 0
137| one_or_five = 0
138| straights = 0
139| six_oak = 0
140| five_oak = 0
141| four_oak = 0
142| three_oak = 0
143| four_two = 0
144| two_triples = 0
145| three_pairs = 0
146| for stat in stats:
147| if stat['1s']: ones += 1
148| if stat['5s']: fives += 1
149| if stat['1s'] or stat['5s']: one_or_five += 1
150| if stat['str8']: straights += 1
151| if stat['6oak']: six_oak += 1
152| if stat['5oak']: five_oak += 1
153| if stat['4oak']: four_oak += 1
154| if stat['3oak']: three_oak += 1
155| if stat['4+2']: four_two += 1
156| if stat['3+3']: two_triples += 1
157| if stat['3P']: three_pairs += 1
158|
159| fn = path.join(BasePath, 'dice.out')
160| fp = open(fn, mode='w', encoding='utf-8')
161| try:
162| print('Roll Stats:', file=fp)
163| print('===========', file=fp)
164| print('Dice: 6', file=fp)
165| print('Sides: %d' % BASE, file=fp)
166| print('Possible rolls: %d' % possible_rolls, file=fp)
167| print(file=fp)
168| print('Rolls with a 1: %d (%.2f%%)' % (ones, 100.0*(ones/possible_rolls)), file=fp)
169| print('Rolls with a 5: %d (%.2f%%)' % (fives, 100.0*(fives/possible_rolls)), file=fp)
170| print('Rolls with a 1 or 5: %d (%.2f%%)' % (one_or_five, 100.0*(one_or_five/possible_rolls)), file=fp)
171| print(file=fp)
172| print('Straights: %d (%.2f%%)' % (straights, 100.0*(straights/possible_rolls)), file=fp)
173| print(file=fp)
174| print('Three Pairs: %d (%.2f%%)' % (three_pairs, 100.0*(three_pairs/possible_rolls)), file=fp)
175| print(file=fp)
176| print('4+2: %d (%.2f%%)' % (four_two, 100.0*(four_two/possible_rolls)), file=fp)
177| print('3+3: %d (%.2f%%)' % (two_triples, 100.0*(two_triples/possible_rolls)), file=fp)
178| print(file=fp)
179| print('6-of-a-kind: %5d (%7.4f%%)' % (six_oak, 100.0*(six_oak/possible_rolls)), file=fp)
180| print('5-of-a-kind: %5d (%6.3f%%)' % (five_oak, 100.0*(five_oak/possible_rolls)), file=fp)
181| n = four_oak - four_two
182| print('4-of-a-kind: %5d (%5.2f%%) [raw: %d]' % (n, 100.0*(n/possible_rolls), four_oak), file=fp)
183| n = three_oak - two_triples
184| print('3-of-a-kind: %5d (%5.2f%%) [raw: %d]' % (n, 100.0*(n/possible_rolls), three_oak), file=fp)
185| print(file=fp)
186| n = 0
187| print(file=fp)
188| for stat in stats:
189| oak = stat['oak']
190| if (oak[0]==0) and (oak[4]==0) and (max(oak) < 3) and (not stat['3P']):
191| n += 1
192| print('Farkle: %d (%.2f%%)' % (n, 100.0*(n/possible_rolls)), file=fp)
193| print(file=fp)
194| except:
195| raise
196| finally:
197| fp.close()
198| return 'Done'
199|
200| def do_main (*args):
201| Log.info('main/parameters: %d' % len(args))
202| return 'Done'
203|
204| def dispatch (cmd, *args):
205| Log.info('command: %s' % cmd)
206| Log.info('arguments: %d' % len(args))
207| Log.info()
208| if cmd == 'odds': return do_odds(*args)
209| if cmd == 'list': return do_list(*args)
210| if cmd == 'demo': return do_demo(*args)
211| if cmd == 'test': return do_test(*args)
212| if cmd == 'main': return do_main(*args)
213| return 'Nothing to do!'
214|
215| if __name__ == '__main__':
216| print('autorun: %s' % argv[0])
217| Log.start(path.join(BasePath,'dice.log'))
218| Log.level(info())
219| Log.info(argv[0])
220| Log.info()
221| cmd = argv[1] if 1 < len(argv) else ''
222| etc = argv[2:] if 2 < len(argv) else []
223| try:
224| print('CMD: %s' % cmd)
225| print()
226| obj = dispatch(cmd, *etc)
227| print()
228| print(obj)
229| Log.info()
230| Log.info(obj)
231| except:
232| etype, evalue, tb = exc_info()
233| ts = extract_tb(tb)
234| Log.error()
235| Log.error('%s: %s' % (etype.__name__,str(evalue)))
236| Log.error()
237| for t in ts[-5:]:
238| Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
239| Log.error(' %s' % t[3])
240| Log.error()
241| raise
242| finally:
243| Log.end()
244|
245| '''eof'''
246|