-
Notifications
You must be signed in to change notification settings - Fork 1
/
MAIN
730 lines (656 loc) · 15.4 KB
/
MAIN
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
//library inclusions, don't worry about this for papyrus
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//function prototypes
void printintro(void); // printing the instructions and start options for the game
void printerror(void); //function to print error report
int checkante(int x); //checks if AI is eligible to ante
int Pcheckante(int x); //checks if P is eligible and wants to ante
int deal(int drawn[]); //draws a valid card based off of drawn array
int getsuit(int cin); //returns value of suit of input card, 1-4
int pocketrank(int in1, int in2, int in1s, int in2s); //fun for determining rank of pocket cards
int card;
int main(void) {
int input; //var for taking user decision input
int vektor[52]; //shuffled card array'
int drawn[52]; // tracks dealt cards
int z; //multi purpose int var
int pot; //var for the table pot
int AI1bet=0;
int AI2bet=0;
int Pbet=0;
int AI3bet=0;
int AI1pr;
int AI2pr;
int AI3pr;
int AI1play=1;
int AI2play=1;
int Pplay=1;
int AI3play=1;
int AI1cash=500;
int AI2cash=500;
int Pcash=500;
int AI3cash=500;
int AI1pc1; //pocket cards and suits for each player
int AI1pc2;
int AI1pc1s;
int AI1pc2s;
int AI2pc1;
int AI2pc2;
int AI2pc1s;
int AI2pc2s;
int Ppc1;
int Ppc2;
int Ppc1s;
int Ppc2s;
int AI3pc1;
int AI3pc2;
int AI3pc1s;
int AI3pc2s;
int bool; //generic bool value to be used for checks
srand(time(NULL)); //seed random number generator
printintro(); //print introduction
//take user input
fflush(stdin);
scanf("%d",&input);
//if statement to decide if game should start or to exit game, includes error checking
if (input==2){ //selected to quit, so exit program
printf("\n\nProgram terminated.\n\n\n\n");
fflush(stdout);
}
else if (input==1){ //chose to play, so start game
//GAME RUNS WITHIN THIS BRANCH, EXITING THIS BLOCK EXITS PROGRAM
pot=0;
printf("\n\nGAME STARTED!\n\n");
fflush(stdout);
printf("Shuffling Cards...\n\n");
fflush(stdout);
for (int i = 0; i < 52; i++) { // fill array
vektor[i] = (i+1);
}
for (int i = 0; i < 52; i++) { // shuffle array
int temp = vektor[i];
int randomIndex = rand() % 52;
vektor[i] = vektor[randomIndex];
vektor[randomIndex] = temp; //vektor is your shuffled card array
}
printf("Cards shuffled! Taking antes...\n\n");
fflush(stdout);
bool = checkante(AI1cash);
if (bool==1){
AI1cash=(AI1cash-1);
pot=pot+1;
AI1play=1;
printf("\nCP1 has anted!\n");
fflush(stdout);
}
else {
AI1play=0;
printf("\nCP1 has busted out and will not play.\n");
fflush(stdout);
}
bool = checkante(AI2cash);
if (bool==1){
AI2cash=(AI2cash-1);
pot=pot+1;
AI2play=1;
printf("\nCP2 has anted!\n");
fflush(stdout);
}
else {
AI2play=0;
printf("\nCP2 has busted out and will not play.");
fflush(stdout);
}
bool=Pcheckante(Pcash);
if (bool==1){
Pcash=(Pcash-1);
pot=pot+1;
Pplay=1;
printf("\nYou have anted!\n");
fflush(stdout);
}
else if (bool==3){
Pplay=0;
goto Exit;
}
else {
Pplay=0;
}
bool = checkante(AI3cash);
if (bool==1){
AI3cash=(AI3cash-1);
pot=pot+1;
AI3play=1;
printf("\nCP3 has anted!\n");
fflush(stdout);
}
else {
AI3play=0;
printf("\nCP3 has busted out and will not play.");
fflush(stdout);
}
//Antes have been taken, deal cards
printf("\nAntes have been taken, dealing cards!\n");
fflush(stdout);
//init drawn array:
for (int i=0; i < 52; i++){
drawn[i] = 0;
}
//deal cards to each player (who is playing) and assign to drawn array
if (AI1play==1){
//AI1 is playing so deal cards
card=deal(drawn);
z=(card-1);
drawn[z]=card;
AI1pc1=card; //AI1 Pocket card 1
AI1pc1s=getsuit(AI1pc1);
card=deal(drawn);
z=(card-1);
drawn[z]=card;
AI1pc2=card; //AI1 Pocket card 2
AI1pc2s=getsuit(AI1pc2);
}
else if (AI1play != 1){
//AI1 is not playing so skip
}
if (AI2play == 1){
//AI2 is playing so deal cards
card=deal(drawn);
z=(card-1);
drawn[z]=card;
AI2pc1=card; //AI2 pocket card 1
AI2pc1s=getsuit(AI2pc1);
card=deal(drawn);
z=(card-1);
drawn[z]=card;
AI2pc2=card; //AI2 pocket card 2
AI2pc2s=getsuit(AI2pc2);
}
else if (AI2play != 1){
//AI2 is not playing so skip
}
if (Pplay == 1){
//player is playing so deal cards
card=deal(drawn);
z=(card-1);
drawn[z]=card;
Ppc1=card; //P pocket card 1
Ppc1s=getsuit(Ppc1);
card=deal(drawn);
z=(card-1);
drawn[z]=card;
Ppc2=card; //P pocket card 2
Ppc2s=getsuit(Ppc2);
}
else if (Pplay != 1){
//player is not playing so skip
}
if (AI3play == 1){
//AI3 is playing so deal cards
card=deal(drawn);
z=(card-1);
drawn[z]=card;
AI3pc1=card; //AI3 pocket card 1
AI3pc1s=getsuit(AI3pc1);
card=deal(drawn);
z=(card-1);
drawn[z]=card;
AI3pc2=card; //AI3 pocket card 2
AI3pc2s=getsuit(AI3pc2);
}
else if (AI3play != 1){
//AI3 is not playing so skip
}
//cards are dealt, first round of betting starts
printf("\nCards have been dealt! Taking first round of bets:\n");
fflush(stdout);
// perform AI1 operations, if applicable
if (AI1play != 1){
//AI1 is not playing so skip
}
else if (AI1play == 1){
//AI1 is playing so run ops
//first check if AI1 has enough cash to bet
if (AI1cash == 0){
//is all in, can no longer bet
printf("\nAI1 is all in. AI1 checks.");
fflush(stdout);
}
else if (AI1cash > 0){
//can bet
//get rank of AI1 pocket cards
AI1pr=pocketrank(AI1pc1,AI1pc2,AI1pc1s,AI1pc2s);
//AI1pr is the rank, decide what to do
if ((AI1pr < 7)){ //first level is rank
if ( (AI2bet>AI1bet) || (Pbet>AI1bet) || (AI3bet>AI1bet) ){ //second level is what to do if others bet more
AI1play=0;
printf("\nAI1 folds!\n");
fflush(stdout);
}
else{ //no one else bet more so do what it wants
AI1bet=0;
printf("\nAI1 checks.\n");
fflush(stdout);
}
}
else if ( (AI1pr >= 7) && (AI1pr <= 13) ){ //rank level check
if ((AI2bet>AI1bet) || (Pbet>AI1bet) || (AI3bet>AI1bet)){
AI1play=0;
printf("\nAI1 folds\n");
fflush(stdout);
}
else{ //no one else bet more, so what it wants
AI1bet=5;
while (AI1cash <= AI1bet) { //runs loop if cash is less than bet
AI1bet=AI1bet-1; //lowers bet until is acceptable
}
AI1cash=AI1cash-AI1bet;
pot=pot+AI1bet;
printf("\nAI1 raised bet to %i chip(s).\n",AI1bet);
fflush(stdout);
}
}
else if( (AI1pr >= 14) && (AI1pr <= 19) ){ //rank level check
if ((AI2bet>AI1bet) || (Pbet>AI1bet) || (AI3bet>AI1bet)){
AI1play=0;
printf("\nAI1 folds\n");
fflush(stdout);
}
else{
AI1bet=12;
while (AI1cash <= AI1bet) { //runs loop if cash is less than bet
AI1bet=AI1bet-1; //lowers bet until is acceptable
}
AI1cash=AI1cash-AI1bet;
pot=pot+AI1bet;
printf("\nAI1 raised bet to %i chip(s).\n",AI1bet);
}
}
else if ((AI1pr >= 20) && (AI1pr <= 26)){ //rank level check
if ((AI2bet>AI1bet) || (Pbet>AI1bet) || (AI3bet>AI1bet)){ //at this rank, call any bet if possible
if (AI2bet>AI1bet){
if (AI1cash>=AI2bet){
AI1bet=AI2bet;
AI1cash=AI1cash-AI1bet;
pot=pot+AI1bet;
printf("\nAI1 called AI2's bet of %i chip(s).\n",AI1bet);
fflush(stdout);
}
else{
AI1play=0;
printf("\nAI1 folds.\n");
fflush(stdout);
}
}
else if (Pbet>AI1bet){
if (AI1cash>=Pbet){
AI1bet=Pbet;
AI1cash=AI1cash-AI1bet;
pot=pot+AI1bet;
printf("\nAI1 called your bet of %i chip(s).\n",AI1bet);
fflush(stdout);
}
else{
AI1play=0;
printf("\nAI1 folds.\n");
fflush(stdout);
}
}
else if (AI3bet>AI1bet){
if (AI1cash>=AI3bet){
AI1bet=AI3bet;
AI1cash=AI1cash-AI1bet;
pot=pot+AI1bet;
printf("\nAI1 called AI3's bet of %i chip(s).\n",AI1bet);
fflush(stdout);
}
else{
AI1play=0;
printf("\nAI1 folds.\n");
fflush(stdout);
}
}
}
else{ //no one else bet, do what it wants
AI1bet=18;
while (AI1cash <= AI1bet) { //runs loop if cash is less than bet
AI1bet=AI1bet-1; //lowers bet until is acceptable
}
AI1cash=AI1cash-AI1bet;
pot=pot+AI1bet;
printf("\nAI1 raised bet to %i chip(s).\n",AI1bet);
}
}
}
}
//AI2 ops, if applicable
if (AI2play != 1){
//AI2 is not playing so skip
}
else if (AI2play == 1){
//AI2 is playing so run ops
//first check if AI2 has enough cash to bet
if (AI2cash == 0){
//is all in, can no longer bet
printf("\nAI2 is all in. AI2 checks.");
fflush(stdout);
}
else if (AI2cash > 0){
//can bet
//get rank of AI2 pocket cards
AI2pr=pocketrank(AI2pc1,AI2pc2,AI2pc1s,AI2pc2s);
//AI2pr is the rank, decide what to do
if ((AI2pr < 7)){ //first level is rank
if ( (AI1bet>AI2bet) || (Pbet>AI2bet) || (AI3bet>AI2bet) ){ //second level is what to do if others bet more
AI2play=0;
printf("\nAI2 folds!\n");
fflush(stdout);
}
else{ //no one else bet more so do what it wants
AI2bet=0;
printf("\nAI2 checks.\n");
fflush(stdout);
}
}
else if ( (AI2pr >= 7) && (AI2pr <= 13) ){ //rank level check
if ((AI1bet>AI2bet) || (Pbet>AI2bet) || (AI3bet>AI2bet)){
AI2play=0;
printf("\nAI2 folds!\n");
fflush(stdout);
}
else{ //no one else bet more, so what it wants
AI2bet=5;
while (AI2cash <= AI2bet) { //runs loop if cash is less than bet
AI2bet=AI2bet-1; //lowers bet until is acceptable
}
AI2cash=AI2cash-AI2bet;
pot=pot+AI2bet;
printf("\nAI2 raised bet to %i chip(s).\n",AI2bet);
fflush(stdout);
}
}
else if( (AI2pr >= 14) && (AI2pr <= 19) ){ //rank level check
if ((AI1bet>AI2bet) || (Pbet>AI2bet) || (AI3bet>AI2bet)){
AI2play=0;
printf("\nAI2 folds!\n");
fflush(stdout);
}
else{
AI2bet=12;
while (AI2cash <= AI2bet) { //runs loop if cash is less than bet
AI2bet=AI2bet-1; //lowers bet until is acceptable
}
AI2cash=AI2cash-AI2bet;
pot=pot+AI2bet;
printf("\nAI2 raised bet to %i chip(s).\n",AI2bet);
}
}
else if ((AI2pr >= 20) && (AI2pr <= 26)){ //rank level check
if ((AI1bet>AI2bet) || (Pbet>AI2bet) || (AI3bet>AI2bet)){ //at this rank, call any bet if possible
if (AI1bet>AI2bet){
if (AI2cash>=AI1bet){
AI2bet=AI1bet;
AI2cash=AI2cash-AI2bet;
pot=pot+AI2bet;
printf("\nAI2 called AI1's bet of %i chip(s).\n",AI2bet);
fflush(stdout);
}
else{
AI2play=0;
printf("\nAI1 folds.\n");
fflush(stdout);
}
}
else if (Pbet>AI2bet){
if (AI2cash>=Pbet){
AI2bet=Pbet;
AI2cash=AI2cash-AI2bet;
pot=pot+AI2bet;
printf("\nAI2 called your bet of %i chip(s).\n",AI2bet);
fflush(stdout);
}
else{
AI2play=0;
printf("\nAI2 folds.\n");
fflush(stdout);
}
}
else if (AI3bet>AI2bet){
if (AI2cash>=AI3bet){
AI2bet=AI3bet;
AI2cash=AI2cash-AI2bet;
pot=pot+AI2bet;
printf("\nAI2 called AI3's bet of %i chip(s).\n",AI2bet);
fflush(stdout);
}
else{
AI1play=0;
printf("\nAI2 folds.\n");
fflush(stdout);
}
}
}
else{ //no one else bet, do what it wants
AI2bet=18;
while (AI2cash <= AI2bet) { //runs loop if cash is less than bet
AI2bet=AI2bet-1; //lowers bet until is acceptable
}
AI2cash=AI2cash-AI2bet;
pot=pot+AI2bet;
printf("\nAI2 raised bet to %i chip(s).\n",AI2bet);
}
}
}
}
//start player menu
//run AI3 operations
}
else { //invalid input, terminate program
printerror(); //function prints error and terminates program
}
Exit:
return EXIT_SUCCESS;
}
//end of main
//function definitions:
void printintro(void) //defining function for printing game instructions
{
printf("\nWelcome to Texas Hold 'Em Poker!\n");
fflush(stdout);
printf("Would you like to play?\n");
fflush(stdout);
printf("Type '1' for 'Yes'\n");
fflush(stdout);
printf("Type '2' for 'No'\n");
fflush(stdout);
printf("Your response: ");
fflush(stdout);
}
void printerror(void) //defining function for printing error report
{
printf("\n\nError! Invalid input!\n Program terminated.\n\n\n");
fflush(stdout);
}
int checkante(int x){
int i;
if (x>=1){
i=1;
}
else {
i=0;
}
return i;
}
int Pcheckante(int x){
int i;
int in;
if (x>=1){
printf("\nWould you like to ante?\n1) Yes\n2) No\n3)Leave table\nYour response: ");
fflush(stdout);
fflush(stdin);
scanf("%d",&in);
if (in==1){
i=1;
}
else if (in==2){
printf("\nYou have chosen not to ante! Spectating game.");
fflush(stdout);
i=2;
}
else if (in==3){
printf("\nYou have chosen to stand up from the table.\n");
fflush(stdout);
i=3;
}
else{
printf("\n INVALID INPUT! Defaulting to spectate!");
i=0;
}
}
else {
printf("\nYou have busted out! Exiting game.\n");
fflush(stdout);
i=0;
}
return i;
}
int deal(int drawn[]){
int i;
int x;
int z;
int card;
//first check to make sure not all cards have been drawn
z=0;
i=0;
for (int y=0;y<52;y++) {
if (drawn[z]==0){
//continue
z++;
}
else {
//add one to counter
i++;
z++;
}
}
if (i==52){
//all cards have been dealt, report error and exit
printf("\nALL CARDS HAVE BEEN DEALT, ERROR\n");
card=0;
goto Funexit;
}
//all cards have not been dealt, so try to deal a card
retry:
x=(rand()%52+1); //gets a card ID to deal
z=0;
for (int y=0;y<52;y++) {
if (drawn[z]==x) {
//ID is a repeat so try again
goto retry; //picks another card
}
else{
//not a repeat, continue
z++;
}
}
// loop has finished so card is valid
card=x;
Funexit:
return card;
}
int getsuit(int cin){
int suit;
if (cin<14){
//is a heart
suit = 1;
goto funexit;
}
else if ((cin>=14) && (cin<27)){
//is a diamond
suit = 2;
goto funexit;
}
else if ((cin>=27) && (cin<40)){
//is a club
suit = 3;
goto funexit;
}
else {
//is a spade
suit = 4;
goto funexit;
}
funexit:
return suit;
}
int pocketrank(int in1, int in2, int in1s, int in2s){
int pc1;
int pc1s;
int pc2;
int pc2s;
int rank;
//find lowest p card and assign accordingly, use pc1 and pc2 for testing
if (in1<in2){
pc1=in1;
pc1s=in1s;
pc2=in2;
pc2s=in2s;
}
else{
pc1=in2;
pc1s=in2s;
pc2=in1;
pc2s=in1s;
}
//check for pairs
if (((pc1+13) == pc2) || ((pc1+26) == pc2) || ((pc1+39) == pc2)){
//is a pair, check low card suit to determine rank
if (pc1s == 1){
//is a heart
rank = (pc1+13);
goto funexit;
}
else if (pc1s == 2){
//is a diamond
rank = pc1;
goto funexit;
}
else if (pc1s == 3){
//is a club
rank = (pc1-13);
goto funexit;
}
else {
//is a spade
rank = (pc1-26);
goto funexit;
}
}
else {
//is not a pair, higher id is pc2, determine rank
if (pc2s == 1){
//is a heart
rank = (pc2);
goto funexit;
}
else if (pc2s == 2){
//is a diamond
rank = (pc2-13);
goto funexit;
}
else if (pc2s == 3){
//is a club
rank = (pc2-26);
goto funexit;
}
else {
//is a spade
rank = (pc2-39);
goto funexit;
}
}
funexit:
return rank;
}