缺少来自C Blackjack游戏的int数组的输出

时间:2013-02-08 14:31:18

标签: c arrays output blackjack

所以我试图在C中写一个二十一点游戏,只是为了它的乐趣(我之前从未尝试过的东西)。我已经完成了前几个步骤,例如如何设置套牌以及如何将卡片交给经销商和玩家。但是,显示的输出也没有显示我想要的东西(显示经销商只有1张卡应该显示2)。这是一个截图:

Yes, this was run on my phone, but I have the same results on my computer too.

我在手机上运行了这个(更容易获得屏幕截图),但我在使用Code :: Blocks的计算机上获得了相同的结果。无论如何,我有两个功能来处理设置卡座和显示当前卡。我使用循环处理main()函数中处理的卡片。我知道makeDeck()函数工作正常,所以我假设我的错误在其他地方。看看我的代码,让我知道发生了什么(同样,任何改进建议都将受到赞赏):

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

/*Function Prototypes*/
int* makeDeck();
    /*Usage - returns an int* to be used for the deck */

void showHands(int[], int[]);
    /*Usage - pass dealerHand[], then playerHand[] */

int main() {
        srand(time(0));     //Make a new rand() seed value
    int  x, count = 0, choice = 1;
    int* cards        = makeDeck();
    int  dealerCards[12];       //cards in hand will never exceed 11
    int  playerCards[12];       //4 A's, 4 2's, 3 3's

    /*Loop to run the game. One iteration per hand */
    while(choice != 0) {
        for(x = 0; x < 12; x++) {
            dealerCards[x] = 0;
            playerCards[x] = 0;
        }

        //Deal 2 cards to dealer and player
        for(x = 0; x < 2; x++) {
            dealerCards[x] = cards[count];
            count++;
        }
        for(x = 0; x < 2; x++) {
            playerCards[x] = cards[count];
            count++;
        }

        showHands(dealerCards, playerCards);

                /*DEBUGGING */
        printf("\nEnter 0 to exit loop: ");
        scanf("%i", &choice);
    }
    return 0;
}

//Declare placeholder variable "bunchOfCards" globally
int bunchOfCards[52];

int* makeDeck(){
    int* deck = bunchOfCards;
    int  x    = 0,
         y    = 0,
         card = 0;
    for(x = 0; x < 52; x++) {           //set all cards to 0
        deck[x] = 0;
    }
    for(x = 0; x < 4; x++) {                //set up deck
        for(y = 1; y < 14; y++) {
             card = (rand() % 52);

             //check if deck position is already used
             while(deck[card] != 0) {
                    card = (rand() % 52);
             }
             deck[card] = y;
        }
    }
    /* DEBUGGING
    for(x = 0; x < 52; x++) {
        printf("%i\t", deck[x]);
    } */
    return deck;
}

void showHands(int* dealer, int* player) {
    int x; char card[3] = { '\0', '\0', '\0' };
    puts("The hands are: \n\nDealer:");

    //Display dealer cards
    for(x = 0; x < 12; x++) {
        if(dealer[x] != 0) {
            if((dealer[x] < 10) && (dealer[x] != 1)) {
                card[0] = (char)(((int)'0') + dealer[x]);
                card[1] = '\0';
            } else if(dealer[x] == 1) {
                card[0] = 'A';
                card[1] = '\0';
            } else if(dealer[x] == 10) {
                card[0] = '1';
                card[1] = '0';
            } else if(dealer[x] == 11) {
                card[0] = 'J';
                card[1] = '\0';
            } else if(dealer[x] == 12) {
                card[0] = 'Q';
                card[1] = '\0';
            } else if(dealer[x] == 13) {
                card[0] = 'K';
                card[1] = '\0';
            }
            printf("\t%s", card);
        }

        //Display player cards
        puts("\nPlayer: ");
        for(x = 0; x < 12; x++) {
            if(player[x] != 0) {
                if((player[x] < 10) && (player[x] != 1)) {
                    card[0] = (char)(((int)'0') + player[x]);
                    card[1] = '\0';
                } else if(player[x] == 1) {
                    card[0] = 'A';
                    card[1] = '\0';
                } else if(player[x] == 10) {
                    card[0] = '1';
                    card[1] = '0';
                } else if(player[x] == 11) {
                    card[0] = 'J';
                    card[1] = '\0';
                } else if(player[x] == 12) {
                    card[0] = 'Q';
                    card[1] = '\0';
                } else if(player[x] == 13) {
                    card[0] = 'K';
                    card[1] = '\0';
                }
                printf("\t%s", card);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你的for-loops似乎互相包含。

for(x = 0; x < 12; x++) {
  if(dealer[x] != 0) {
    ...
  }
  puts("\nPlayer: ");
  for(x = 0; x < 12; x++) {
    ...
  }
}

第一个for循环以x = 0开始,第二个for循环上升到x = 12,然后第一个for循环在第二次执行之前停止,因为它达到了终止条件。