-
poker game without suits
Hi I really need help with this program. I'm separating it into 3 functions:
read_cards
anaylze_cards
and then print_results
but I'm stuck on read_cards
I'm basically doing a color blind poker game where the suits are ignoreed. There are two players and each player has 5 cards. The players compare their hands according to rankings. But I'm stuck on how to have the user input their cards in a
2x5 array. ANY help is really apperciated. I have my anaylze functions pretty much done but it needs to input like this:
02020 JJQQA
1st ply| 2nd plyr
Code:
#include <stdbool.h> /* C99 only */
#include <stdio.h>
#include <stdlib.h>
#define NUM_PLAYERS 2
#define HANDS 2
#define CARDS 5
/* external variables */
int hand[NUM_PLAYERS][5];
bool straight, flush, four, three;
int pairs; /* can be 0, 1, or 2 */
/* prototypes */
void read_cards(void);
void analyze_hand(void);
void print_result(void);
/**********************************************************
* main: Calls read_cards, analyze_hand, and print_result *
* repeatedly. *
**********************************************************/
int main(void)
{
for (;;) {
read_cards();
}
}
/**********************************************************
* read_cards: Reads the cards into the external variable *
* hand; checks for bad cards and duplicate *
* cards. *
**********************************************************/
void read_cards(void)
{
char ch, hands_ch, cards_ch;
int i, hands, cards;
bool bad_card, duplicate_card;
int cards_read = 0;
printf("Enter a card: ");
cards_ch = getchar();
switch (cards_ch) {
case '0': cards = 10; break;
case '2': cards = 0; break;
case '3': cards = 1; break;
case '4': cards = 2; break;
case '5': cards = 3; break;
case '6': cards = 4; break;
case '7': cards = 5; break;
case '8': cards = 6; break;
case '9': cards = 7; break;
case 't': case 'T': cards = 8; break;
case 'j': case 'J': cards = 9; break;
case 'q': case 'Q': cards = 10; break;
case 'k': case 'K': cards = 11; break;
case 'a': case 'A': cards = 12; break;
default: bad_card = true;
}
hands_ch = getchar();
while ((ch = getchar()) != '\n')
if (ch != ' ') bad_card = true;
if (bad_card) {
printf("Bad card; ignored.\n");
continue;
}
duplicate_card = false;
for (i = 0; i < cards_read; i++)
if (hand[i][HANDS] == hands && hand[i][CARDS] == cards) {
printf("Duplicate card; ignored.\n");
duplicate_card = true;
break;
}
if (!duplicate_card) {
hand[cards_read][HANDS] = hands;
hand[cards_read][CARDS] = cards;
cards_read++;
}
}
}
-
Code:
for( player = 0; player < PLAYERS; player++ )
readcards( player );
Something like that. Combine with something like this:
Code:
void readcards( int player )
{
if( player > -1 && player < PLAYERS )
{
...read each card into hands[ player ][ card ]
}
}
Alternately, you could make the function return a value if the read was successful or not.
Quzah.
-
So do I replace my orignial for statment with the one you listed above?
I've been working on this for about 4 hours already, just trying to get the right output so I can have a goodnight
-
So lets take a look at this, first off you have your constant definitions however fail to use them when you define your array:
Code:
int hand[NUM_PLAYERS][5]; // This could be hand[NUM_PLAYERS][CARDS]
You also define a bunch of global variables including your array; although this works so your functions can use them I would take a look at restructuring this as it is bad practice.
As for your function prototypes, to ease gaming logic you might want to think about passing a value to your read_cards function to represent the current player, thus your function prototype would be:
Code:
void read_cards(int);
For your gaming logic in your main function, just think about how you want to do this. I am assuming you would like the game to perform input cards for each player and then run your analysis and print functions for each hand. To me that sounds like a nested loop:
Code:
int main(void)
{
for(int currentHand=0; currentHand < HANDS; currentHand++){
for(int currentPlayer=0; currentPlayer < NUM_PLAYERS; currentPlayer++){
read_cards(currentPlayer);
}
analyze_hand();
print_result();
}
return(0); //<-- Do not forget this
}
Now your read cards function only needs to worry about the current player. So when you access your array you just have to worry about inputting the number of cards for that player:
Code:
void read_cards(int player)
{
for(int currentCard=0; currentCard < CARDS; currentCard++)
{
...ask for card and do your conversions....
...store the card in hand[player][currentCard]....
}
}
-
nivoca... could u email me the working code please .?
-
@ricky12155
Stop hijacking threads with "gimmetehcodez" requests.