我已经把我的套牌洗牌了,然后把它解决了。然而,当我即将让用户在我的程序中输入他/她的输入并获得两张卡片时,我得到相同的卡片,这不是我想要的,我想要两个玩家的不同卡片而且我不希望它们重复。 我看不出我的代码有什么问题,花了几个小时来浏览它,但在我眼里,我觉得应该没问题。
当我调用该函数时:player1();
并从那里调用printcards(deck)
并在这里找到我的问题所在。
你们中的任何人都可以帮助我解决这个问题,所以当我调用我的函数printcards(deck)
时,我会得到不同类型的卡片,而不会重复它们。
下面是我的代码完全看起来如何:(它是我的问题所在的最后一个函数)。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIAMONDS 0
#define CLUBS 1
#define HEARTS 2
#define SPADES 3
#define COLOR_SIZE 13
#define NR_OF_SUITS 4
#define DECK_SIZE 52
#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 1
struct Card
{
int suit;
int value;
int *sum;
};
int test(struct Card *cardDeck);
int player2(struct Card* deck);
int player1(struct Card* deck);
int printCards(struct Card *cardDeck);
void swapCards(struct Card *cardA, struct Card *cardB);
void shuffleCards(struct Card *cardDeck);
int main()
{
srand((unsigned)time(NULL));
//struct Card deck[DECK_SIZE]; //Statiskt allokerad array
struct Card * deck; //Dynamiskt allokerad array
int index;
int suit_index;
int startAt = 0;
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);
printf("\n\n");
startAt = printCards(deck, startAt); //player 1
startAt = printCards(deck, startAt); //player 2
system("pause");
return 0;
}
int printCards(struct Card *cardDeck, int start_index)
{
int sum = 0;
int i = 0;
for (int i = start_index; i < start_index + 2; i++)
{
switch (cardDeck[i].value + 1)
{
case ACE: printf("Ace ");
cardDeck[i].value = 11;
break;
case JACK: printf("Jack ");
cardDeck[i].value = 10;
break;
case QUEEN: printf("Queen");
cardDeck[i].value = 10;
break;
case KING: printf("King ");
cardDeck[i].value = 10;
break;
default: printf("%d ", cardDeck[i].value + 1);
break;
}
printf("of ");
switch (cardDeck[i].suit)
{
case DIAMONDS: printf("Diamonds ");
break;
case HEARTS: printf("Hearts ");
break;
case CLUBS: printf("Clubs ");
break;
case SPADES: printf("Spades ");
break;
default: printf("Something went wrong!! ");
break;
}
printf("\n");
}
return i;
}
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 < DECK_SIZE; i++)
swapCards(&cardDeck[i], &cardDeck[rand() % 52]);
}
答案 0 :(得分:3)
似乎你需要一个新结构
struct CardDeck
{
struct Card cards[52];
int last_Index;
};
这可以保存在你的牌组中,并且不会使用全局变量。
所以: -
重新编写问题代码以便编译。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIAMONDS 0
#define CLUBS 1
#define HEARTS 2
#define SPADES 3
#define COLOR_SIZE 13
#define NR_OF_SUITS 4
#define DECK_SIZE 52
#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 1
struct Card /* <<<< Card needs to be before CardDeck */
{
int suit;
int value;
int *sum;
};
struct CardDeck
{
struct Card cards[52];
int last_Index;
};
int test(struct Card *cardDeck);
int player2(struct Card* deck);
int player1(struct Card* deck);
int printCards(struct CardDeck *cardDeck);
void swapCards(struct Card *cardA, struct Card *cardB);
void shuffleCards(struct Card *cardDeck);
int main()
{
srand((unsigned)time(NULL));
//struct Card deck[DECK_SIZE]; //Statiskt allokerad array
struct CardDeck * deck; //Dynamiskt allokerad array
int index;
int suit_index;
int startAt = 0;
/* Changed deck to a CardDeck */
deck = (struct CardDeck *)malloc(sizeof(struct CardDeck) );
deck->last_Index = 0; /* << ensure the deck is full of cards */
for (suit_index = 0; suit_index < NR_OF_SUITS; suit_index++) /* Initiera kortleken */
for (index = 0; index < COLOR_SIZE; index++)
{
deck->cards[suit_index*COLOR_SIZE + index].suit = suit_index;
deck->cards[suit_index*COLOR_SIZE + index].value = index;
}
shuffleCards(deck->cards); /* shuffleCards can still shuffle the cards array */
printf("\n\n");
system("pause");
return 0;
}
int printCards(struct CardDeck *cardDeck) /* changed to CardDeck */
{
int sum = 0;
int i = 0;
for (int i = cardDeck->last_Index; i < cardDeck->last_Index + 2; i++)
{
switch (cardDeck->cards[i].value + 1)
{
case ACE: printf("Ace ");
cardDeck->cards[i].value = 11;
break;
case JACK: printf("Jack ");
cardDeck->cards[i].value = 10;
break;
case QUEEN: printf("Queen");
cardDeck->cards[i].value = 10;
break;
case KING: printf("King ");
cardDeck->cards[i].value = 10;
break;
default: printf("%d ", cardDeck->cards[i].value + 1);
break;
}
printf("of ");
switch (cardDeck->cards[i].suit)
{
case DIAMONDS: printf("Diamonds ");
break;
case HEARTS: printf("Hearts ");
break;
case CLUBS: printf("Clubs ");
break;
case SPADES: printf("Spades ");
break;
default: printf("Something went wrong!! ");
break;
}
printf("\n");
}
cardDeck->last_Index += 2; /* original post had this increment - ensures each time this is called, 2 cards get used */
return i;
}
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 < DECK_SIZE; i++)
swapCards(&cardDeck[i], &cardDeck[rand() % 52]);
}
答案 1 :(得分:0)
你的代码总是返回相同卡片的原因是因为在两种情况下(Player1和Player2)你都在访问套牌中的相同索引:
for (int i = 0; i < 2; i++){
switch(cardDeck[i].value){
...
你应该存储从牌组中取得的最后一个索引:
int last_index = 0; //Global variable
void printCards(struct Card *cardDeck)
{
int sum = 0;
for (int i = last_index; i < last_index+2; i++)
{
switch (cardDeck[i].value + 1)
...
}
last_index += 2;
}
没有全局变量:
void printCards(struct Card *cardDeck, int start_index)
{
int sum = 0;
for (int i = start_index; i < last_index+2; i++)
{
switch (cardDeck[i].value + 1)
....
}
return i;
}
并称之为:
int startAt = 0;
startAt = printCards(deck, startAt); //player 1
startAt = printCards(deck, startAt); //player 2