gpt4 book ai didi

c - C语言扑克程序

转载 作者:行者123 更新时间:2023-11-30 20:38:16 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com