importsys |
importnumpyasnp |
'' |
Simulate chutes and ladders. |
Reports the number of moves for 1-player to reach the end, |
followed by the list of rolls that player had. |
Run as follows for 100000 games with 1 player. Report the total |
number of moves made by the winning player: |
python cl.py 100000 1 |
1000 games with 4 players |
python cl.py 1000 4 |
'' |
chutes= { |
16:6, 47:26, 49:11, 56:53, 62:19, |
64:60, 87:24, 93:73, 95:75, 98:78 |
} |
ladders= { |
1:38, 4:14, 9:31, 21:42, 28:84, |
36:44, 51:67, 71:91, 80:100 |
} |
defroll(sides=20): |
'' |
Roll a fair die of arg(sides) sides |
'' |
returnnp.random.randint(1,sides+1) |
definitialize_game(num_players): |
'' |
Reset the game. All players at position 0. |
'' |
p_curr= {} |
p_moves= {} |
p_rolls= {} |
forpinrange(num_players): |
p_curr[p] =0 |
p_moves[p] =0 |
p_rolls[p] = [] |
returnp_curr, p_moves, p_rolls |
num_games=int(sys.argv[1]) |
num_players=int(sys.argv[2]) |
end=100 |
forgameinrange(num_games): |
# setup the game |
winner=None |
total_moves=0 |
p_curr, p_moves, p_rolls=initialize_game(num_players) |
whilewinnerisNone: |
forpinrange(num_players): |
r=roll() |
total_moves+=1 |
p_moves[p] +=1 |
p_curr[p] +=r |
p_rolls[p].append(r) |
# house rules. land on or exceed 100. |
ifp_curr[p] >=end: |
winner=p |
break |
# did the current player land on a chute? |
ifp_curr[p] inchutes: |
p_curr[p] =chutes[p_curr[p]] |
# did the current player land on a ladder? |
elifp_curr[p] inladders: |
p_curr[p] =ladders[p_curr[p]] |
ifp_curr[p] end: |
winner=p |
break |
printstr(winner) +'t'+str(p_moves[winner]) +'t'+str(total_moves) + |
't'+','.join(str(r) forrinp_rolls[winner]) |
python cl.py 10000 1 | head | column -t |
winning_player winners_move_cnt tot_moves winners_moves |
1 36 36 6,3,6,6,5,2,5,6,5,2,3,2,3,1,2,6,5,6,2,5,1,5,5,4,6,2,2,5,1,1,4,6,5,6,1,5 |
1 12 12 3,3,6,1,4,6,5,2,5,3,3,5 |
1 59 59 5,5,2,2,3,4,6,5,1,1,3,2,1,4,4,1,3,2,4,4,4,2,1,5,3,6,5,1,3,6,6,6,5,6,2,2,5,6,2,1,3,4,5,4,3,3,4,1,1,5,5,3,2,5,6,6,6,3,6 |
1 12 12 1,6,2,2,6,2,6,4,5,3,4,5 |
1 37 37 5,1,4,3,6,5,5,6,4,3,4,3,5,4,6,1,5,1,6,5,4,6,5,3,3,1,4,6,6,3,1,2,5,2,6,1,2 |
1 24 24 2,1,2,5,1,6,1,3,1,5,3,4,2,4,2,2,2,1,1,6,1,6,1,1 |
1 17 17 6,6,1,2,2,4,3,1,5,1,4,4,6,6,3,6,3 |
1 15 15 2,1,1,1,6,3,1,1,2,6,2,2,2,3,6 |
1 35 35 6,6,2,1,3,4,1,4,4,3,4,1,2,3,1,2,6,6,4,5,5,1,5,5,4,5,1,1,3,5,6,6,6,1,4 |
# stats on number of moves: |
python cl.py 100000 1 | awk 'NR>1'| cut -f 2 | stats |
Total lines: 100000 |
Sum of lines: 3579432 |
Ari. Mean: 35.79432 |
Geo. Mean: 29.9810932680843 |
Median: 29 |
Mode: 18 (N=2857) |
Anti-Mode: 178 (N=1) |
Minimum: 7 |
Maximum: 264 |
Variance: 537.847395737582 |
StdDev: 23.191537157713 |