endgame=101;(*Thenumberofstatesinthegame*)numoptions=6;(*Assumingequallyprobableoutcomes-1/6forasix-sideddie.*)lastsquare=endgame-1;(*Thelastsquareinthegame-notusedinthiscode,butwasusedsoIleftit*)moves=Table[Table[Min[j+i,endgame],{i,1,numoptions}],{j,1,endgame}];(*Setupthegamewithnochutes,noladders,andnoexceptions.Thiswouldassumethatyoudonotneedanexactrolltowin.*)gamemat=moves;game1exceptions=(*Enteryourexceptionshere-theseareSTATES,notsquarenumbers!*){{100,2,100},{100,3,100},{100,4,100},{100,5,100},{100,6,100},{99,3,99},{99,4,99},{99,5,99},{99,6,99},{96,6,96},{97,5,97},{97,6,97},{98,4,98},{98,5,98},{98,6,98}};
In[]:=
(*Enteryourchutesandladdershere.Makeanarrowwithadashandalessthansign.Rememberthatsquarenonthegameboardisstatenumbern+1inmanygames,sinceplayersstartofftheboard.YoucanalsousetheslidestoarrowsfunctionwhichI'vedemonstratedbelow.Thisturnsslidesontheboardintostates.*)(*chutesandladders={210,1535,7212,58,13->73,10->50,51->11};Youcouldtypethisdirectly,orusethecommand.*)chutesandladders=slidestoarrows[{{1,9},{14,34},{71,11},{4,7},{12,72},{9,49},{50,10}}];updownlist=Table[chutesandladders[[i,2]]-chutesandladders[[i,1]],{i,1,Length[chutesandladders]}];{Total[Select[updownlist,Positive]],Total[Select[updownlist,Negative]]}
Out[]=
{131,-100}
This is how the slidestoarrows command works. The {1,9} says there is a ladder from square 1 to square 9.
In[]:=
slidestoarrows[{{1,9},{14,34},{71,11},{4,7},{12,72},{9,49},{50,10}}]
Out[]=
{210,1535,7212,58,1373,1050,5111}
The next cell will tell you if your game is valid, at least in terms of the length of the chutes and ladders.
In[]:=
goodcount=Count[updownlist,-60]+Count[updownlist,60]+Count[updownlist,40]+Count[updownlist,-40];gencount=Count[updownlist,x_/;x>20]+Count[updownlist,x_/;x<3]-goodcount;enoughchutes=And[And[Total[Select[updownlist,Positive]]==200,Total[Select[updownlist,Negative]]==225],And[goodcount==4,gencount==0]];If[And[enoughchutes,And[goodcount==4,gencount==0]],Print["Looks good!"],Print["Not Valid!"]];
Not Valid!
In[]:=
Do[gamemat[[game1exceptions[[k,1]],game1exceptions[[k,2]]]]=game1exceptions[[k,3]],{k,1,Length[game1exceptions]}];(*Thisputstheexceptionsintothegamematrix*)gamemat=gamemat/.chutesandladders;(*Thisputsthechutesandladddersintothegamematrix.*)(*Webeginwithamatrixofall0sforamovesmatrix.*)movesmatrix=Table[0,{i,1,endgame},{j,1,endgame}];(*Thisonecommmandproducesthemovesmatrixfromthegamematrix.*)Do[Do[movesmatrix[[i,gamemat[[i,j]]]]=movesmatrix[[i,gamemat[[i,j]]]]+1/numoptions,{i,1,endgame}],{j,1,numoptions}];
In[]:=
gamemat//MatrixForm
Out[]//MatrixForm=
10 | 3 | 4 | 8 | 6 | 7 |
3 | 4 | 8 | 6 | 7 | 8 |
4 | 8 | 6 | 7 | 8 | 9 |
8 | 6 | 7 | 8 | 9 | 50 |
6 | 7 | 8 | 9 | 50 | 11 |
7 | 8 | 9 | 50 | 11 | 12 |
8 | 9 | 50 | 11 | 12 | 73 |
9 | 50 | 11 | 12 | 73 | 14 |
50 | 11 | 12 | 73 | 14 | 35 |
11 | 12 | 73 | 14 | 35 | 16 |
12 | 73 | 14 | 35 | 16 | 17 |
73 | 14 | 35 | 16 | 17 | 18 |
14 | 35 | 16 | 17 | 18 | 19 |
35 | 16 | 17 | 18 | 19 | 20 |
16 | 17 | 18 | 19 | 20 | 21 |
17 | 18 | 19 | 20 | 21 | 22 |
18 | 19 | 20 | 21 | 22 | 23 |
19 | 20 | 21 | 22 | 23 | 24 |
20 | 21 | 22 | 23 | 24 | 25 |
21 | 22 | 23 | 24 | 25 | 26 |
22 | 23 | 24 | 25 | 26 | 27 |
23 | 24 | 25 | 26 | 27 | 28 |
24 | 25 | 26 | 27 | 28 | 29 |
25 | 26 | 27 | 28 | 29 | 30 |
26 | 27 | 28 | 29 | 30 | 31 |
27 | 28 | 29 | 30 | 31 | 32 |
28 | 29 | 30 | 31 | 32 | 33 |
29 | 30 | 31 | 32 | 33 | 34 |
30 | 31 | 32 | 33 | 34 | 35 |
31 | 32 | 33 | 34 | 35 | 36 |
32 | 33 | 34 | 35 | 36 | 37 |
33 | 34 | 35 | 36 | 37 | 38 |
34 | 35 | 36 | 37 | 38 | 39 |
35 | 36 | 37 | 38 | 39 | 40 |
36 | 37 | 38 | 39 | 40 | 41 |
37 | 38 | 39 | 40 | 41 | 42 |
38 | 39 | 40 | 41 | 42 | 43 |
39 | 40 | 41 | 42 | 43 | 44 |
40 | 41 | 42 | 43 | 44 | 45 |
41 | 42 | 43 | 44 | 45 | 46 |
42 | 43 | 44 | 45 | 46 | 47 |
43 | 44 | 45 | 46 | 47 | 48 |
44 | 45 | 46 | 47 | 48 | 49 |
45 | 46 | 47 | 48 | 49 | 50 |
46 | 47 | 48 | 49 | 50 | 11 |
47 | 48 | 49 | 50 | 11 | 52 |
48 | 49 | 50 | 11 | 52 | 53 |
49 | 50 | 11 | 52 | 53 | 54 |
50 | 11 | 52 | 53 | 54 | 55 |
11 | 52 | 53 | 54 | 55 | 56 |
52 | 53 | 54 | 55 | 56 | 57 |
53 | 54 | 55 | 56 | 57 | 58 |
54 | 55 | 56 | 57 | 58 | 59 |
55 | 56 | 57 | 58 | 59 | 60 |
56 | 57 | 58 | 59 | 60 | 61 |
57 | 58 | 59 | 60 | 61 | 62 |
58 | 59 | 60 | 61 | 62 | 63 |
59 | 60 | 61 | 62 | 63 | 64 |
60 | 61 | 62 | 63 | 64 | 65 |
61 | 62 | 63 | 64 | 65 | 66 |
62 | 63 | 64 | 65 | 66 | 67 |
63 | 64 | 65 | 66 | 67 | 68 |
64 | 65 | 66 | 67 | 68 | 69 |
65 | 66 | 67 | 68 | 69 | 70 |
66 | 67 | 68 | 69 | 70 | 71 |
67 | 68 | 69 | 70 | 71 | 12 |
68 | 69 | 70 | 71 | 12 | 73 |
69 | 70 | 71 | 12 | 73 | 74 |
70 | 71 | 12 | 73 | 74 | 75 |
71 | 12 | 73 | 74 | 75 | 76 |
12 | 73 | 74 | 75 | 76 | 77 |
73 | 74 | 75 | 76 | 77 | 78 |
74 | 75 | 76 | 77 | 78 | 79 |
75 | 76 | 77 | 78 | 79 | 80 |
76 | 77 | 78 | 79 | 80 | 81 |
77 | 78 | 79 | 80 | 81 | 82 |
78 | 79 | 80 | 81 | 82 | 83 |
79 | 80 | 81 | 82 | 83 | 84 |
80 | 81 | 82 | 83 | 84 | 85 |
81 | 82 | 83 | 84 | 85 | 86 |
82 | 83 | 84 | 85 | 86 | 87 |
83 | 84 | 85 | 86 | 87 | 88 |
84 | 85 | 86 | 87 | 88 | 89 |
85 | 86 | 87 | 88 | 89 | 90 |
86 | 87 | 88 | 89 | 90 | 91 |
87 | 88 | 89 | 90 | 91 | 92 |
88 | 89 | 90 | 91 | 92 | 93 |
89 | 90 | 91 | 92 | 93 | 94 |
90 | 91 | 92 | 93 | 94 | 95 |
91 | 92 | 93 | 94 | 95 | 96 |
92 | 93 | 94 | 95 | 96 | 97 |
93 | 94 | 95 | 96 | 97 | 98 |
94 | 95 | 96 | 97 | 98 | 99 |
95 | 96 | 97 | 98 | 99 | 100 |
96 | 97 | 98 | 99 | 100 | 101 |
97 | 98 | 99 | 100 | 101 | 96 |
98 | 99 | 100 | 101 | 97 | 97 |
99 | 100 | 101 | 98 | 98 | 98 |
100 | 101 | 99 | 99 | 99 | 99 |
101 | 100 | 100 | 100 | 100 | 100 |
101 | 101 | 101 | 101 | 101 | 101 |
Let's find the minimum length of a game and the probability of having a game of that length.
Finds the average length of a game by simulation.
Keeps track of game length and turns beyond a given value, usually the minimum.
Keeps track of game length and turns beyond a given value, usually the minimum.
These cells produce the distribution function and the cumulative distribution function from the simulated data, assuming that the previous cell was run first.
And the theoretical value is...
The modified game is just the number of moves taken beyond the minimum. Nice for graphing.
We can also see the actual cumulative distribution function (from the matrix) and compare to the simulation.
The actual distribution obtained from the cumulative distribution function.
The functions below are used in this file. Not all of them may be useful to you, but you are welcome to read about them and see what they do.
The first assumes that the modified distribution table has been generated, and computes values for the cumulative distribution function.
The functions below are used in this file. Not all of them may be useful to you, but you are welcome to read about them and see what they do.
The first assumes that the modified distribution table has been generated, and computes values for the cumulative distribution function.
The next two functions combine to switch rows i and j in a matrix, usually a transition matrix. The first generates the appropriate matrix, and the second applies it.
This function switches rows i and j in a matrix, but does it by moving row i into row j by sliding all intermediate rows up one position. It assumes that i<j.
This command makes the matrix Q by eliminating the last k rows and k columns from the matrix given as input, where k is the number of absorbing states.
Given a matrix assumed to be Q, this function produces N. The next produces N in decimal form - often much more useful.
This command finds the minimum length of a game. The flag indicates whether the probability of having a game of minimum length is printed.
This function produces values for the cumulative distribution function for the value "upper". Do not set the flag to True to compute exact values unless you know that you want them!
This function finds the probability of a game of a specific length.
The next two compute the expected length of a game, either in decimals or exact. Again, don't use the exact unless you know you want it!
This command computes the expected length vector and the variance vector for a game. You input the moves matrix in canonical form and the number of absorbing states.