我已经尝试了一切!我应该添加皇家同花顺部分。我理解直的,齐平的和齐平的冲洗部分,但我似乎无法获得皇家同花顺部分。最后,代码应该打印出50000张牌中有多少直,冲,直冲和皇室冲洗。这开始是我与合作伙伴的一个实验室,但现在为了额外的功劳,我应该添加皇家同花顺部分而且我一无所知。提前谢谢。
#include <stdio.h>
#define SIZE 50000
typedef struct card_t_struct{
int value; // 1 through 13
int suit; // 0 is clubes, 1 is diamonds, 2 is spades, 3 is hearts
} card_t;
void printCard(card_t card){
if(card.value == 1){
printf("Ace");
}else if(card.value == 11){
printf("Jack");
}else if(card.value == 12){
printf("Queen");
}else if(card.value == 13){
printf("King");
}else{
printf("%d", card.value);
}
printf(" of ");
if(card.suit == 0){
printf("Clubs.");
}else if(card.suit == 1){
printf("Diamonds.");
}else if(card.suit == 2){
printf("Spades.");
}else if(card.suit == 3){
printf("Hearts.");
}else{
printf("%d not a suit", card.suit); // Error message
}
printf("\n");
}
void hand(card_t* a){
int i = 0;
for(i = 0; i < 5; i++){
printCard(a[i]);
}
}
void shuffle(card_t* array, int length){
int i = 0;
for(i = 0; i < length * 2; i++){
int from = rand() % length;
int to = rand() % length;
card_t temp = array[from];
array[from] = array[to];
array[to] = temp;
}
}
void bubble(card_t* f, int length){
int i = 0;
int bub_num = 0;
for(bub_num = 0; bub_num < length; bub_num++){
for(i = 0; i < length - 1; i++){
if(f[i].value > f[i+1].value){ // Wrong order
int temp = f[i].value;
f[i].value = f[i+1].value;
f[i+1].value = temp;
}else{
}
}
}
}
int isFlush(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i].suit != deck[0].suit){
return 0;
}
}
return 1;
}
int isStraight(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i+1].value != deck[i].value + 1){
return 0;
}
}
return 1;
}
int isSFlush(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i+1].value != deck[i].value + 1){
return 0;
}
}
for(i = 1; i < 5; i++){
if(deck[i].suit != deck[0].suit){
return 0;
}
}
return 1;
}
int isRFlush(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i].value != deck[i].value){
return 0;
}
}
for(i = 1; i < 5; i++){
if(deck[i].suit != deck[0].suit){
return 0;
}
}
return 1;
}
int main(){
srand(time(0));
card_t deck[52] = {};
int i = 0;
int flush = 0;
int straight = 0;
int straight_flush = 0;
int royal_flush = 0;
int suit = 0;
for(suit = 0; suit < 4; suit++){
int value = 1;
for(value = 1; value <= 13; value++){
deck[i].suit = suit;
deck[i].value = value;
i++;
}
}
for(i = 0; i < SIZE; i++){
shuffle(deck, 52);
hand(deck);
if(isFlush(deck) ){
flush++;
}
printf("\n");
bubble(deck, 5);
if(isStraight(deck) ){
straight++;
}
if(isSFlush(deck) ){
straight_flush++;
}
if(isRFlush(deck) ){
royal_flush++;
}
}
printf("The number of flushes you have is: %d\n", flush);
printf("The number of straights you have is: %d\n", straight);
printf("The number of straight flushes you have is: %d\n", straight_flush);
printf("The number of royal flushes you have is: %d\n", royal_flush);
}
答案 0 :(得分:0)
根据现有的IsStraight(...)
功能,我假设您的isRFlush(...)
功能正在通过一张五张牌(不是整个牌组),其牌是按升序排列的?< / p>
int isRFlush(card_t* deck) {
int i = 0; // zero-based index into your hand of five cards (0-4)
// This next bit DOESN'T ACTUALLY DO ANYTHING. The reason is that comparing anything
// with itself will always result in an equality (i.e. 1==1, 3.14==3.14, a==a, etc),
// so this comparison always returns false, and the return 0 after the if statement
// is never taken, regardless of the value of the cards passed in.
/*
for (i = 1; i < 5; i++) {
if (deck[i].value != deck[i].value) { // <--- Never false, no matter what
return 0;
}
}
*/
// What the other isXxxx methods above were doing was ensuring that each 'next' card
// was 'one higher' than the card that came before it in the hand. If that's found to
// be untrue for any card in the hand, then the function exits with a zero.
//
// That's the logic for a STRAIGHT, but we're looking for a ROYAL STRAIGHT, so the
// hand HAS to be "10 J Q K A" (all of the same suit, but we'll check that part later).
//
// Since the Ace represents a non-sequential value, I think you may as well just do
// this using five comparisons without the loop:
if ( deck[0].value != 1 // if 1st card isn't Ace
|| deck[1].value != 10 // or 2nd card isn't 10
|| deck[2].value != 11 // or 3rd card isn't Jack
|| deck[3].value != 12 // or 4th card isn't Queen
|| deck[4].value != 13) { // or 5th card isn't King
return 0; // Not a royal straight
}
// If we've made it this far, we know that we have a ROYAL STRAIGHT (e.g. 10, J, Q, K, A)
// Now, we need to ensure that we also have a FLUSH
// Loop through the 2nd to last cards, ensuring they are all
// of the same suit as the first card in the hand
for (i = 1; i < 5; i++) {
if (deck[i].suit != deck[0].suit) {
return 0; // Not a flush
}
}
return 1; // This hand IS a Royal Straight Flush (aka Royal Flush)
}
最终说明:
此程序可能还有其他错误。例如,isStraight(...)
会跳过第一张卡并在最后四张卡片上循环,但是当它应该减去一张时它会添加到索引i
:
// Note: This function doesn't recognize a non-sequential "ace-high" flush
// (e.g. 10,J,Q,K,A).
int isStraight(card_t* deck) {
int i = 0;
for (i = 1; i < 5; i++) {
// Loop values of i will be [1, 2, 3, 4], which correspond to the 2nd through 5th card in the hand passed in.
// To check these against the previous card, you should subtract one (vs adding one).
/*
if (deck[i+1].value != deck[i].value + 1) { // <--- Array Index Out Of Bounds
*/
// If card in hand isn't 'one higher' than the previous card...
if (deck[i-1].value != deck[i].value + 1) {
return 0;
}
}
return 1;
}
由于您已经有一个检查手牌是否有冲洗的功能,可以利用它来通过在isRFlush(...)
功能中调用来保存一些输入:
int isRFlush(card_t* deck) {
if ( deck[0].value != 1 // if 1st card isn't Ace
|| deck[1].value != 10 // or 2nd card isn't 10
|| deck[2].value != 11 // or 3rd card isn't Jack
|| deck[3].value != 12 // or 4th card isn't Queen
|| deck[4].value != 13) { // or 5th card isn't King
return 0; // Not a royal straight
}
// At this point, you know it's a ROYAL STRAIGHT, so just return whether or not it's also
// a FLUSH
return isFlush(deck);
}