77import { unzip , zip } from "@std/collections" ;
88import { Counter , range } from "../lib/util.ts" ;
99
10+ // Don't use `as const` this object here because I need to treat these
11+ // values as number. Normally, we would define a type with readonly for
12+ // all properties. However, there were many properties and I was lazy and
13+ // cut corners.
14+ //
1015// deno-fmt-ignore
11- enum Square {
12- GO = 0 , A1 = 1 , CC1 = 2 , A2 = 3 , T1 = 4 ,
13- R1 = 5 , B1 = 6 , CH1 = 7 , B2 = 8 , B3 = 9 ,
14- JAIL = 10 , C1 = 11 , U1 = 12 , C2 = 13 , C3 = 14 ,
15- R2 = 15 , D1 = 16 , CC2 = 17 , D2 = 18 , D3 = 19 ,
16- FP = 20 , E1 = 21 , CH2 = 22 , E2 = 23 , E3 = 24 ,
17- R3 = 25 , F1 = 26 , F2 = 27 , U2 = 28 , F3 = 29 ,
18- G2J = 30 , G1 = 31 , G2 = 32 , CC3 = 33 , G3 = 34 ,
19- R4 = 35 , CH3 = 36 , H1 = 37 , T2 = 38 , H2 = 39 ,
20- }
16+ const SQUARE = {
17+ GO : 0 , A1 : 1 , CC1 : 2 , A2 : 3 , T1 : 4 ,
18+ R1 : 5 , B1 : 6 , CH1 : 7 , B2 : 8 , B3 : 9 ,
19+ JAIL : 10 , C1 : 11 , U1 : 12 , C2 : 13 , C3 : 14 ,
20+ R2 : 15 , D1 : 16 , CC2 : 17 , D2 : 18 , D3 : 19 ,
21+ FP : 20 , E1 : 21 , CH2 : 22 , E2 : 23 , E3 : 24 ,
22+ R3 : 25 , F1 : 26 , F2 : 27 , U2 : 28 , F3 : 29 ,
23+ G2J : 30 , G1 : 31 , G2 : 32 , CC3 : 33 , G3 : 34 ,
24+ R4 : 35 , CH3 : 36 , H1 : 37 , T2 : 38 , H2 : 39 ,
25+ } ;
2126
2227const communityChest = ( sq : number ) : number => {
2328 switch ( Math . trunc ( Math . random ( ) * 16 ) ) {
2429 case 0 :
25- return Square . GO ;
30+ return SQUARE . GO ;
2631 case 1 :
27- return Square . JAIL ;
32+ return SQUARE . JAIL ;
2833 default :
2934 return sq ;
3035 }
@@ -33,43 +38,43 @@ const communityChest = (sq: number): number => {
3338const chanceCard = ( sq : number ) : number => {
3439 const nextR = ( sq : number ) : number => {
3540 switch ( sq ) {
36- case Square . CH1 :
37- return Square . R2 ;
38- case Square . CH2 :
39- return Square . R3 ;
40- case Square . CH3 :
41- return Square . R1 ;
41+ case SQUARE . CH1 :
42+ return SQUARE . R2 ;
43+ case SQUARE . CH2 :
44+ return SQUARE . R3 ;
45+ case SQUARE . CH3 :
46+ return SQUARE . R1 ;
4247 default :
4348 throw new RangeError ( `invalid square: ${ sq } ` ) ;
4449 }
4550 } ;
4651
4752 const nextU = ( sq : number ) : number => {
4853 switch ( sq ) {
49- case Square . CH1 :
50- return Square . U1 ;
51- case Square . CH2 :
52- return Square . U2 ;
53- case Square . CH3 :
54- return Square . U1 ;
54+ case SQUARE . CH1 :
55+ return SQUARE . U1 ;
56+ case SQUARE . CH2 :
57+ return SQUARE . U2 ;
58+ case SQUARE . CH3 :
59+ return SQUARE . U1 ;
5560 default :
5661 throw new RangeError ( `invalid square: ${ sq } ` ) ;
5762 }
5863 } ;
5964
6065 switch ( Math . trunc ( Math . random ( ) * 16 ) ) {
6166 case 0 :
62- return Square . GO ;
67+ return SQUARE . GO ;
6368 case 1 :
64- return Square . JAIL ;
69+ return SQUARE . JAIL ;
6570 case 2 :
66- return Square . C1 ;
71+ return SQUARE . C1 ;
6772 case 3 :
68- return Square . E3 ;
73+ return SQUARE . E3 ;
6974 case 4 :
70- return Square . H2 ;
75+ return SQUARE . H2 ;
7176 case 5 :
72- return Square . R1 ;
77+ return SQUARE . R1 ;
7378 case 6 :
7479 case 7 :
7580 return nextR ( sq ) ;
@@ -86,30 +91,30 @@ const chanceCard = (sq: number): number => {
8691
8792const monteCarlo = ( dice : ( ) => number , loopCnt : number ) : string => {
8893 const counter : number [ ] = new Array ( 40 ) . fill ( 0 ) ;
89- let sq = Square . GO ;
94+ let sq = SQUARE . GO ;
9095 let double = 0 ;
9196
9297 for ( const _ of range ( 0 , loopCnt ) ) {
9398 const d1 = dice ( ) ;
9499 const d2 = dice ( ) ;
95100 double = ( d1 !== d2 ) ? 0 : double + 1 ;
96101 if ( double >= 3 ) {
97- sq = Square . JAIL ;
102+ sq = SQUARE . JAIL ;
98103 double = 0 ;
99104 } else {
100105 sq = ( sq + d1 + d2 ) % 40 ;
101106 switch ( sq ) {
102- case Square . G2J :
103- sq = Square . JAIL ;
107+ case SQUARE . G2J :
108+ sq = SQUARE . JAIL ;
104109 break ;
105- case Square . CC1 :
106- case Square . CC2 :
107- case Square . CC3 :
110+ case SQUARE . CC1 :
111+ case SQUARE . CC2 :
112+ case SQUARE . CC3 :
108113 sq = communityChest ( sq ) ;
109114 break ;
110- case Square . CH1 :
111- case Square . CH2 :
112- case Square . CH3 :
115+ case SQUARE . CH1 :
116+ case SQUARE . CH2 :
117+ case SQUARE . CH3 :
113118 sq = chanceCard ( sq ) ;
114119 break ;
115120 }
0 commit comments