问题描述
首先,这里是我的code:
First, here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define NUM_SUITS 4
#define NUM_RANKS 13
bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
bool newcard = {false};
int num_cards, rank, suit, totrank;
const char rank_code[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K',};
const char suit_code[] = {'C','D','H','S'};
int main_hand ()
{
suit = rand() % NUM_SUITS;
rank = rand() % NUM_RANKS;
if (!in_hand[suit][rank]) {
in_hand[suit][rank] = true;
num_cards--;
if (suit == 0){
printf("%c of Clubs \n", rank_code[rank]);
}
else if (suit == 1){
printf("%c of Diamonds \n", rank_code[rank]);
}
else if (suit == 2){
printf("%c of Hearts \n", rank_code[rank]);
}
else if (suit == 3){
printf("%c of Spades \n", rank_code[rank]);
}
}
}
int print_hand (suit)
{
}
int totrank_check (totrank)
{
if (totrank > 21) {
printf ("You lose!");
}
else if (totrank == 21) {
printf ("You win!");
}
}
int main()
{
bool stay = {false};
srand((unsigned) time(NULL));
totrank = 0;
num_cards = 2;
printf("Your hand: ");
while (num_cards > 0) {
main_hand();
totrank = totrank + (rank + 1);
}
printf("Total rank: %d\n", totrank);
totrank_check(totrank);
printf("\n");
while (totrank < 24 && stay == false) {
printf("Another card? 1. Yes 0. No\n");
scanf("%d", &newcard);
if(!newcard) {
main_hand();
}
totrank = totrank + (rank + 1);
totrank_check(totrank);
printf("Total rank: %d\n", totrank);
printf("Stay? 1. Yes 0. No\n");
scanf("%d", &stay);
}
return 0;
}
基本上这是一个code,它已仿真和二十一点的手。
它开始,兰特()
选择两个号码,军衔和卡的西装,那是在一个矩阵设置为true,因此它们不能被再次选择相同的组合,然后打印出来。
有对的卡的总排名的检查(因此,如果超过21,你自动丧失),然后你问,如果你想另一张卡,或者你想留下来。
Basically it's a code that "simulates" and hand of blackjack.It starts, rand()
chooses two numbers, the rank and the suit of the cards, that are set as true in a matrix so that they can't be chosen again in the same combination and then printed.There's a check for the total rank of the cards (so that if it exceeds 21 you automatically lose) and then you are asked if you want another card or you want to stay.
下面的错误:如果你选择,你想要另一个卡,这种新卡将是一样的最后一个。
基本上,你会得到黑桃王牌,在和钻石的两个,那么你要另一张牌,你会得到钻石的另外两个。和所述另一个,和另一个。如果删除第二级检查,而你可以看到,基于最后一张牌军衔等级增长。
Here's the error: if you choose that you want another cards, this new card will be the same as the last one.Basically, you get a Ace of Spades, the and Two of Diamonds, then you want another card and you get another Two of Diamonds. And the another, and another. If you remove the rank check in the second while you can see that the rank grows based on the rank of the last card.
之前,用printfs是在 print_hand()
的功能,你可以看到,你总是得到相同的卡,现在我把它们放在 main_hand()
函数,因为我认为这可能是问题(这不是),也因为具有打印一个单独的函数是多余的。但是你可以看到,在技术上,在如果(!in_hand [诉讼] [排名])
的作品,因为,因为该卡是一样的,它没有,如果进入它不打印。
Before, the printfs were in that print_hand()
function, and you could see that you always got the same card, now I moved them in the main_hand()
function because I thought that it might be the problem (it wasn't) and because having a separate function for the print was redundant. But you can see that technically, the if(!in_hand[suit][rank])
works, because, since the card is the same, it doesn't enter the if and it doesn't get printed.
我不知道什么导致了这个问题。任何想法?
I don't know what's causing this problem. Any idea?
请注意我已经使用函数srand((无符号)时间(NULL));
种子兰特()
。
Please note I'm already using srand((unsigned) time(NULL));
to seed rand()
.
推荐答案
scanf函数(%d个,&安培; NEWCARD);
和 scanf函数(%d个,&安培;住宿);
是一个问题,因为变量布尔
,但格式说明是 INT
。在2个地方,像变化:
scanf("%d", &newcard);
and scanf("%d", &stay);
are a problem as the variables are bool
, but the format specifier is for int
. In 2 places, change like:
// scanf("%d", &newcard);
int t;
scanf("%d", &t);
newcard = !!t;
3函数返回 INT
但不返回任何东西。更改为无效的功能。
3 functions return int
but do not return anything. Change to void functions.
// int main_hand() {
void main_hand() {
有似乎是其他逻辑问题了。结果
1)code有时存在不知道,如果韩元/丢失结果
2)上述正确@Jongware评论:
There appears to be other logic issues too.
1) Code sometimes exists without knowing if won/loss
2) @Jongware comment above correct:
最后:如果相同的随机序列仍然来了,带code到裸主函数srand时间的printf兰特
键,看看是否能工程。 时间()
,如果没有工作总是返回-1。结果
或简单添加以下并验证函数srand()
调用不同的值。
Lastly: If the same random sequence is still coming up, strip code to a bare main srand time printf rand
and see if that works. time()
, if not working always returns -1.
Or simple add the following and verify srand()
called with different values.
int main(void) {
unsigned now = (unsigned) time(NULL);
printf("now = %u\n", now);
srand(now);
这篇关于RAND()后首次返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!