gpt4 book ai didi

c - 我的 C Blackjack 程序卡住了

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

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

#define CARD_MAX 52
typedef int Deck[CARD_MAX];
Deck deck;
int cardnumber = 0;
int playerwin = 0, dealerwin = 0, tie = 0;

int game();
void setupDeck(Deck deck);
void shuffleDeck(Deck deck);
void printCard(int card);
int pickCard(Deck deck, int cardnum);
void endProgram();
void playAgain();

main()
{
char yn;

srand(time(NULL));
setupDeck(deck);
shuffleDeck(deck);

again:
printf("Welcome! Do you wanna try your luck in a Blackjack Game? If so type 'y'.\n");
printf("If not type 'n' to close the program: ");
scanf_s("%c", &yn);
getchar();

if (yn == 'y')
game();

else if (yn == 'n')
{
printf("Looks like you aren't feeling lucky today! Comeback tomorrow!\n");
system("pause");
}
else
{
printf("Unknown character! Try again!\n");
goto again;
}


}

int game()
{
int card, cardvalue = 0, cardtype = 0, tcvp = 0, tcvd = 0;
int aceflagp = 0, faceflagp = 0, aceflagd = 0, faceflagd = 0, bjp = 0, bjd = 0;
int loopcounter;
char hitorpass;

system("cls");
printf("Your cards:\n");
for (loopcounter = 0; loopcounter < 2; loopcounter++)
{
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagp++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagp = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;

tcvp += cardvalue;
}
if (aceflagp == 1 && faceflagp == 1)
bjp = 1;

printf("Dealers cards:\n");

card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagd++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagd = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;

tcvd += cardvalue;

card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
if (cardvalue == 1)
{
aceflagd++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagd = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;

tcvd += cardvalue;

if (aceflagd == 1 && faceflagd == 1)
bjd = 1;

if (bjp == 1 && bjd == 1)
{
printf("Both sides has Blackjack! Tie!\n");
tie++;
playAgain();
}
else if (bjp == 1)
{
printf("Player Blackjack!\n");
playerwin++;
playAgain();
}
else if (bjd == 1)
{
printf("Dealer Blackjack!\n");
dealerwin++;
playAgain();
}
printf("Your card total is %d.\n", tcvp);
again:
printf("If you want to hit type 'h'. If you want to pass type 'p'");
scanf_s("%c", &hitorpass);
getchar();

if (hitorpass == 'h')
{
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagp++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagp = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;

tcvp += cardvalue;
if (tcvp > 21)
{
if (aceflagp > 0)
{
tcvp -= 10;
aceflagp--;
printf("Your card total is %d.\n", tcvp);
goto again;
}
else
{
printf("Your hand busted. You lost!\n");
dealerwin++;
playAgain();
}
}
else if (tcvp == 21)
{
printf("You have 21 in your hand can't hit more!\n");
goto dealersturn;
}
else if (tcvp < 21)
{
printf("Your card total is %d.\n", tcvp);
goto again;
}

}
else if (hitorpass == 'p')
{
dealersturn:
printf("Dealers turn:\n");
if (tcvd < 17)
{
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagd++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagd = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;

tcvd += cardvalue;
if (tcvd > 21)
{
if (aceflagd > 0)
{
tcvd -= 10;
aceflagd--;
goto dealersturn;
}
else
{
printf("Dealers has a total of %d. Dealers hand busted!\n", tcvd);
playerwin++;
playAgain();
}
}
else if (tcvd < 17)
goto dealersturn;
else
{
if (tcvp > tcvd)
{
printf("Player has %d, dealer has %d. Player won!", tcvp, tcvd);
playerwin++;
playAgain();
}
else if (tcvp == tcvd)
{
printf("Both player and the dealer has %d. Tie!", tcvd);
tie++;
playAgain();
}
else
{
printf("Player has %d, dealer has %d. Dealer won!", tcvp, tcvd);
dealerwin++;
playAgain();
}
}
}
}
else
{
printf("Unknown character. Try again!");
goto again;
}

system("pause");
}

void endProgram()
{
printf("Thanks for playing my blackjack game! The results are:\n");
printf("Player won %d times!\n", playerwin);
printf("Dealer won %d times!\n", dealerwin);
printf("There have been a tie %d times!\n", tie);
system("pause");
}

void playAgain()
{
char chardummy;
again:
printf("Would you like to play again? Type 'y' for Yes and 'n' for No.\n");
scanf_s("%c", &chardummy);
getchar();
if (chardummy == 'y')
{
if (cardnumber >= 48)
{
printf("There is not enough card left in the deck!");
endProgram();
}
else
game();
}
else if (chardummy == 'n')
endProgram();
else
{
printf("Unknown character. Try again!");
goto again;
}
}

void setupDeck(Deck deck)
{
int i;
for (i = 0; i<CARD_MAX; i++)
{
deck[i] = i;
}
}

void shuffleDeck(Deck deck)
{
for (int i = 52 - 1; i >= 0; --i) {
int r = rand() % (i + 1);
int t = deck[i];
deck[i] = deck[r];
deck[r] = t;
}
}

void printCard(int cardvalue, int cardtype)
{
char cardtype2;


if (cardtype == 0)
cardtype2 = 'C';
else if (cardtype == 1)
cardtype2 = 'D';
else if (cardtype == 2)
cardtype2 = 'H';
else if (cardtype == 3)
cardtype2 = 'S';

switch (cardvalue)
{
case 0:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* K *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
case 1:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* A *\n");
printf("* *\n");
printf("*******\n");
break;
}
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* %2d *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
case 11:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* J *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
case 12:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* Q *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
}
}

int pickCard(Deck deck, int cardnum)
{
if (cardnumber == 52)
{
printf("There is not enough cards left to complete the game!");
endProgram();
}
else
return deck[cardnum];

}

这是我的简单二十一点游戏的代码。由于某种原因,经过几轮经销商轮流不起作用。如果您能找到原因,我将不胜感激,而我找不到原因。请随时提供有关改进我的代码的提示。

最佳答案

您的代码中存在以下问题,我可以从编译器警告/错误中看到这些问题。

  1. printCard 的声明与其用法和实现不匹配。将声明更改为:

    void printCard(int cardvalue, int cardtype);
  2. 明确 main 的返回类型。将其更改为:

    int main () ...
  3. 更改打印 KJQ 的行。在这些行中,您不需要 printf 的附加参数。

    printf("*   K *\n");
    printf("* J *\n");
    printf("* Q *\n");

其中,第一个是最关键的修复。通过这些更改,我能够毫无问题地运行您的程序。我的测试平台:Linux,gcc 4.8.4。

提高警告级别,以便您可以检测此类警告/错误并在将来的编程工作中修复它们。

关于c - 我的 C Blackjack 程序卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522728/

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