c - C语言扑克程序

标签 c arrays sorting pointers poker

我已经编写了一个可以完美处理手牌扑克的程序。现在我希望程序能够实现什么时候所发的牌是顺子、同花、对子、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");
}

最佳答案

您的 main() 函数中的逻辑似乎有问题:

int suitInHand[SUITS], facesInHand[FACES];

analyzeHand(suitInHand, facesInHand);

您声明了两个整数数组,但没有初始化它们,并且在它们为空时在您的analyzeHand()函数中使用它们。

如果您想获得任何类型的有效结果,则必须首先填充这 2 个数组。

编辑:根据这两个数组中存储的信息类型,它们可能与您的 analyzeHand() 函数的逻辑有关。

关于c - C语言扑克程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29730737/

相关文章:

c - 驱动程序开发 (Windows) : What is RTL?

java - JAVA中从ArrayList随机生成字符串而不进行替换

sorting - MongoDB如何进行多键排序?

java - 使用索引数组对列表进行排序的最佳方法

java - 在java 8中从父级排序子级列表

c - 你知道支持 COW 事务的 C 字典吗?

我可以在 "... "之前传递数组类型吗?在哪里可以找到 va_* 的宏代码

c - 在没有警告的情况下将可变矩阵作为常量传递

python - 如何创建行间等差的 Numpy 二维数组?

arrays - 遍历数组,如果值存在则返回 TRUE