-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideopoker.go
1138 lines (923 loc) · 22.9 KB
/
videopoker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// videopoker - command line video poker game with color Unicode suits.
//
// version 1.0
package main
/* Some of the code in this file may look very C-like because it
was translated to Go from the C language version of videopoker. */
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
const VERSION = "videopoker 1.0"
/* values for -uN unicode flag */
const (
UNICODE_TTY = iota
UNICODE_OFF
UNICODE_SUITS
UNICODE_CARDS
)
var unicode int = UNICODE_SUITS
// unicode is nonzero if unicode output is enabled.
var hands int = 0 // number of hands played
/* the card type, for holding infomation about the deck of cards */
type card struct
{
index int /* cards value, minus 1 */
sym string /* textual appearance */
uc string /* Unicode value for the card */
suit int /* card's suit (see just below) */
gone int /* true if it's been dealt */
}
/* The number of cards in the hand */
const CARDS = 5
/* The number of cards in the deck */
const CARDSINDECK = 52
const (
CLUBS = iota
DIAMONDS
HEARTS
SPADES
)
/* one-character suit designations */
const NUMSUITS = 4
var suitname [NUMSUITS]string = [NUMSUITS]string {
"C",
"D",
"H",
"S",
}
const (
TWO = iota+1
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
TEN /* needed for recognizing royal flush, or tens or better (TEN), or jacks or better (JACK) */
JACK /* needed for recognizing royal flush, or tens or better (TEN), or jacks or better (JACK) */
QUEEN
KING
ACE /* needed for recognizing Ace-low straight (Ace, 2, 3, 4, 5) */
)
/* The standard deck of 52 cards */
var deck [CARDSINDECK]card = [CARDSINDECK]card {
/* index, card, Unicode, suit, gone */
{ TWO, " 2", "\U0001F0D2", CLUBS, 0 },
{ THREE, " 3", "\U0001F0D3", CLUBS, 0 },
{ FOUR, " 4", "\U0001F0D4", CLUBS, 0 },
{ FIVE, " 5", "\U0001F0D5", CLUBS, 0 },
{ SIX, " 6", "\U0001F0D6", CLUBS, 0 },
{ SEVEN, " 7", "\U0001F0D7", CLUBS, 0 },
{ EIGHT, " 8", "\U0001F0D8", CLUBS, 0 },
{ NINE, " 9", "\U0001F0D9", CLUBS, 0 },
{ TEN, "10", "\U0001F0Da", CLUBS, 0 },
{ JACK, " J", "\U0001F0Db", CLUBS, 0 },
{ QUEEN, " Q", "\U0001F0Dd", CLUBS, 0 },
{ KING, " K", "\U0001F0De", CLUBS, 0 },
{ ACE, " A", "\U0001F0D1", CLUBS, 0 },
{ TWO, " 2", "\U0001F0C2", DIAMONDS, 0 },
{ THREE, " 3", "\U0001F0C3", DIAMONDS, 0 },
{ FOUR, " 4", "\U0001F0C4", DIAMONDS, 0 },
{ FIVE, " 5", "\U0001F0C5", DIAMONDS, 0 },
{ SIX, " 6", "\U0001F0C6", DIAMONDS, 0 },
{ SEVEN, " 7", "\U0001F0C7", DIAMONDS, 0 },
{ EIGHT, " 8", "\U0001F0C8", DIAMONDS, 0 },
{ NINE, " 9", "\U0001F0C9", DIAMONDS, 0 },
{ TEN, "10", "\U0001F0Ca", DIAMONDS, 0 },
{ JACK, " J", "\U0001F0Cb", DIAMONDS, 0 },
{ QUEEN, " Q", "\U0001F0Cd", DIAMONDS, 0 },
{ KING, " K", "\U0001F0Ce", DIAMONDS, 0 },
{ ACE, " A", "\U0001F0C1", DIAMONDS, 0 },
{ TWO, " 2", "\U0001F0B2", HEARTS, 0 },
{ THREE, " 3", "\U0001F0B3", HEARTS, 0 },
{ FOUR, " 4", "\U0001F0B4", HEARTS, 0 },
{ FIVE, " 5", "\U0001F0B5", HEARTS, 0 },
{ SIX, " 6", "\U0001F0B6", HEARTS, 0 },
{ SEVEN, " 7", "\U0001F0B7", HEARTS, 0 },
{ EIGHT, " 8", "\U0001F0B8", HEARTS, 0 },
{ NINE, " 9", "\U0001F0B9", HEARTS, 0 },
{ TEN, "10", "\U0001F0Ba", HEARTS, 0 },
{ JACK, " J", "\U0001F0Bb", HEARTS, 0 },
{ QUEEN, " Q", "\U0001F0Bd", HEARTS, 0 },
{ KING, " K", "\U0001F0Be", HEARTS, 0 },
{ ACE, " A", "\U0001F0B1", HEARTS, 0 },
{ TWO, " 2", "\U0001F0A2", SPADES, 0 },
{ THREE, " 3", "\U0001F0A3", SPADES, 0 },
{ FOUR, " 4", "\U0001F0A4", SPADES, 0 },
{ FIVE, " 5", "\U0001F0A5", SPADES, 0 },
{ SIX, " 6", "\U0001F0A6", SPADES, 0 },
{ SEVEN, " 7", "\U0001F0A7", SPADES, 0 },
{ EIGHT, " 8", "\U0001F0A8", SPADES, 0 },
{ NINE, " 9", "\U0001F0A9", SPADES, 0 },
{ TEN, "10", "\U0001F0Aa", SPADES, 0 },
{ JACK, " J", "\U0001F0Ab", SPADES, 0 },
{ QUEEN, " Q", "\U0001F0Ad", SPADES, 0 },
{ KING, " K", "\U0001F0Ae", SPADES, 0 },
{ ACE, " A", "\U0001F0A1", SPADES, 0 },
}
/* The hand. It holds five cards. */
var hand [5]card
/* sorted hand, for internal use when recognizing winners */
var shand [5]card
/* keys used to select kept cards */
const NUMKEYS = 5
var keys [NUMKEYS]byte = [NUMKEYS]byte {
' ', 'j', 'k', 'l', ';',
}
/* initial number of chips held */
const INITCHIPS = 1000
var score int = INITCHIPS
/* minimum and maximum swing of score during this game */
var score_low int = INITCHIPS
var score_high int = INITCHIPS
/* The games starts with a bet of 10, the minimum allowed */
const INITMINBET = 10
var minbet int = INITMINBET
var bet int = INITMINBET
/* number of chips or groups of 10 chips bet */
var betmultiplier int = 1
/* Options */
/* -b (Bold): print in boldface */
var boldface bool = false
/* -mh (Mark Held): Mark cards that are held */
var markheld bool = false
/* -q (Quiet): Don't print banner or final report */
var quiet bool = false
/* Sanity check: check that there are no duplicate cards in hand */
func check_for_dupes() bool {
//
// For debugging: To enable checking, un-comment the following:
/*
for i := 0; i < CARDS; i++ {
//
for j := i+1; j < CARDS; j++ {
//
if hand[i].index == hand[j].index &&
hand[i].suit == hand[j].suit { return true }
}
}
*/
return false
}
/*
Some ANSI Terminal escape codes:
ESC[38;5; then one of (0m = black, 1m = red, 2m = green, 3m = yellow,
4m = blue, 5m = magenta, 6m = cyan, 7m = white)
ESC[1m = bold, ESC[0m = reset all attributes
*/
/* The below are part of the escape codes listed above.
Do not change the values. Do not use iota. */
const BLACK = 0
const RED = 1
const GREEN = 2
const YELLOW = 3
const BLUE = 4
const MAGENTA = 5
const CYAN = 6
const WHITE = 7
func color(color int) {
if unicode == UNICODE_TTY { return }
fmt.Printf("\033[38;5;%dm",color)
}
func ANSIbold() {
if unicode == UNICODE_TTY { return }
fmt.Printf("\033[1m")
}
func ANSIreset() {
if unicode == UNICODE_TTY { return }
fmt.Printf("\033[0m")
if boldface { ANSIbold() }
}
/*
Display the hand
This is where the Unicode output setting (-u<N> option) takes effect,
so there are three different ways it can display the cards.
*/
func showhand() {
//
var i int
/* Unicode characters for the suits */
const spade string = "\u2660"
const heart string = "\u2665"
const diamond string = "\u2666"
const club string = "\u2663"
/* Method 1: Unicode Card Faces (-u3 option),
which requires only one line */
if unicode == UNICODE_CARDS { /* print the Unicode card faces */
//
for i = 0; i < CARDS; i++ {
//
switch hand[i].suit {
case DIAMONDS:
fallthrough
case HEARTS:
color(RED)
// fmt.Printf("\033[38;5;1m%s\033[0m", hand[i].uc)
fmt.Printf("%s ", hand[i].uc)
ANSIreset()
case CLUBS:
fallthrough
case SPADES:
fmt.Printf("%s ", hand[i].uc)
}
}
/* print a space to separate output from user input */
fmt.Printf(" ")
return
}
/* Method 2: Two Line Output for -u0/-u1/-u2 options, requires 2 lines */
if boldface { ANSIbold() }
/* First Line of output: show card values */
for i = 0; i < CARDS; i++ {
//
switch hand[i].suit {
case DIAMONDS:
fallthrough
case HEARTS:
/* print in red */
color(RED)
// fmt.Printf("\033[38;5;1m%s\033[0m ", hand[i].sym)
fmt.Printf("%s", hand[i].sym)
ANSIreset()
fmt.Printf(" ")
case CLUBS:
fallthrough
case SPADES:
/* print in default text color */
fmt.Printf("%s ", hand[i].sym)
}
}
fmt.Printf("\n")
for i = 0; i < CARDS; i++ {
//
if unicode == UNICODE_SUITS {
/* Unicode method */
switch hand[i].suit {
case DIAMONDS:
/* print in red */
fmt.Printf(" ")
color(RED)
// fmt.Printf("\033[38;5;1m %s\033[0m ", diamond)
fmt.Printf("%s", diamond)
ANSIreset()
fmt.Printf(" ")
case HEARTS:
fmt.Printf(" ")
color(RED)
// fmt.Printf("\033[38;5;1m %s\033[0m ", heart)
fmt.Printf("%s", heart)
ANSIreset()
fmt.Printf(" ")
case CLUBS:
/* print in default text color */
fmt.Printf(" %s ", club)
case SPADES:
fmt.Printf(" %s ", spade)
}
} else {
/* ASCII method */
switch hand[i].suit {
case DIAMONDS:
fallthrough
case HEARTS:
/* print H or D in red */
fmt.Printf(" ")
color(RED)
// fmt.Printf(" \033[38;5;1m%s\033[0m ", suitname[hand[i].suit])
fmt.Printf("%s", suitname[hand[i].suit])
ANSIreset()
fmt.Printf(" ")
case CLUBS:
fallthrough
case SPADES:
/* print S or C in default text color */
fmt.Printf(" %s ", suitname[hand[i].suit])
}
}
}
/* print a space to separate output from user input */
fmt.Printf(" ")
if check_for_dupes() {
//
fmt.Printf("\n!!! DUPLICATE CARD !!!\n\n")
exit(1)
}
}
/* The various video poker games that are supported */
const (
AllAmerican = iota
TensOrBetter
BonusPoker
DoubleBonus
DoubleBonusBonus
JacksOrBetter // default
JacksOrBetter95
JacksOrBetter86
JacksOrBetter85
JacksOrBetter75
JacksOrBetter65
NUMGAMES
)
/*
The game in play. Default is Jacks or Better,
which is coded into initialization of static data
*/
var game int = JacksOrBetter
var gamenames [NUMGAMES]string = [NUMGAMES]string {
"All American",
"Tens or Better",
"Bonus Poker",
"Double Bonus",
"Double Bonus Bonus",
"Jacks or Better",
"9/5 Jacks or Better",
"8/6 Jacks or Better",
"8/5 Jacks or Better",
"7/5 Jacks or Better",
"6/5 Jacks or Better",
}
// TODO: the above consts, gamenames[], and option strings can
// be put in an array of structures:
// type gameinfo struct {} gameinfo;
// var game []gameinfo = { ... }; (etc.)
// Then change badgame(), option handling in main(), and setgame()
/* Error message for -g option. Also a way to display the list of games */
func badgame() {
//
fmt.Printf("Video Poker: -g option is missing valid game name.\n")
fmt.Printf("Available games are:\n")
fmt.Printf("aa - All American\n")
fmt.Printf("10s - Tens or Better\n")
/*
fmt.Printf("bon - Bonus Poker\n")
fmt.Printf("db - Double Bonus\n")
fmt.Printf("dbb - Double Bonus Bonus\n")
*/
fmt.Printf("jb95 - 9/5 Jacks or Better\n")
fmt.Printf("jb86 - 8/6 Jacks or Better\n")
fmt.Printf("jb85 - 8/5 Jacks or Better\n")
fmt.Printf("jb75 - 7/5 Jacks or Better\n")
fmt.Printf("jb65 - 6/5 Jacks or Better\n")
os.Exit(0)
}
/* replacements for C library getchar() and ungetc() functions */
var stdin *bufio.Reader
var char int
func getchar() int {
//
var inputbyte byte
inputbyte, _ = stdin.ReadByte()
char = int(inputbyte)
return char
}
func ungetc() {
//
stdin.UnreadByte()
}
/* replacement for C library random() function */
var randomgen *rand.Rand
func srandom() {
//
randomgen = rand.New(rand.NewSource(time.Now().UnixNano()))
}
func random() int {
//
return randomgen.Int()
}
/* replacement for C library exit() function */
func exit(error int) {
//
os.Exit(error)
}
/* Initialize, Handle arguments, enter loop */
func main() {
//
var ai int
var i, cnt int
var arg string
// open stdin
stdin = bufio.NewReader(os.Stdin)
/* initialize random number generator */
srandom()
/* process arguments */
ai = 0
for cnt = len(os.Args); cnt > 1; {
//
/* -b (Bold) */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'b' &&
len(os.Args[1+ai]) == 2 {
//
if cnt < 2 { badgame() }
boldface = true
/* advance to next argument */
ai += 1
cnt -= 1
continue
}
/* -b1 (Bet 1 Chip) */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'b' &&
os.Args[1+ai][2] == '1' &&
len(os.Args[1+ai]) == 3 {
if cnt < 2 { badgame() }
/* set minimum bet */
minbet = 1; bet = 1
/* advance to next argument */
ai += 1
cnt -= 1
continue
}
/* -g <name> Choose Game */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'g' &&
len(os.Args[1+ai]) == 2 {
//
if cnt < 3 { badgame() }
arg = os.Args[2+ai]
if arg == "jb95" {
game = JacksOrBetter95
} else if arg == "jb86" {
game = JacksOrBetter86
} else if arg == "jb85" {
game = JacksOrBetter85
} else if arg == "jb75" {
game = JacksOrBetter75
} else if arg == "jb65" {
game = JacksOrBetter65
} else if arg == "aa" {
game = AllAmerican
} else if arg == "10s" {
game = TensOrBetter
} else if arg == "tens" {
game = TensOrBetter
/*
} else if arg == "bon" {
game = BonusPoker
} else if arg == "db" {
game = DoubleBonus
} else if arg == "dbb" {
game = DoubleBonusBonus
*/
} else { badgame() }
setgame(game)
/* advance to next argument */
ai += 2
cnt -= 2
continue
}
/* -is (Initial Score) */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'i' &&
os.Args[1+ai][2] == 's' &&
len(os.Args[1+ai]) == 3 {
if cnt < 3 { badgame() }
score, _ = strconv.Atoi(os.Args[2+ai])
if score <= 0 || score > 100000 {
fmt.Printf("Video Poker: bad number given with the -is option.\n")
os.Exit(1)
}
if score%10 != 0 { minbet = 1; bet = 1 }
/* advance to next argument */
ai += 2
cnt -= 2
continue
}
/* -k <5-char-string> Redefine input keys (default is " jkl;" */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'k' &&
len(os.Args[1+ai]) == 2 {
//
if cnt < 3 { badgame() }
arg = os.Args[2+ai]
if(len(arg) != NUMKEYS) {
// complain and exit
fmt.Printf("Video Poker: the string given with the -k option is the wrong length.\n")
os.Exit(1)
}
/* copy the string into keys[] */
for i = 0; i < NUMKEYS; i++ {
if arg[i] == 'q' || arg[i] == 'e' {
fmt.Printf("Video Poker: for the -k option, the string may not contain q or e.\n")
os.Exit(1)
}
keys[i] = byte(arg[i])
}
/* advance to next argument */
ai += 2
cnt -= 2
continue
}
/* -mh (Mark Held) */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'm' &&
os.Args[1+ai][2] == 'h' &&
len(os.Args[1+ai]) == 3 {
//
if cnt < 2 { badgame() }
/* turn on Mark Held flag */
markheld = true
/* advance to next argument */
ai += 1
cnt -= 1
continue
}
/* -q (Quiet) */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'q' &&
len(os.Args[1+ai]) == 2 {
//
if cnt < 2 { badgame() }
/* turn on Quiet flag */
quiet = true
/* advance to next argument */
ai += 1
cnt -= 1
continue
}
/* -u<n> Unicode Output */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'u' &&
len(os.Args[1+ai]) > 2 {
switch os.Args[1+ai][2] {
case '0': unicode = UNICODE_TTY
case '1': unicode = UNICODE_OFF
case '2': unicode = UNICODE_SUITS
case '3': unicode = UNICODE_CARDS
default:
fmt.Printf("Video Poker: digit %d given with -u option is out of range.\n",os.Args[1+ai][2]-'0')
os.Exit(1)
}
/* advance to next argument */
ai += 1
cnt--
continue
}
/* -v (Version) */
if os.Args[1+ai][0] == '-' &&
os.Args[1+ai][1] == 'v' &&
len(os.Args[1+ai]) == 2 {
//
if cnt < 2 { badgame() }
fmt.Printf("%s\n",VERSION)
os.Exit(0)
}
/* unrecognized option */
fmt.Printf("Video Poker: the %s option was not recognized\n",os.Args[1+ai])
os.Exit(1)
}
/* Before starting play, print the name of the game in green */
if ! quiet {
//
fmt.Printf("\n ")
color(GREEN)
ANSIbold()
fmt.Printf("%s",gamenames[game])
ANSIreset()
fmt.Printf("\n\n")
}
for { play() }
}
/* Functions that recognize winning hands */
/*
Flush:
returns true if the sorted hand is a flush
*/
func flush() bool {
//
if shand[0].suit == shand[1].suit &&
shand[1].suit == shand[2].suit &&
shand[2].suit == shand[3].suit &&
shand[3].suit == shand[4].suit { return true }
return false
}
/*
Straight:
returns true if the sorted hand is a straight
*/
func straight() bool {
//
if shand[1].index == shand[0].index + 1 &&
shand[2].index == shand[1].index + 1 &&
shand[3].index == shand[2].index + 1 &&
shand[4].index == shand[3].index + 1 { return true }
if shand[4].index == ACE &&
shand[0].index == TWO &&
shand[1].index == THREE &&
shand[2].index == FOUR &&
shand[3].index == FIVE { return true }
return false
}
/*
Four of a kind:
the middle 3 all match, and the first or last matches those
*/
func four() bool {
//
if (shand[1].index == shand[2].index &&
shand[2].index == shand[3].index ) &&
( shand[0].index == shand[2].index ||
shand[4].index == shand[2].index) { return true }
return false
}
/*
Full house:
3 of a kind and a pair
*/
func full() bool {
//
if shand[0].index == shand[1].index &&
(shand[2].index == shand[3].index &&
shand[3].index == shand[4].index) { return true }
if shand[3].index == shand[4].index &&
(shand[0].index == shand[1].index &&
shand[1].index == shand[2].index) { return true }
return false
}
/*
Three of a kind:
it can appear 3 ways
*/
func three() bool {
//
if shand[0].index == shand[1].index &&
shand[1].index == shand[2].index { return true }
if shand[1].index == shand[2].index &&
shand[2].index == shand[3].index { return true }
if shand[2].index == shand[3].index &&
shand[3].index == shand[4].index { return true }
return false
}
/*
Two pair:
it can appear in 3 ways
*/
func twopair() bool {
//
if ((shand[0].index == shand[1].index) && (shand[2].index == shand[3].index)) ||
((shand[0].index == shand[1].index) && (shand[3].index == shand[4].index)) ||
((shand[1].index == shand[2].index) && (shand[3].index == shand[4].index)) { return true }
return false
}
/*
Two of a kind (pair), jacks or better
or if the game is Tens or Better, 10s or better.
*/
func two() bool {
//
var min int = JACK
if game == TensOrBetter { min = TEN }
if shand[0].index == shand[1].index && shand[1].index >= min { return true }
if shand[1].index == shand[2].index && shand[2].index >= min { return true }
if shand[2].index == shand[3].index && shand[3].index >= min { return true }
if shand[3].index == shand[4].index && shand[4].index >= min { return true }
return false
}
/*
This bunch of consts is used to index into
paytable[] and handname[], so make sure the two match.
*/
const (
ROYAL = iota
STRFL
FOURK
FULL
FLUSH
STR
THREEK
TWOPAIR
PAIR
NOTHING
/* the number of the above: */
NUMHANDTYPES
)
var paytable [NUMHANDTYPES]int = [NUMHANDTYPES]int {
800, /* royal flush: 800 */
50, /* straight flush: 50 */
25, /* 4 of a kind: 25 */
9, /* full house: 9 */
6, /* flush: 6 */
4, /* straight: 4 */
3, /* 3 of a kind: 3 */
2, /* two pair: 2 */
1, /* jacks or better: 1 */
0, /* nothing */
}
var handname [NUMHANDTYPES]string = [NUMHANDTYPES]string {
"Royal Flush ",
"Straight Flush ",
"Four of a Kind ",
"Full House ",
"Flush ",
"Straight ",
"Three of a Kind",
"Two Pair ",
"Pair ",
"Nothing ",
}
const INVALID = 100 /* higher than any valid card index */
/* returns type of hand */
func recognize() int {
//
var i, j, f int
var min int = INVALID
var tmp [CARDS]card
var st, fl bool /* both are auto-initialized to 0 */
/* Sort hand into sorted hand (shand) */
/* make copy of hand */
for i = 0; i < CARDS; i++ { tmp[i] = hand[i] }
for i = 0; i < CARDS; i++ {
/* put lowest card in hand into next place in shand */
for j = 0; j < CARDS; j++ {
if tmp[j].index <= min {
min = tmp[j].index
f = j
}
}
shand[i] = tmp[f]
tmp[f].index = INVALID /* larger than any card */
min = INVALID
}
/* royal and straight flushes, straight, and flush */
fl = flush()
st = straight()
if st && fl && shand[0].index == TEN { return ROYAL }
if st && fl { return STRFL }
if four() { return FOURK }
if full() { return FULL }
if fl { return FLUSH }
if st { return STR }
if three() { return THREEK }
if twopair() { return TWOPAIR }
if two() { return PAIR }
/* Nothing */
return NOTHING
}
/* The loop */
func play() {
//
var i int
var crd int
var c int
var hold[CARDS] int
var digit int
/* initialize deck */
for i = 0; i < CARDSINDECK; i++ { deck[i].gone = 0 }
/* initialize hold[] */
for i = 0; i < CARDS; i++ { hold[i] = 0 }
score -= bet
for i = 0; i < CARDS; i++ {
//
/* find a card not already dealt */
// Go has no 'do ... while' construct, so it's done like this:
for {
//
crd = random()%CARDSINDECK
if deck[crd].gone == 0 { break }
}
deck[crd].gone = 1
hand[i] = deck[crd]
}
showhand()
/* get cards to hold, and replace others */
for {
//
c = getchar()
if c == '\n' { break }
if c == 'q' || c == 'e' {
//
boldface = false
ANSIreset()
if ! quiet {
fmt.Printf("\nYou quit with %d chips after playing %d hands.\n",score+bet,hands)
fmt.Printf("Range: %d - %d\n", score_low, score_high)
}
os.Exit(0)
}
if c == 'b' { /* Change the bet. Only 1, 2, 3, 4, and 5 are allowed. */