二十一点程序多个问题(交易,错误,命中)

时间:2015-02-07 19:52:00

标签: c

我只用了几个星期的c,所以我很可能会忽略一些明显的错误。我看过其他主题,但我不太了解我正在阅读的内容。 该计划假设一个无限大的套牌。

已知问题:

  • clearBuffer目前尚未使用,我正在尝试不同的事情。
  • dealerhand不能正确添加值。
  • 比较功能尚不存在。
  • 多个非数字输入会导致重复出现错误消息。
  • 试图“击中”崩溃程序。

当前代码:

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

int deal()
{
    unsigned long timenow;
    timenow = time(NULL);
    srand(timenow);
    int deck[] ={2,3,4,5,6,7,8,9,10,10,10,10,11};
    return deck[rand() % 13];
        printf("%d\n", deal());
}

void clearBuffer(){
    char ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
}

int dealerhand()
{
    int dealerhand[10];
    int dealertotal = 0;
    while (dealertotal < 17)
    {
        dealertotal = deal();
        printf("%d\n dx", deal());
        dealertotal = dealertotal + deal();
        printf("%d\n D", dealertotal);
    }
}

int playerhand()
{
    int playerhand [10];
    int i;
    for(i=0; i<1; i++)
    {
        playerhand[i] = deal();
        printf(" %d\n", playerhand[i]);
    }
}

int hit()
{
    int i;
    int playerhand[10];
    playerhand[i] = deal();
    printf(" %d", playerhand[i]);
}

int main() 
{
    int hold = 1;
    int num;
    int num2;
    int money = 500;
    printf("Welcome to Blackjack! You have $500. Play a hand? 1=y 2=n. ");
    while (hold = 1)
        {
            scanf ("%d",&num);
            if (num == 1)
                {
                    printf("Great! Ante is $20, here's your hand!");
                    playerhand();
                    playerhand();
                    dealerhand();
                    money = money - 20;
                    printf("You have %d dollars. Hit? 1=y 2=n", money);
                    //clearBuffer();
                    scanf("%d", num2);
                    if (num2 = 1)
                    {
                        playerhand();
                        break;
                    }
                    if (num2 = 2)
                    {
                     //   compare();
                    }
                    }
            if (num == 2)
                {
                    printf("Too bad, come back soon!");
                    break;
                }
            if (num != 1 || num != 2)
                {
                printf("Sorry what was that? Play a hand? 1=y 2=n.");
                while((num = getchar()) =! EOF && num != '\n');
                }
        }
}

1 个答案:

答案 0 :(得分:1)

我试图纠正这个但是它有很多问题,请参阅以下来源并尝试纠正您的逻辑错误,这是我能为您做的最好:

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

int deal(void)
{
    int deck[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
    return deck[rand() % 13];
}

void clearBuffer(void)
{
    char ch;

    while ((ch = getchar()) != '\n' && ch != EOF);
}

int dealerhand(void)
{
    int dealerhand[10];
    int dealertotal = 0;

    while (dealertotal < 17) {
        dealertotal = deal();
        printf("%d\n dx", deal());
        dealertotal = dealertotal + deal();
        printf("%d\n D", dealertotal);
    }
    /* RETURN INT ... */
}

int playerhand(void)
{
    int playerhand[10];
    int i;

    for (i = 0; i < 10; i++) {
        playerhand[i] = deal();
        printf(" %d\n", playerhand[i]);
    }
    /* RETURN INT ... */
}

int hit(void)
{
    int i;
    int playerhand[10];

    /* I think you need for here ... */
    for (i = 0; i < 10; i++) {
        playerhand[i] = deal();
        printf(" %d", playerhand[i]);
    }

    /* RETURN INT ... */
}

int main(int argc, char *argv[])
{
    srand((unsigned)time(NULL));

    int hold = 1;
    int num;
    int num2;
    int money = 500;

    printf("Welcome to Blackjack! You have $500. Play a hand? 1=y 2=n. ");

    while (hold == 1) {
        scanf("%d", &num);
            if (num == 1) {
                printf("Great! Ante is $20, here's your hand!");
                playerhand();
                playerhand();
                dealerhand();
                money = money - 20;

                printf("You have %d dollars. Hit? 1=y 2=n", money);
                scanf("%d", &num2);
                if (num2 == 1) {
                    playerhand();
                    break;
                }
                if (num2 == 2);
                    /* compare(); */
            }
            if (num == 2) {
                printf("Too bad, come back soon!");
                break;
            }
            if (num != 1 || num != 2) {
                printf("Sorry what was that? Play a hand? 1=y 2=n.");
                while (((num = getchar()) != EOF) && num != '\n');
            }
    }
}