From 85e7860aa13a6d747943d9ef6689f5960c750928 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Thu, 28 Apr 2022 08:36:49 +1000 Subject: [PATCH] Simplify some functions --- 71_Poker/csharp/Games.cs | 243 ++++++++++++++++++--------------------- 1 file changed, 112 insertions(+), 131 deletions(-) diff --git a/71_Poker/csharp/Games.cs b/71_Poker/csharp/Games.cs index d6cb862a..a7d7ffcb 100644 --- a/71_Poker/csharp/Games.cs +++ b/71_Poker/csharp/Games.cs @@ -22,12 +22,12 @@ internal class Game } } - private readonly FloatArray AA = new(new float[51]); // 10 DIM A(50),B(15) + private readonly FloatArray _cards = new(new float[51]); // 10 DIM A(50),B(15) private readonly FloatArray BB = new(new float[16]); private float O = 1; // 90 = 120 - private float C = 200; - private float S = 200; - private float P = 0; + private float _computerBalance = 200; + private float _playerBalance = 200; + private float _pot = 0; private float B; private float M; @@ -35,7 +35,6 @@ internal class Game private float D; private float G; private float I; - private float N; private float U; private float Z; private float X; @@ -54,9 +53,9 @@ internal class Game _random = random; } - private float FNA() => (int)_random.NextFloat(10); // 20 DEF FNAA[X]=(int)(10*_random.NextFloat()) + private int Get0To9() => _random.Next(10); - private float FNB(float x) => x - 100 * (int)(x / 100); // 30 DEF FNB(X)=X-100*(int)(X/100) + private static int Mod100(float x) => (int)x % 100; internal void Play() { @@ -64,43 +63,41 @@ internal class Game _io.Write(Resource.Streams.Instructions); _90: O=1; -_100: C=200; -_110: S=200; +_100: _computerBalance=200; +_110: _playerBalance=200; while(PlayHand()); } internal bool PlayHand() { -_120: P=0; +_120: _pot=0; _140: _io.WriteLine(); -_150: if (C<=5) { Line_3670(); return false; } +_150: if (_computerBalance<=5) { Line_3670(); return false; } _160: _io.WriteLine("The ante is $5. I will deal:"); _170: _io.WriteLine(); -_180: if (S>5) { goto _200; } +_180: if (_playerBalance>5) { goto _200; } _190: if (Line_3830()) { return false; } -_200: P=P+10; -_210: S=S-5; -_220: C=C-5; +_200: _pot=_pot+10; +_210: _playerBalance=_playerBalance-5; +_220: _computerBalance=_computerBalance-5; _230: for (Z=1; Z <= 10; Z++) { _240: Line_1740(); } _260: _io.WriteLine("Your hand:"); -_270: N=1; -_280: Line_1850(); -_290: N=6; +_280: DisplayHand(1); _300: I=2; -_310: Line_2170(); +_310: AnalyzeHand(6); _320: _io.WriteLine(); _330: if (I!=6) { goto _470; } -_340: if (FNA()<=7) { goto _370; } +_340: if (Get0To9()<=7) { goto _370; } _350: X=11100; _360: goto _420; -_370: if (FNA()<=7) { goto _400; } +_370: if (Get0To9()<=7) { goto _400; } _380: X=11110; _390: goto _420; -_400: if (FNA()>=1) { goto _450; } +_400: if (Get0To9()>=1) { goto _450; } _410: X=11111; _420: I=7; _430: Z=23; @@ -108,7 +105,7 @@ _440: goto _580; _450: Z=1; _460: goto _510; _470: if (U>=13) { goto _540; } -_480: if (FNA()>=2) { goto _500; } +_480: if (Get0To9()>=2) { goto _500; } _490: goto _420; _500: Z=0; _510: K=0; @@ -116,9 +113,9 @@ _520: _io.WriteLine("I check."); _530: goto _620; _540: if (U<=16) { goto _570; } _550: Z=2; -_560: if (FNA()>=1) { goto _580; } +_560: if (Get0To9()>=1) { goto _580; } _570: Z=35; -_580: V=Z+FNA(); +_580: V=Z+Get0To9(); _590: if (Line_3480()) { return false; } _600: _io.WriteLine($"I'll open with ${V}"); _610: K=V; @@ -129,7 +126,12 @@ _640: goto _820; bool? Line_650() { -_650: if (I!=3) { return Line_760(); } +_650: if (I!=3) + { + if (I!=4) { return null; } + _io.WriteLine(); + return Line_780(); + } _660: _io.WriteLine(); return Line_670(); } @@ -137,13 +139,13 @@ _660: _io.WriteLine(); bool? Line_670() { _670: _io.WriteLine("I win."); -_680: C=C+P; +_680: _computerBalance=_computerBalance+_pot; return Line_690(); } bool? Line_690() { -_690: _io.WriteLine($"Now I have ${C}and you have ${S}"); +_690: _io.WriteLine($"Now I have ${_computerBalance}and you have ${_playerBalance}"); _700: _io.Write("Do you wish to continue"); _710: HS = _io.ReadString(""); _720: if (HS.Equals("YES", InvariantCultureIgnoreCase)) { return true; } @@ -152,25 +154,13 @@ _740: _io.WriteLine("Answer Yes or No, please."); _750: goto _700; } - bool? Line_760() - { -_760: if (I!=4) { return Line_810(); } -_770: _io.WriteLine(); - return Line_780(); - } - bool? Line_780() { _780: _io.WriteLine("You win."); -_790: S=S+P; +_790: _playerBalance=_playerBalance+_pot; _800: return Line_690(); } - bool? Line_810() - { -_810: return null; - } - _820: _io.WriteLine(); _830: _io.Write("Now we draw -- How many cards do you want"); _840: T = _io.ReadNumber(""); @@ -186,8 +176,7 @@ _920: U = _io.ReadNumber(""); _930: Line_1730(); } _950: _io.WriteLine("Your new hand:"); -_960: N=1; -_970: Line_1850(); +_970: DisplayHand(1); _980: Z=10+T; _990: for (U=6; U <= 10; U++) { @@ -202,10 +191,9 @@ _1060: _io.WriteLine("s"); _1070: _io.WriteLine(); _1080: goto _1100; _1090: _io.WriteLine(); -_1100: N=6; -_1110: V=I; +_1100: V=I; _1120: I=1; -_1130: Line_2170(); +_1130: AnalyzeHand(6); _1140: B=U; _1150: M=D; _1160: if (V!=7) { goto _1190; } @@ -216,12 +204,12 @@ _1200: Z=1; _1210: goto _1330; _1220: if (U>=13) { goto _1270; } _1230: Z=2; -_1240: if (FNA()!=6) { goto _1260; } +_1240: if (Get0To9()!=6) { goto _1260; } _1250: Z=19; _1260: goto _1330; _1270: if (U>=16) { goto _1320; } _1280: Z=19; -_1290: if (FNA()!=8) { goto _1310; } +_1290: if (Get0To9()!=8) { goto _1310; } _1300: Z=11; _1310: goto _1330; _1320: Z=2; @@ -232,7 +220,7 @@ _1360: if (V==7) { goto _1400; } _1370: if (I!=6) { goto _1400; } _1380: _io.WriteLine("I'll check"); _1390: goto _1460; -_1400: V=Z+FNA(); +_1400: V=Z+Get0To9(); _1410: if (Line_3480()) { return false; } _1420: _io.WriteLine($"I'll bet ${V}"); _1430: K=V; @@ -244,29 +232,27 @@ _1470: _io.WriteLine("Now we compare hands:"); _1480: JS=HS; _1490: KS=IS; _1500: _io.WriteLine("My hand:"); -_1510: N=6; -_1520: Line_1850(); -_1530: N=1; -_1540: Line_2170(); +_1520: DisplayHand(6); +_1540: AnalyzeHand(1); _1550: _io.WriteLine(); _1560: _io.Write("You have "); _1570: K=D; -_1580: Line_3690(); +_1580: DisplayHandRank(); _1590: HS=JS; _1600: IS=KS; _1610: K=M; _1620: _io.Write("and I have "); -_1630: Line_3690(); +_1630: DisplayHandRank(); _1640: if (B>U) { return Line_670().Value; } _1650: if (U>B) { return Line_780().Value; } _1660: if (HS=="A Flus") { goto _1700; } -_1662: if (FNB(M)FNB(D)) { return Line_670().Value; } +_1662: if (Mod100(M)Mod100(D)) { return Line_670().Value; } _1670: _io.WriteLine("The hand is drawn."); -_1680: _io.WriteLine($"All ${P}remains in the pot."); +_1680: _io.WriteLine($"All ${_pot}remains in the pot."); _1690: goto _140; -_1700: if (FNB(M)>FNB(D)) { return Line_670().Value; } -_1710: if (FNB(D)>FNB(M)) { return Line_780().Value; } +_1700: if (Mod100(M)>Mod100(D)) { return Line_670().Value; } +_1710: if (Mod100(D)>Mod100(M)) { return Line_780().Value; } _1720: goto _1670; void Line_1730() @@ -277,29 +263,27 @@ _1730: Z=Z+1; void Line_1740() { -_1740: AA[Z]=100*(int)(4*_random.NextFloat())+(int)(100*_random.NextFloat()); -_1750: if ((int)(AA[Z]/100)>3) { goto _1740; } -_1760: if (AA[Z]-100*(int)(AA[Z]/100)>12) { goto _1740; } +_1740: _cards[Z]=100*(int)(4*_random.NextFloat())+(int)(100*_random.NextFloat()); +_1750: if ((int)(_cards[Z]/100)>3) { goto _1740; } +_1760: if (_cards[Z]-100*(int)(_cards[Z]/100)>12) { goto _1740; } _1765: if (Z==1) { goto _1840; } _1770: for (K=1; K <= Z-1; K++) { -_1780: if (AA[Z]==AA[K]) { goto _1740; } +_1780: if (_cards[Z]==_cards[K]) { goto _1740; } } _1800: if (Z<=10) { goto _1840; } -_1810: N=AA[U]; -_1820: AA[U]=AA[Z]; -_1830: AA[Z]=N; + (_cards[U], _cards[Z]) = (_cards[Z], _cards[U]); _1840: return; } - void Line_1850() + void DisplayHand(int firstCard) { -_1850: for (Z = N; Z <= N+4; Z++) +_1850: for (Z = firstCard; Z <= firstCard+4; Z++) { _1860: _io.Write($"{Z}-- "); -_1870: Line_1950(); +_1870: DisplayRank(Z); _1880: _io.Write(" of"); -_1890: Line_2070(); +_1890: DisplaySuit(Z); _1900: if (Z/2!=(int)(Z/2)) { goto _1920; } _1910: _io.WriteLine(); _1920: ; @@ -308,102 +292,100 @@ _1930: _io.WriteLine(); _1940: return; } - void Line_1950() + void DisplayRank(float cardNumber) { -_1950: K=FNB(AA[Z]); - Line_1960(); + Line_1960(Mod100(_cards[cardNumber])); } - void Line_1960() + void Line_1960(int rank) { -_1960: if (K!=9) { goto _1980; } +_1960: if (rank!=9) { goto _1980; } _1970: _io.Write("Jack"); -_1980: if (K!=10) { goto _2000; } +_1980: if (rank!=10) { goto _2000; } _1990: _io.Write("Queen"); -_2000: if (K!=11) { goto _2020; } +_2000: if (rank!=11) { goto _2020; } _2010: _io.Write("King"); -_2020: if (K!=12) { goto _2040; } +_2020: if (rank!=12) { goto _2040; } _2030: _io.Write("Ace"); -_2040: if (K>=9) { goto _2060; } -_2050: _io.Write(K+2); +_2040: if (rank>=9) { goto _2060; } +_2050: _io.Write(rank+2); _2060: return; } - void Line_2070() + void DisplaySuit(float cardNumber) { -_2070: K=(int)(AA[Z]/100); - Line_2080(); + Line_2080((int)(_cards[cardNumber]/100)); } - void Line_2080() + void Line_2080(int suitNumber) { -_2080: if (K!=0) { goto _2100; } +_2080: if (suitNumber!=0) { goto _2100; } _2090: _io.Write(" Clubs"); -_2100: if (K!=1) { goto _2120; } +_2100: if (suitNumber!=1) { goto _2120; } _2110: _io.Write(" Diamonds"); -_2120: if (K!=2) { goto _2140; } +_2120: if (suitNumber!=2) { goto _2140; } _2130: _io.Write(" Hearts"); -_2140: if (K!=3) { goto _2160; } +_2140: if (suitNumber!=3) { goto _2160; } _2150: _io.Write(" Spades"); _2160: return; } - void Line_2170() + void AnalyzeHand(int firstCard) { _2170: U=0; -_2180: for (Z=N; Z <= N+4; Z++) +_2180: for (Z=firstCard; Z <= firstCard+4; Z++) { -_2190: BB[Z]=FNB(AA[Z]); -_2200: if (Z==N+4) { goto _2230; } -_2210: if ((int)(AA[Z]/100)!=(int)(AA[Z+1]/100)) { goto _2230; } +_2190: BB[Z]=Mod100(_cards[Z]); +_2200: if (Z==firstCard+4) { goto _2230; } +_2210: if ((int)(_cards[Z]/100)!=(int)(_cards[Z+1]/100)) { goto _2230; } _2220: U=U+1; _2230: ; } _2240: if (U!=4) { goto _2310; } _2250: X=11111; -_2260: D=AA[N]; +_2260: D=_cards[firstCard]; _2270: HS="A Flus"; _2280: IS="h in"; _2290: U=15; _2300: return; -_2310: for (Z=N; Z <= N+3; Z++) +_2310: for (Z=firstCard; Z <= firstCard+3; Z++) { -_2320: for (K=Z+1; K <= N+4; K++) +_2320: for (K=Z+1; K <= firstCard+4; K++) { _2330: if (BB[Z]<=BB[K]) { goto _2390; } -_2340: X=AA[Z]; -_2350: AA[Z]=AA[K]; +_2340: X=_cards[Z]; +_2350: _cards[Z]=_cards[K]; _2360: BB[Z]=BB[K]; -_2370: AA[K]=X; -_2380: BB[K]=AA[K]-100*(int)(AA[K]/100); +_2370: _cards[K]=X; +_2380: BB[K]=_cards[K]-100*(int)(_cards[K]/100); _2390: ; } } _2410: X=0; -_2420: for (Z=N; Z <= N+3; Z++) +_2420: for (Z=firstCard; Z <= firstCard+3; Z++) { _2430: if (BB[Z]!=BB[Z+1]) { goto _2470; } -_2440: X=X+11*(float)Math.Pow(10, Z-N); -_2450: D=AA[Z]; +_2440: X=X+11*(float)Math.Pow(10, Z-firstCard); +_2450: D=_cards[Z]; _2460: Line_2760(); _2470: ; } _2480: if (X!=0) { goto _2620; } -_2490: if (BB[N]+3!=BB[N+3]) { goto _2520; } +_2490: if (BB[firstCard]+3!=BB[firstCard+3]) { goto _2520; } _2500: X=1111; _2510: U=10; -_2520: if (BB[N+1]+3!=BB[N+4]) { goto _2620; } +_2520: if (BB[firstCard+1]+3!=BB[firstCard+4]) { goto _2620; } _2530: if (U!=10) { goto _2600; } _2540: U=14; _2550: HS="Straig"; _2560: IS="ht"; _2570: X=11111; -_2580: D=AA[N+4]; +_2580: D=_cards[firstCard+4]; _2590: return; _2600: U=10; _2610: X=11110; _2620: if (U>=10) { goto _2690; } -_2630: D=AA[N+4]; +_2630: D=_cards[firstCard+4]; _2640: HS="Schmal"; _2650: IS="tz, "; _2660: U=9; @@ -413,7 +395,7 @@ _2690: if (U!=10) { goto _2720; } _2700: if (I==1) { goto _2740; } _2710: goto _2750; _2720: if (U>12) { goto _2750; } -_2730: if (FNB(D)>6) { goto _2750; } +_2730: if (Mod100(D)>6) { goto _2750; } _2740: I=6; _2750: return; } @@ -423,7 +405,7 @@ _2750: return; _2760: if (U>=11) { goto _2810; } _2770: U=11; _2780: HS="A Pair"; -_2790: IS=" OF "; +_2790: IS=" of "; _2800: return; _2810: if (U!=11) { goto _2910; } _2820: if (BB[Z]!=BB[Z-1]) { goto _2870; } @@ -468,7 +450,7 @@ _3100: if (G!=0) { goto _3120; } _3110: if (T==.5) { return Line_3410(); } _3120: _io.WriteLine("No small change, please."); _3130: goto _3060; -_3140: if (S-G-T>=0) { goto _3170; } +_3140: if (_playerBalance-G-T>=0) { goto _3170; } _3150: if (Line_3830()) { return true; } _3160: goto _3060; _3170: if (T!=0) { goto _3200; } @@ -506,9 +488,9 @@ _3370: K=G; bool Line_3380() { -_3380: S=S-G; -_3390: C=C-K; -_3400: P=P+G+K; +_3380: _playerBalance=_playerBalance-G; +_3390: _computerBalance=_computerBalance-K; +_3400: _pot=_pot+G+K; return Line_3410(); } @@ -525,7 +507,7 @@ _3420: if (G>3*Z) { return Line_3350(); } bool Line_3430() { -_3430: V=G-K+FNA(); +_3430: V=G-K+Get0To9(); _3440: if (Line_3480()) { return true; } _3450: _io.WriteLine($"I'll see you, and raise you{V}"); _3460: K=G+V; @@ -534,23 +516,23 @@ _3470: return Line_3060(); bool Line_3480() { -_3480: if (C-G-V>=0) { goto _3660; } +_3480: if (_computerBalance-G-V>=0) { goto _3660; } _3490: if (G!=0) { goto _3520; } -_3500: V=C; +_3500: V=_computerBalance; _3510: return false; -_3520: if (C-G>=0) { return Line_3360(); } +_3520: if (_computerBalance-G>=0) { return Line_3360(); } _3530: if (O % 2 != 0) { goto _3600; } _3540: _io.Write("Would you like to buy back your watch for $50"); _3550: JS = _io.ReadString(""); _3560: if (JS.StartsWith("N", InvariantCultureIgnoreCase)) { goto _3600; } -_3570: C=C+50; +_3570: _computerBalance=_computerBalance+50; _3580: O=O/2; _3590: return false; _3600: if (O % 3!= 0) { return Line_3670(); } _3610: _io.Write("Would you like to buy back your tie tack for $50"); _3620: JS = _io.ReadString(""); _3630: if (JS.StartsWith("N", InvariantCultureIgnoreCase)) { return Line_3670(); } -_3640: C=C+50; +_3640: _computerBalance=_computerBalance+50; _3650: O=O/3; _3660: return false; } @@ -561,16 +543,15 @@ _3670: _io.WriteLine("I'm busted. Congratulations!"); _3680: return true; // STOP } - void Line_3690() + void DisplayHandRank() { _3690: _io.Write($"{HS}{IS}"); _3700: if (HS!="A FLUS") { goto _3750; } -_3710: K=K/100; -_3720: Line_2080(); +_3710: ; +_3720: Line_2080((int)(K/=100)); _3730: _io.WriteLine(); _3740: return; -_3750: K=FNB(K); -_3760: Line_1960(); +_3750: Line_1960((int)(K=Mod100(K))); _3770: if (HS=="Schmal") { goto _3790; } _3780: if (HS!="Straig") { goto _3810; } _3790: _io.WriteLine(" High"); @@ -587,24 +568,24 @@ _3850: if (O/2 == (int)(O/2)) { goto _3970; } _3860: _io.Write("Would you like to sell your watch"); _3870: JS = _io.ReadString(""); _3880: if (JS.StartsWith("N", InvariantCultureIgnoreCase)) { goto _3970; } -_3890: if (FNA()>=7) { goto _3930; } +_3890: if (Get0To9()>=7) { goto _3930; } _3900: _io.WriteLine("I'll give you $75 for it."); -_3910: S=S+75; +_3910: _playerBalance=_playerBalance+75; _3920: goto _3950; _3930: _io.WriteLine("That's a pretty crummy watch - I'll give you $25."); -_3940: S=S+25; +_3940: _playerBalance=_playerBalance+25; _3950: O=O*2; _3960: return false; _3970: if (O/3 != (int)(O/3)) { goto _4090; } _3980: _io.WriteLine("Will you part with that diamond tie tack"); _3990: JS = _io.ReadString(""); _4000: if (JS.StartsWith("N", InvariantCultureIgnoreCase)) { goto _4080; } -_4010: if (FNA()>=6) { goto _4050; } +_4010: if (Get0To9()>=6) { goto _4050; } _4020: _io.WriteLine("You are now $100 richer."); -_4030: S=S+100; +_4030: _playerBalance=_playerBalance+100; _4040: goto _4070; _4050: _io.WriteLine("It's paste. $25."); -_4060: S=S+25; +_4060: _playerBalance=_playerBalance+25; _4070: O=O*3; _4080: return false; _4090: _io.WriteLine("Your wad is shot. So long, sucker!");