hi guys and gals, just a young padawan learner trying to write this poker program from scratch. i know their are programs out their but nothing like the one you write your self. Im trying to have my program read in the number of players from the keyboard input. I have eliminated all the numbers i dont want. but what if someone enters in letters or special characters how do i eliminate all the keyboard functions except my small range of numbers. My first guess was to have the 1-20 in ascii and all other ascii codes would be a bad input didnt know how to write it. this is probaly elimentary but im stomped heres the little code i have wrote. numofplayers is a int i made it a char but that didnt work. also i hear goto statements are bad how could i avoid the first two goto statements in this elimentary program. thanks i appreciate any help.
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
//================================================== main
int main() {
int numofplayers=0;
char random_play_flag;
char play_again;
//char CARD_FACES[13] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'};
//char CARD_SUITS[4] = {'S', 'H', 'C', 'D'};
char cards[52][25] = { "SA", "S2", "S3", "S4", "S5", "S6", "S7",
"S8", "S9","ST", "SJ", "SQ","SK",
"HA", "H2", "H3", "H4", "H5", "H6", "H7"
, "H8", "H9", "HT", "HJ", "HQ","HK",
"CA", "C2", "C3", "C4", "C5", "C6", "C7"
, "C8", "C9", "CT", "CJ", "CQ","CK",
"DA", "D2", "D3", "D4", "D5", "D6", "D7"
, "D8", "D9", "DT", "DJ", "DQ","DK" };
char players[52][25];
char deck[52][25];
char dealer[52][25];
char complete_hand[52][25];
play_again_label:
cout << endl;
cout << "Enter Number of Players in Game: ";
cin >>numofplayers;
if (numofplayers < 1 || numofplayers > 20)
{
cout << "Please try a number less than 20";
goto play_again_label;
}
start_again:
cout << "Do you want Random Play (Y/N)";
cin >> random_play_flag ;
if ( random_play_flag != 'y' && random_play_flag != 'Y' && random_play_flag != 'n' && random_play_flag != 'N')
{
cout << "Please type y or n";
cout << endl;
goto start_again;
}
//shuffle deck
srand((unsigned)time(0));
int gen_random=0;
int random_integer;
int lowest=0, highest=51;
int range=(highest-lowest)+1;
for (int d=0;d<52;d++){
pick_num:
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
// printf("rand num=%d\n", random_integer);
strcpy(deck[d],cards[random_integer]);
int x=0;
while( x < d){
if (!strcmp(deck[x], cards[random_integer])){
// printf("DUPS deck=%s random card=%s\n", deck[x], cards[random_integer]);
goto pick_num;
}
x++;
}
}
// for (int dd=0;dd<52;dd++){
// printf("----->Random card =%s\n",deck[dd]);
// }
if (random_play_flag == 'Y' || random_play_flag =='y')
{
for (int numcards=0;numcards<2;numcards++){ //2 sets of cards
for(int index=0; index < numofplayers; index++){
if (numcards == 0){
strcpy(players[index],deck[index]);
}
else{
strcat(players[index]," ");
strcat(players[index],deck[index+numofplayers]);
// printf("IN card 2=%d %d %s\n",numcards,index,deck[index+numofplayers]);
}
}
}
}
else if (random_play_flag == 'N' || random_play_flag =='n'){
// cout << "No random play" ;
for (int l=0; l<numofplayers;l++){
printf("\n\nEnter Hand for Player %d = \n\n",l+1);
cout << "Enter Card 1:\n";
cin >> players[l];
cout << "Enter Card 2:\n";
strcat(players[l]," ");
cin >> deck[0];
strcat(players[l],deck[0]);
}
}
//Dealer
//Burn a card - start with (numplayers*2)+1
//Deal 3 cards
strcpy(dealer[0],deck[(numofplayers*2)+1]);
strcat(dealer[0]," ");
strcat(dealer[0],deck[(numofplayers*2)+2]);
strcat(dealer[0]," ");
strcat(dealer[0],deck[(numofplayers*2)+3]);
//1 dead card
//deal card
strcat(dealer[0]," ");
strcat(dealer[0],deck[(numofplayers*2)+5]);
//1 dead card
//deal card
strcat(dealer[0]," ");
strcat(dealer[0],deck[(numofplayers*2)+7]);
for (int z=0; z<numofplayers;z++){
printf("\n\nPlayer %d Hand\n", z+1);
printf("%s\n\n",players[z]);
}
printf("\n\nDEALERS HAND=%s\n\n",dealer[0]);
//Evaluate Hand for a flush
for (int tt=0;tt<numofplayers;tt++){
int diamondflush=0;
int spadesflush=0;
int clubflush=0;
int heartflush=0;
strcpy(complete_hand[tt],dealer[0]);
strcat(complete_hand[tt]," ");
strcat(complete_hand[tt],players[tt]);
printf("COMPLETE HAND FOR PLAYER %d = ",tt+1);
for (int u=0;u<21;u++){
if (complete_hand[tt][u] == 'D')
printf("%c",char(5));
else if (complete_hand[tt][u] == 'S')
printf("%c",char(4));
else if (complete_hand[tt][u] == 'C')
printf("%c",char(3));
else if (complete_hand[tt][u] == 'H')
printf("%c",char(6));
else if (complete_hand[tt][u] == 'T')
printf("%s","10");
else
printf("%c",complete_hand[tt][u]);
}
printf("\n");
//printf("COMPLETE HAND FOR PLAYER %d = %s\n",tt+1, complete_hand[tt]);
//printf("%c\n",complete_hand[0][1]);
for (int r=0; r<21;r=r+3)
{
if (complete_hand[tt][r] == 'D')
diamondflush++;
if (complete_hand[tt][r] == 'S')
spadesflush++;
if (complete_hand[tt][r] == 'C')
clubflush++;
if (complete_hand[tt][r] == 'H')
heartflush++;
//printf("%c\n",char(5));
}
if (diamondflush >=5)
printf("You got a Flush\n");
if (spadesflush >=5)
printf("You got a Flush\n");
if (clubflush >=5)
printf("You got a Flush\n");
if (heartflush >=5)
printf("You got a Flush\n");
int twos=0;
int threes=0;
int fours=0;
int fives=0;
int six=0;
int seven=0;
int eight=0;
int nine=0;
int ten=0;
int ace=0;
int king=0;
int queen=0;
int jack=0;
char three_cards='N';
char two_cards='N';
for (int rr=1; rr<21;rr=rr+3)
{
if (complete_hand[tt][rr] == '2')
twos++;
if (complete_hand[tt][rr] == '3')
threes++;
if (complete_hand[tt][rr] == '4')
fours++;
if (complete_hand[tt][rr] == '5')
fives++;
if (complete_hand[tt][rr] == '6')
six++;
if (complete_hand[tt][rr] == '7')
seven++;
if (complete_hand[tt][rr] == '8')
eight++;
if (complete_hand[tt][rr] == '9')
nine++;
if (complete_hand[tt][rr] == 'T')
ten++;
if (complete_hand[tt][rr] == 'A')
ace++;
if (complete_hand[tt][rr] == 'K')
king++;
if (complete_hand[tt][rr] == 'Q')
queen++;
if (complete_hand[tt][rr] == 'J')
jack++;
//printf("number=%c\n",complete_hand[tt][rr]);
}
if (twos==4)
printf("Four of a Kind 2\n");
else if (twos==3){
printf("Three of a Kind 2\n");
three_cards='Y';
}
else if (twos==2){
printf("Pairs 2\n");
two_cards='Y';
}
if (threes==4)
printf("Four of a Kind 3\n");
else if (threes==3){
printf("Three of a Kind 3\n");
three_cards='Y';
}
else if (threes==2){
printf("Pairs 3\n");
two_cards='Y';
}
if (fours==4)
printf("Four of a Kind 4\n");
else if (fours==3){
printf("Three of a Kind 4\n");
three_cards='Y';
}
else if(fours==2){
printf("Pairs 4\n");
two_cards='Y';
}
if (fives==4)
printf("Four of a Kind 5\n");
else if(fives==3){
printf("Three of a Kind 5\n");
three_cards='Y';
}
else if (fives==2){
printf("Pairs 5\n");
two_cards='Y';
}
if (six==4)
printf("Four of a Kind 6\n");
else if (six==3){
printf("Three of a Kind 6\n");
three_cards='Y';
}
else if (six==2){
printf("Pairs 6\n");
two_cards='Y';
}
if (seven==4)
printf("Four of a Kind 7\n");
else if(seven==3){
printf("Three of a Kind 7\n");
three_cards='Y';
}
else if(seven==2){
printf("Pairs 7\n");
two_cards='Y';
}
if (eight==4)
printf("Four of a Kind 8\n");
else if (eight==3){
printf("Three of a Kind 8\n");
three_cards='Y';
}
else if (eight==2){
printf("Pairs 8\n");
two_cards='Y';
}
if (nine==4)
printf("Four of a Kind 9\n");
else if (nine==3){
printf("Three of a Kind 9\n");
three_cards='Y';
}
else if (nine==2){
printf("Pairs 9\n");
two_cards='Y';
}
if (ten==4)
printf("Four of a Kind 10\n");
else if (ten==3){
printf("Three of a Kind 10\n");
three_cards='Y';
}
else if (ten==2){
printf("Pairs 10\n");
two_cards='Y';
}
if (ace==4)
printf("Four of a Kind A\n");
else if (ace==3){
printf("Three of a Kind A\n");
three_cards='Y';
}
else if (ace==2){
printf("Pairs A\n");
two_cards='Y';
}
if (king==4)
printf("Four of a Kind K\n");
else if (king==3){
printf("Three of a Kind K\n");
three_cards='Y';
}
else if (king==2){
printf("Pairs Y\n");
two_cards='Y';
}
if (queen==4)
printf("Four of a Kind Q\n");
else if (queen==3){
printf("Three of a Kind Q\n");
three_cards='Y';
}
else if (queen==2){
printf("Pairs Q\n");
two_cards='Y';
}
if (jack==4)
printf("Four of a Kind J\n");
else if (jack==3){
printf("Three of a Kind J\n");
three_cards='Y';
}
else if(jack==2){
printf("Pairs J\n");
two_cards='Y';
}
if (three_cards =='Y' && two_cards == 'Y')
printf("Full House\n");
}
cout << "Play again? ";
cin >>play_again;
if (play_again == 'Y' || play_again =='y') {
system("CLS");
goto play_again_label;
}
return 0;
}//end main
<< moderator edit: added [code][/code] tags >>