Reputation: 61
I'm making a card game and i have to show the card number and the suit, I have to generate a random number, grab that number of cards from the end and put it in the beginning. I tried using this code but it doesnt work.
void split (short * deck){
char a = deck[0];
for (int i = 0; i < 39; i++)
{
deck[i]=deck[i+1];
}
deck[39]=a;
}
void splitmultiple (short * deck){
int k = rand()%40;
for (int i = 0; i < k; i++)
{
split(deck);
}
}
here i'm just taking the last card and puting it in the beginning. the second fuction is just calling the other one k times.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DECK_NSUITS 4
#define DECK_NVALUES 10
#define DECK_SIZE (DECK_NSUITS * DECK_NVALUES)
#define HAND_COUNT 4
#define HAND_SIZE (DECK_SIZE / HAND_COUNT)
#define card_suit(card) ((card >> 8) & 0xff)
#define card_value(card) (card & 0xff)
#define card_compose(suit, value) (((suit & 0xff) << 8) | (value & 0xff))
#define card_swap(card1, card2) do { if (*card1 != *card2) { *card1 ^= *card2; *card2 ^= *card1; *card1 ^= *card2; } } while (0)
#if defined(_WIN32) || defined(__MSDOS__)
#define SPADE "\x06"
#define CLUB "\x05"
#define HEART "\x03"
#define DIAMOND "\x04"
#else
#define SPADE "\xE2\x99\xA0"
#define CLUB "\xE2\x99\xA3"
#define HEART "\xE2\x99\xA5"
#define DIAMOND "\xE2\x99\xA6"
#endif
void deck_build(short * deck)
{
int suit = 0, value = 0;
for (suit = 0; suit < DECK_NSUITS; suit++)
for (value = 0; value < DECK_NVALUES; value++)
{
deck[suit * DECK_NVALUES + value] = card_compose((suit + 1), (value + 1));
}
}
void card_output(short card)
{
switch (card_value(card))
{
case 1 :
printf("A");
break;
case 8 :
printf("Q");
break;
case 9 :
printf("J");
break;
case 10 :
printf("K");
break;
default :
printf("%d", card_value(card));
}
switch (card_suit(card))
{
case 1 :
printf("%s ", HEART); /* Hearts */
break;
case 2 :
printf("%s ", DIAMOND); /* Diamonds */
break;
case 3 :
printf("%s ", CLUB); /* Clubs */
break;
case 4 :
printf("%s ", SPADE); /* Spades */
break;
}
}
void deck_shuffle(short * deck)
{
int i = 0, r = 0;
/* Start randon number genereation */
srand(time(NULL));
for (i = 0; i < DECK_SIZE; i++)
{
r = rand() % (DECK_SIZE - i);
card_swap(&deck[r], &deck[i + r]);
}
}
void split (short * deck){
char a = deck[0];
for (int i = 0; i < 39; i++)
{
deck[i]=deck[i+1];
}
deck[39]=a;
}
void splitmultiple (short * deck){
int k = rand()%40;
for (int i = 0; i < k; i++)
{
split(deck);
}
}
int main()
{
short deck[DECK_SIZE];
short hands[DECK_SIZE];
int i, j;
/* Deck build */
deck_build(deck);
/* Deck destribution to the players hands */
for (i = 0; i < HAND_COUNT; i++)
memcpy(&hands[i * HAND_SIZE], &deck[i * HAND_SIZE], HAND_SIZE * sizeof(short));
/* Hand presentation */
printf("BARALHO:\n");
for (i = 0; i < HAND_COUNT; i++)
{
for (j = 0; j < HAND_SIZE; j++)
{
card_output(hands[i * HAND_SIZE + j]);
}
printf("\n");
}
printf("\n");
/* Deck shuffle */
deck_shuffle(deck);
/* Deck destribution to the players hands */
for (i = 0; i < HAND_COUNT; i++)
memcpy(&hands[i * HAND_SIZE], &deck[i * HAND_SIZE], HAND_SIZE * sizeof(short));
/* Player que baralha */
int num = (rand() % (4)) + 1;
printf("Player%d shuffle:\n", num);
/* Hand presentation after shuffle */
for (i = 0; i < HAND_COUNT; i++)
{
for (j = 0; j < HAND_SIZE; j++)
{
card_output(hands[i * HAND_SIZE + j]);
}
printf("\n");
}
splitmultiple(deck);
printf("\n");
/* Cutting the deck */
if(num == 4)
{
printf("Player1 cut deck:\n");
}
else
{
printf("Player%d cut deck:\n", num+1);
}
for (i = 0; i < HAND_COUNT; i++)
{
for (j = 0; j < HAND_SIZE; j++)
{
card_output(hands[i * HAND_SIZE + j]);
}
printf("\n");
}
printf("\n");
/* dá a hand */
if((num+1) == 4)
{
printf("Player1 da:\n");
}
else if((num+1) == 5)
{
printf("Player2 da:\n");
}
else
{
printf("Player%d da:\n", num + 2);
}
for (i = 0; i < HAND_COUNT; i++)
{
printf("player%d: ", i+1);
for (j = 0; j < HAND_SIZE; j++)
{
card_output(hands[i * HAND_SIZE + j]);
}
printf("\n");
}
printf("\n");
return 0;
}
Upvotes: -1
Views: 60
Reputation: 4836
If I am interpreting what you want for a result regarding card movement (move a card from the end to the beginning), then you will run through your for loop in the opposite manner in the split function.
When I tried out your code with some additional card printouts and temporarily set the number of splits to one, I got the following sample output at the terminal.
Deck shuffle:
A♦ 3♥ 4♥ 6♦ 3♦ 6♥ 3♠ J♠ J♥ J♣
K♥ A♣ 6♠ A♥ 4♦ 7♠ 7♦ Q♠ 5♦ 4♣
2♦ K♦ 4♠ K♠ 2♠ Q♥ 7♣ 5♣ 5♥ K♣
J♦ 3♣ 7♥ Q♦ 2♥ 6♣ 5♠ Q♣ A♠ 2♣
Deck split:
3♥ 4♥ 6♦ 3♦ 6♥ 3♠ J♠ J♥ J♣ K♥
A♣ 6♠ A♥ 4♦ 7♠ 7♦ Q♠ 5♦ 4♣ 2♦
K♦ 4♠ K♠ 2♠ Q♥ 7♣ 5♣ 5♥ K♣ J♦
3♣ 7♥ Q♦ 2♥ 6♣ 5♠ Q♣ A♠ 2♣ A♦
Here the ace of diamonds was moved from the beginning to the end and all of the other cards were moved back one location. From your problem description, I believe you wanted to move the two of clubs to the beginning and move all of the other cards down one position instead.
If that is the case, you would probably want to refactor your function as noted in the following code snippet.
void split (short * deck){
// char a = deck[0];
int a = deck[39];
for (int i = 39; i > 0; i--)
{
deck[i]=deck[i-1];
}
deck[0]=a;
}
This will take the last card in the deck and move it to the first position and move the other cards down as noted in the following test output.
Deck shuffle:
Q♠ A♦ 6♣ 4♥ 7♦ 4♠ 7♠ J♠ K♣ 5♥
A♠ 5♣ K♠ A♣ 4♦ 6♦ 5♠ K♦ J♦ J♣
4♣ 3♠ 7♥ J♥ 2♦ 3♥ 6♥ K♥ Q♥ 2♣
Q♦ 3♦ A♥ Q♣ 7♣ 2♠ 3♣ 2♥ 5♦ 6♠
Deck split:
6♠ Q♠ A♦ 6♣ 4♥ 7♦ 4♠ 7♠ J♠ K♣
5♥ A♠ 5♣ K♠ A♣ 4♦ 6♦ 5♠ K♦ J♦
J♣ 4♣ 3♠ 7♥ J♥ 2♦ 3♥ 6♥ K♥ Q♥
2♣ Q♦ 3♦ A♥ Q♣ 7♣ 2♠ 3♣ 2♥ 5♦
Here, the six of spades was the last card which was then moved to the beginning. FYI, as a side note, the definition of work variable "a" was changed from a character to an integer; otherwise, the suit value gets lost in the execution of the value/suit macro shift.
Give that a try to see if it meets the spirit of your project.
Upvotes: 0