我不知道如何在我的二十一点游戏中添加整数并打印它们,C
I don't know how to add integers and print them in my blackjack game, C
我的 21 点游戏快玩完了,但我在添加纸牌整数时遇到了问题。如您所见,我打印了卡片并显示了第一张卡片的价值,第二张价值是两张卡片的总和。我只想打印总和,但出于某种原因我无法摆脱第一张卡片的价值。庄家没有得到他的牌,为什么?
我遇到的第二个问题是,当其中一名玩家获得一张新牌时,我希望将新牌值添加到该玩家的第一笔总和中。
void deal(const Card * const Deck, int value, int size, int size_1, int size_2){
int i, j, length;
char anotherCard[2];
char name1[30];
char name2[30];
int valueName1 = 0, valueName2 = 0, valueDealer = 0;
printf("Name player one > ");
scanf("%s", name1);
printf("Name player two > ");
scanf("%s", name2);
printf("\nWelcome %s and %s, lets begin!\n\n", name1, name2);
getchar();
printf("%s's card:\n", name1);
for (i = 0; i < size; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
valueName1 += Deck[i].value;
printf("\nvalue > %d", valueName1);
}
//Name2 deleted
//Dealer deleted
printf("\nDo %s want another card? Y/N > ", name1);
fgets(anotherCard, sizeof(anotherCard), stdin);
fflush(stdin);
printf("\n");
length = strlen(anotherCard);
for (j = 0; j < length; j++){
if (strcmp(anotherCard, "Y") == 0 || strcmp(anotherCard, "y") == 0){
for (i = 6; i < 7; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
valueName1 = Deck[i].value;
printf("value > %d", valueName1);
}
}
else
break;
}
return;
}
如果您只想打印最终总数,请将 printf
行移到循环之外。您还应该以 \n
开始每张卡片的 printf
,这样卡片将显示在不同的行上。
printf("%s's card:\n", name1);
for (i = 0; i < size; i++){
printf("\n%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
valueName1 += Deck[i].value;
}
printf("\nvalue > %d", valueName1);
我的 21 点游戏快玩完了,但我在添加纸牌整数时遇到了问题。如您所见,我打印了卡片并显示了第一张卡片的价值,第二张价值是两张卡片的总和。我只想打印总和,但出于某种原因我无法摆脱第一张卡片的价值。庄家没有得到他的牌,为什么?
我遇到的第二个问题是,当其中一名玩家获得一张新牌时,我希望将新牌值添加到该玩家的第一笔总和中。
void deal(const Card * const Deck, int value, int size, int size_1, int size_2){
int i, j, length;
char anotherCard[2];
char name1[30];
char name2[30];
int valueName1 = 0, valueName2 = 0, valueDealer = 0;
printf("Name player one > ");
scanf("%s", name1);
printf("Name player two > ");
scanf("%s", name2);
printf("\nWelcome %s and %s, lets begin!\n\n", name1, name2);
getchar();
printf("%s's card:\n", name1);
for (i = 0; i < size; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
valueName1 += Deck[i].value;
printf("\nvalue > %d", valueName1);
}
//Name2 deleted
//Dealer deleted
printf("\nDo %s want another card? Y/N > ", name1);
fgets(anotherCard, sizeof(anotherCard), stdin);
fflush(stdin);
printf("\n");
length = strlen(anotherCard);
for (j = 0; j < length; j++){
if (strcmp(anotherCard, "Y") == 0 || strcmp(anotherCard, "y") == 0){
for (i = 6; i < 7; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
valueName1 = Deck[i].value;
printf("value > %d", valueName1);
}
}
else
break;
}
return;
}
如果您只想打印最终总数,请将 printf
行移到循环之外。您还应该以 \n
开始每张卡片的 printf
,这样卡片将显示在不同的行上。
printf("%s's card:\n", name1);
for (i = 0; i < size; i++){
printf("\n%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
valueName1 += Deck[i].value;
}
printf("\nvalue > %d", valueName1);