gpt4 book ai didi

对扑克手牌进行分类 [同花顺失败]

转载 作者:行者123 更新时间:2023-11-30 20:04:31 25 4
gpt4 key购买 nike

我正在关注C 编程:现代方法一书中的编程项目,我已将 2 个问题纳入我的程序中,但未能实现第三个问题,即找到皇家同花顺手牌。

这是我到目前为止所做的:

/* Classifies a poker hand */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5
#define ROYAL_FLUSH ('A' || 'a' || 'K' || 'k' || 'Q' || 'q' || 'J' || 'j' || 'T' || 't')
#define ACE_LOW (('A' || 'a') && '2' && '3' && '4' && '5')

/* external variables */
/* int ROYAL_FLUSH[8, 9, 10, 11, 12]
int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS]; */
bool royal_flush, ace_low, straight, flush, four, three;
int pairs; /* can be 0, 1 or 2 */

/* Prototypes */
int read_cards(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS]);
int analyze_hand(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS]);
void print_result(void);


int main(void)
{

int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS];
for(;;) {
read_cards(num_in_rank, num_in_suit);
analyze_hand(num_in_rank, num_in_suit);
print_result();
}
}

int read_cards(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS])
{

bool card_exists[NUM_RANKS][NUM_SUITS];
char ch, rank_ch, suit_ch;
int rank, suit;
bool bad_card;
int cards_read = 0;

for (rank = 0; rank < NUM_RANKS; rank++) {
num_in_rank[rank] = 0;
for (suit = 0; suit < NUM_SUITS; suit++)
card_exists[rank][suit] = false;
}

for (suit = 0; suit < NUM_SUITS; suit++)
num_in_suit[suit] = 0;

while (cards_read < NUM_CARDS) {
bad_card = false;

printf("Enter a card: ");

rank_ch = getchar();
switch (rank_ch) {
case '0': exit(EXIT_SUCCESS);
case '2': rank = 0; break;
case '3': rank = 1; break;
case '4': rank = 2; break;
case '5': rank = 3; break;
case '6': rank = 4; break;
case '7': rank = 5; break;
case '8': rank = 6; break;
case '9': rank = 7; break;
case 't': case 'T': rank = 8; break;
case 'j': case 'J': rank = 9; break;
case 'q': case 'Q': rank = 10; break;
case 'k': case 'K': rank = 11; break;
case 'a': case 'A': rank = 12; break;
default: bad_card = true;
}

suit_ch = getchar();
switch (suit_ch) {
case 'c': case 'C': suit = 0; break;
case 'd': case 'D': suit = 1; break;
case 'h': case 'H': suit = 2; break;
case 's': case 'S': suit = 3; break;
default: bad_card = true;
}

while ((ch = getchar()) != '\n')
if (ch != ' ') bad_card = true;

if (bad_card)
printf("Bad card; ignored.\n");
else if (card_exists[rank][suit])
printf("Duplicate card; ignored.\n");
else {
num_in_rank[rank]++;
num_in_suit[suit]++;
card_exists[rank][suit] = true;
cards_read++;
}
}
return 0;
}

int analyze_hand(int num_in_rank[NUM_RANKS], int num_in_suit[NUM_SUITS])
{

int num_consec = 0;
int rank, suit;

royal_flush = false;
straight = false;
ace_low = false;
flush = false;
four = false;
three = false;
pairs = 0;


/*check for royal flush */
/* int arr[5] = {8, 9, 10, 11, 12};
int i; */
if (flush)
for (rank = 0; rank < NUM_RANKS; rank++)
if (num_in_rank[rank] == ROYAL_FLUSH)
continue;
else
break;

royal_flush = true;


/* check for flush */

for (suit = 0; suit < NUM_SUITS; suit++)
if (num_in_suit[suit] == NUM_CARDS)
flush = true;

/* check for ace-low straight */

for (rank = 0; rank < NUM_RANKS; rank++)
if (num_in_rank[rank] == ACE_LOW)
ace_low = true;

/* check for straight */

rank = 0;
while (num_in_rank[rank] == 0) rank++;
for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
num_consec++;
if (num_consec == NUM_CARDS) {
straight = true;
return 0;
}

/* check for 4 of a kind, 3 of a kind and pairs */

for (rank = 0; rank < NUM_RANKS; rank++) {
if (num_in_rank[rank] == 4)
four = true;
if (num_in_rank[rank] == 3)
three = true;
if (num_in_rank[rank] == 2)
pairs++;
}
return 0;
}

void print_result(void)
{

if (royal_flush && flush && straight)
printf("\nRoyal Flush");
else if (straight && flush && royal_flush == false)
printf("\nStraight Flush");
else if (four)
printf("\nFour of a kind");
else if (three && pairs == 1)
printf("\nFull House");
else if (flush)
printf("\nFlush");
else if (straight)
printf("\nStraight");
else if (ace_low)
printf("\nAce-low straight");
else if (three)
printf("\nThree of a kind");
else if (pairs == 2)
printf("\nTwo Pairs");
else if (pairs == 1)
printf("\nPair");
else
printf("\nHigh card");

printf("\n\n");

}

当输入的牌甚至不是皇家同花牌但实际上是同花牌时,我的代码会打印出皇家同花顺

我是c语言新手,所以请不要批评。

语言:c99 ;编译器:gcc

最佳答案

这个:

if (num_in_rank[rank] == ROYAL_FLUSH)

与此相同:

if (num_in_rank[rank] == ('A' || 'a' || 'K' || 'k' || 'Q' || 'q' || 'J' || 'j' || 'T' || 't'))

不做你认为它做的事。它不会将 num_in_rank[rank]Aa 等进行比较。它所做的是首先在 之间执行逻辑或>Aa,然后将该结果与 K 进行逻辑或,等等。

您需要将您的变量与每个变量进行单独比较。

你可以这样做:

if ((num_in_rank[rank] == 'A') || (num_in_rank[rank] == 'a') || ...

或者这个:

char in_royal[] = { 'A', 'a', ... };

for (i=0;i<sizeof(in_royal);i++) {
if (num_in_rank[rank] == in_royal[i]) {
...

其布局方式也存在问题:

  if (flush)
for (rank = 0; rank < NUM_RANKS; rank++)
if (num_in_rank[rank] == ROYAL_FLUSH)
continue;
else
break;

royal_flush = true;

最后一行位于 if block 之外,您将无法捕获所需的情况。确保使用大括号:

  if (flush) {
royal_flush = true;
for (rank = 0; rank < NUM_RANKS; rank++) {
if ((num_in_rank[rank] == 'A') || (num_in_rank[rank] == 'a') || ...) {
continue;
} else {
royal_flush = false;
break;
}
}
}

作为一项改进,无需单独检查皇家同花顺中的所有牌,只需检查 A。连同对顺子和同花的单独检查(当前不在您的代码中)将捕获这一点。然后你可以这样做:

if (straight && flush && has_ace)
printf("\nRoyal Flush");

关于对扑克手牌进行分类 [同花顺失败],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40114054/

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