Last active
August 29, 2015 14:14
-
-
Save rakjin/95087bab837ba8e9bdf0 to your computer and use it in GitHub Desktop.
sdg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
player_num: 1 | |
mean: 39, stdev: 30, min: 3, max: 276 | |
mean+2stdev: 101 | |
player_num: 2 | |
mean: 48, stdev: 33, min: 5, max: 322 | |
mean+2stdev: 114 | |
player_num: 3 | |
mean: 55, stdev: 35, min: 7, max: 323 | |
mean+2stdev: 126 | |
player_num: 4 | |
mean: 62, stdev: 38, min: 9, max: 321 | |
mean+2stdev: 138 | |
player_num: 5 | |
mean: 67, stdev: 40, min: 11, max: 400 | |
mean+2stdev: 147 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import random | |
import numpy | |
PRINT_DEBUG = False | |
def print_debug(string): | |
global PRINT_DEBUG | |
if PRINT_DEBUG: | |
print(string) | |
def get_board(start=1, finish=100): | |
spots = [] | |
spot_start = start | |
spot_finish = finish | |
prev_spot = None | |
for i in range(spot_start, spot_finish+1): | |
spot = dict(display_num=i) | |
if prev_spot: | |
prev_spot['next'] = spot | |
prev_spot = spot | |
spots.append(spot) | |
spot = tuple(spot) | |
shortcuts = ( | |
(4, 16), | |
(8, 12), | |
(18, 38), | |
(20, 74), | |
(22, 2), | |
(24, 36), | |
(28, 6), | |
(32, 56), | |
(34, 46), | |
(40, 60), | |
(44, 26), | |
(48, 54), | |
(58, 42), | |
(66, 14), | |
(68, 52), | |
(70, 88), | |
(72, 50), | |
(76, 86), | |
(80, 100), | |
(84, 62), | |
(90, 92), | |
(94, 64), | |
(96, 82), | |
(98, 78), | |
) | |
for shortcut in shortcuts: | |
spot_from = [x for x in spots if x['display_num'] == shortcut[0]][0] | |
spot_to = [x for x in spots if x['display_num'] == shortcut[1]][0] | |
spot_from['goto'] = spot_to | |
return spots | |
def is_last(spot): | |
return not spot.get('next') | |
def print_turn(turn): | |
print_debug('TURN: %d' % turn) | |
def print_dice(player, dice): | |
print_debug('\t%s dice: %d' % (player['name'], dice)) | |
def print_player(player): | |
print_debug('\t%s at %d' % (player['name'], player['at']['display_num'])) | |
def play_and_get_turns_num(board, players_num=2): | |
turn = 0 | |
players = [dict(name='Player %d'%i, at=board[0]) for i in range(0, players_num)] | |
players = tuple(players) | |
finished = False | |
while True: | |
for player in players: | |
turn = turn+1 | |
print_turn(turn) | |
dice = random.randrange(1, 7) | |
print_player(player) | |
print_dice(player, dice) | |
for _ in range(0, dice): | |
next = player['at'].get('next') | |
if next: | |
player['at'] = next | |
else: | |
finished = True | |
break | |
print_player(player) | |
goto = player['at'].get('goto') | |
if goto: | |
player['at'] = goto | |
print_player(player) | |
if is_last(player['at']): | |
finished = True | |
if finished: | |
break | |
if finished: | |
break | |
return turn | |
def print_figures(player_num): | |
board = get_board() | |
turn_nums = \ | |
[play_and_get_turns_num(board, player_num) for x in xrange(0, 10000)] | |
mean = numpy.mean(turn_nums) | |
stdev = numpy.std(turn_nums) | |
amin = numpy.amin(turn_nums) | |
amax = numpy.amax(turn_nums) | |
print('player_num: %d' % player_num) | |
print('mean: %d, stdev: %d, min: %d, max: %d' % | |
(mean, stdev, amin, amax)) | |
print('mean+2stdev: %d' % (mean+2*stdev)) | |
print('') | |
def main(): | |
print_figures(1) | |
print_figures(2) | |
print_figures(3) | |
print_figures(4) | |
print_figures(5) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment