#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIAMONDS 0
#define CLUBS 1
#define HEARTS 2
#define SPADES 3
#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 1
#define COLOR_SIZE 13
#define NR_OF_SUITS 4
#define DECK_SIZE 52
int random_card;
struct Card
{
int suit;
int value;
};
void diamonds(struct Card *cardDeck);
void spades(struct Card *cardDeck);
void clubs(struct Card *cardDeck);
void hearts(struct Card *cardDeck);
void swapCards(struct Card *cardA, struct Card *cardB);
void shuffleCards(struct Card *cardDeck);
void randomize();
int main()
{
srand((unsigned)time(NULL));
struct Card * deck; //Dynamiskt allokerad array
int index;
int suit_index;
deck = (struct Card *)malloc(sizeof(struct Card) * DECK_SIZE);
for (suit_index = 0; suit_index < NR_OF_SUITS; suit_index++) /* Initiera kortleken */
for (index = 0; index < COLOR_SIZE; index++)
{
deck[suit_index*COLOR_SIZE + index].suit = suit_index;
deck[suit_index*COLOR_SIZE + index].value = index;
}
shuffleCards(deck);
randomize();
printf("\n\n");
system("pause");
return 0;
}
void swapCards(struct Card * cardA, struct Card *cardB)
{
struct Card temp;
temp = *cardA;
*cardA = *cardB;
*cardB = temp;
}
void shuffleCards(struct Card *cardDeck)
{
for (int i = 0; i < COLOR_SIZE; i++)
swapCards(&cardDeck[i], &cardDeck[rand() % 13]);
}
void diamonds(struct Card *cardDeck)
{
for (int i = 0; i < 1; i++)
{
switch (cardDeck[i].value + 1)
{
case ACE: printf("Ace ");
break;
case JACK: printf("Jack ");
break;
case QUEEN: printf("Queen");
break;
case KING: printf("King ");
break;
default: printf("%d ", cardDeck[i].value + 1);
break;
}
printf("of Diamonds");
printf("\n");
}
}
void spades(struct Card *cardDeck)
{
for (int i = 0; i < 1; i++)
{
switch (cardDeck[i].value + 1)
{
case ACE: printf("Ace ");
break;
case JACK: printf("Jack ");
break;
case QUEEN: printf("Queen");
break;
case KING: printf("King ");
break;
default: printf("%d ", cardDeck[i].value + 1);
break;
}
printf("of spades");
printf("\n");
}
}
void hearts(struct Card *cardDeck)
{
for (int i = 0; i < 1; i++)
{
switch (cardDeck[i].value + 1)
{
case ACE: printf("Ace ");
break;
case JACK: printf("Jack ");
break;
case QUEEN: printf("Queen");
break;
case KING: printf("King ");
break;
default: printf("%d ", cardDeck[i].value + 1);
break;
}
printf("of hearts");
printf("\n");
}
}
void clubs(struct Card *cardDeck)
{
for (int i = 0; i < 1; i++)
{
switch (cardDeck[i].value + 1)
{
case ACE: printf("Ace ");
break;
case JACK: printf("Jack ");
break;
case QUEEN: printf("Queen");
break;
case KING: printf("King ");
break;
default: printf("%d ", cardDeck[i].value + 1);
break;
}
printf("of clubs");
printf("\n");
}
}
void randomize() {
random_card = rand() % 4 + 1;
if (random_card == 1) {
struct Card c1;
hearts(&c1);
}
if (random_card == 2) {
struct Card c2;
diamonds(&c2);
}
if (random_card == 3) {
struct Card c3;
spades(&c3);
}
if (random_card == 4) {
struct Card c4;
clubs(&c4);
}
}
我得到的输出是心中的-939213(或所选颜色)。我不知道如何解决这个问题,我根本没有解决方案。 我会在这里得到一些帮助,并解决我的问题。而且仅供参考,我不想使用你可能会看到的任何全局变量。
答案 0 :(得分:5)
我看到的问题:
Card
的地址从diamonds
传递给spades
,randomize
等函数。Card
s的套牌。它只是一个Card
。建议解决问题。
deck
从main
传递到randomize
。randomize
。首先更改功能原型:
void randomize(struct Card* deck);
从main
更改来电。
shuffleCards(deck);
randomize(deck);
printf("\n\n");
更改实施。
void randomize(struct Card* deck) {
random_card = rand() % 4 + 1;
if (random_card == 1) {
hearts(deck);
}
if (random_card == 2) {
diamonds(deck);
}
if (random_card == 3) {
spades(deck);
}
if (random_card == 4) {
clubs(deck);
}
}
答案 1 :(得分:1)
您正在初始化的套牌永远不会交给randomize() - 函数。 您正在堆栈上生成新的Card结构并且没有初始化它们,因此它们包含垃圾。 您应该将初始化的套牌传递给randomize-function并使用那里的值,这样可以为您提供所需的行为。
答案 2 :(得分:0)
看起来randomize
函数只是创建随机卡,而不是随机化套牌。
所以你需要的是:
void randomize() {
struct Card c;
random_card = rand() % NR_OF_SUITS;
c.value = rand() % COLOR_SIZE;
switch(random_card) {
case DIAMONDS:
diamonds(&c);
break;
case CLUBS:
clubs(&c);
break;
case HEARTS:
hearts(&c);
break;
case SPADES:
spades(&c);
break;
}
}
我所做的也是随机值,并用switch和常量重写它。它就像一个魅力。
嘿,你为什么要使用for (int i = 0; i < 1; i++)
?这是什么?
但使用它很奇怪。相反,我建议你创建函数printCard
:
void printCard(struct Card *cardDeck) {
switch (cardDeck->value + 1) {
case ACE: printf("Ace");
break;
case JACK: printf("Jack");
break;
case QUEEN: printf("Queen");
break;
case KING: printf("King");
break;
default: printf("%d", cardDeck->value + 1);
break;
}
printf(" of ");
switch (cardDeck->suit) {
case DIAMONDS:
printf("diamonds");
break;
case CLUBS:
printf("clubs");
break;
case HEARTS:
printf("hearts");
break;
case SPADES:
printf("spades");
break;
}
printf("\n");
}
然后randomize
函数将是:
void randomize() {
struct Card c;
c.suit = rand() % NR_OF_SUITS;
c.value = rand() % COLOR_SIZE;
printCard(&c);
}
此外,您的代码在shuffleCards
中存在一些问题(它只会洗掉前13张卡),应该是:
void shuffleCards(struct Card *cardDeck) {
for (int i = 0; i < DECK_SIZE; i++)
swapCards(&cardDeck[i], &cardDeck[rand() % DECK_SIZE]);
}