我已经整理了一个完美地解决手牌扑克的程序。现在我希望程序实现当发出的牌是直的,同花顺,对,3种,以及4种。程序运行但在需要时从不打印正确的条件,我相信我有一些我找不到的放置或逻辑错误。这是我到目前为止所拥有的。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
#define HAND 5//draw only 5
#define TRUE 1//positive print condition
#define FALSE 0//negative print condition
//prototypes
shuffle( unsigned int wDeck[][FACES]);//shuffling modifies wDeck
deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[] );//dealing doesn't modify the arrays
//true/false conditions
typedef int bool;
bool straight, flush, four, three;
int pairs; //0,1, or 2
int main()
{
//initialize suit array
const char *suit[ SUITS ] =
{
"Hearts", "Diamonds", "Clubs", "Spades"
};
//initialize face array
const char *face[ FACES ] =
{
"Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"
};
int suitInHand[SUITS], facesInHand[FACES];
analyzeHand(suitInHand, facesInHand);
//initialize deck array
unsigned int deck[SUITS][FACES] = { 0 };
srand( time( NULL ) );//seed random-number generator
shuffle( deck );//shuffle the deck
deal( deck, face, suit );//deal the deck
}//end main
//shuffle cards in deck
shuffle( unsigned int wDeck[][FACES])
{
size_t row;//row number
size_t column;//column number
size_t card;//counter
//for each of the cards, choose slot of deck randomly
for( card = 1; card <= CARDS; ++card) {
//choose new random location until unoccupied slot found
do {
row = rand() % SUITS;
column = rand() % FACES;
}
while( wDeck[ row ][ column ] !=0);
//end do-while
//pace card number in chosen slot of deck
wDeck[ row ][ column ] = card;
}//end for
}//end function shuffle
//deal cards in deck
deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[] )
{
size_t card;//card counter
size_t row;//row counter
size_t column;//column counter
//deal each of the cards
for( card = 1; card <= HAND; ++card) {
//loop through rows of wDeck
for( row = 0; row < SUITS; ++row) {
//loop through column of wDeck for current row
for( column = 0; column < FACES; ++column) {
//if slot contains current card, display card
if( wDeck[ row ][ column ] == card ) {
printf("%5s of %-8s%c", wFace[ column ], wSuit[ row ],
card % 2 == 0 ? '\n' : '\t' );//2 column format
}//end if
}//end for
}//end for
}//end for
}//end function deal
analyzeHand(int suitsInHand[], int facesInHand[])
{
int num_consec = 0;
int rank, suit;
straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;
for (suit = 0; suit < SUITS; suit++)
if ( suitsInHand[suit] == 5)
flush = TRUE;
rank = 0;
while ( facesInHand[rank] == 0)
rank++;
for (; rank < FACES && facesInHand[rank]; rank++)
num_consec++;
if(num_consec == 5){
straight = TRUE;
return;
}
for(rank = 0; rank < FACES; rank++) {
if(facesInHand[rank] == 4)
four = TRUE;
if(facesInHand[rank] == 3)
three = TRUE;
if(facesInHand[rank] == 2)
pairs++;
}
if(four)
printf("Four of a kind\n");
else if(straight)
printf("Straight\n");
else if(pairs == 2)
printf("Two Pairs\n");
else if(pairs == 1)
printf("Pair\n");
else
printf("Better Luck Next Time\n");
}
答案 0 :(得分:0)
main()
函数中的逻辑似乎存在问题:
int suitInHand[SUITS], facesInHand[FACES];
analyzeHand(suitInHand, facesInHand);
您正在声明两个整数数组而不初始化它们,而它们是您在analyzeHand()函数中使用它们时它们是空的。
如果您想获得任何类型的有效结果,则必须首先填充这两个数组。
编辑:根据这两个数组中存储的信息类型,它们可能是analyzeHand()
函数逻辑的一些问题。