Click here to Skip to main content
15,883,535 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How do I give the player to choose the random card?

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

char CARD[52][128] = {
"AH","AC","AS","AD","2H","2C","2S","2D",
"3H","3C","3S","3D","4H","4C","4S","4D",
"5H","5C","5S","5D","6H","6C","6S","6D",
"7H","7C","7S","7D","8H","8C","8S","8D",
"9H","9C","9S","9D","TH","TC","TS","TD",
"JH","JC","JS","JD","QH","QC","QS","QD",
"KH","KC","KS","KD"
};

char DECK[52][128];
char HAND1[52][128];
char HAND2[52][128];

int player1Points = 0;
int player2Points = 0;
int difference = 0;
Posted
Updated 8-Jun-22 14:52pm
v11
Comments
Richard MacCutchan 8-Jun-22 7:15am    
Please do not delete the details from your question after it has received suggestions or answers.
merano99 9-Jun-22 14:39pm    
You write: "some code I haven't learn yet so my homework not allowed me to use it."
Although the question is tagged C++, this is currently pure C, not C++. Is a C++ solution possible? What exactly are the permitted means, what are the restrictions on homework ?

Something else:
The fact that large parts of the specifications were exchanged after there were already proposed solutions is really unpleasant. It would be better to add more info, or leave the original question and create a new one.

I noticed you have many occurrences of several literal strings. That is not a good practice. It is much better to define constants for them. Something like :
using PCSTR = const char *;
PCSTR E4749 = "E4749 Dark-X Nepstrius";
PCSTR F3965 = "F3965 Cyclone Belfyre";
... and so on.
This will provide (at least) two advantages for you : they are all in one place so if you ever change them it will be much easier and it will shrink your logic quite a bit so it will be easier to understand.

Another advantage is you could assign rankings to them by making arrays listing them with the higher ranks being the ones that are earlier in the list.

I think this is what you are after but I am not sure. You stated you have two characteristics - Type and System. What is not clear is do you have two kinds of cards or just one? Either way, to get a random value first seed your Pseudo-Random Number Generator (PRNG) and then request a random value. I will assume you have seven cards for your System values. That means they have values ranging from 0 to 6. To get a random value in a range I make a function that looks like this :
C++
int GetRandomInt( int minval, int maxval )
{
    int range = maxval - minval + 1;
    int value = minval + ( rand() % range );
    return value;
}

// to call it :

   srand( time( NULL ) % RAND_MAX );    // do this ONLY once !!!

   const int players = 2;
   int cards[ players ] = { -1 };

   for( int n = 0; n < players; ++n )
   {
      cards[ n ] = GetRandomInt( 0, 6 );
   }
If you have more card types with more values then adjust the calling parameters to GetRandomInt to reflect that.
 
Share this answer
 
C++
// char CARD[52][128] = {..}
// That doesn't look right.
// 1. Why a fixed length of 128 ?
// 2. Here are 52 cards that have different names
// 3. If the names should not change, they are const

#define MAXCARDS 52
char* const CARD[MAXCARDS] = {
   "AH","AC","AS","AD","2H","2C","2S","2D",
   "3H","3C","3S","3D","4H","4C","4S","4D",
   "5H","5C","5S","5D","6H","6C","6S","6D",
   "7H","7C","7S","7D","8H","8C","8S","8D",
   "9H","9C","9S","9D","TH","TC","TS","TD",
   "JH","JC","JS","JD","QH","QC","QS","QD",
   "KH","KC","KS","KD" };

The main program could start like this
C++
int main()
{
   // Do not capitalize variables, only constants
   // and better not global, but local
   char* deck[MAXCARDS];
   char* hand[2][MAXCARDS];

   srand(time(NULL) % RAND_MAX);    // do this ONLY once !!!

   // copy all to deck
   for (int i = 0; i < MAXCARDS; i++) {
      deck[i] = CARD[i];
   }

   // shuffle cards
   shufflecards(deck);

...

Suggestion:
C++
// Take Rick York's GetRandomInt() function by any two
// select cards in the deck and swap them.You do that a lot
// many times in a row to mix well.
void shufflecards(char* deck[MAXCARDS]);

//edit:
Based on Ricks second solution you can write a GenerateDeck Funktion witch combines copy and shuffle.
C++
void GenerateDeck(char* deck[])
{
	// int missfit = 0;
	for (int n=0; n<MAXCARDS; ++n) {
		for(;;) {                // loop until a new card is found
			int i = GetRandomInt(0, MAXCARDS - 1);
			char* card = CARD[i];
			if (!FindCard(deck, card, n)) {
				deck[n] = card;
				break;           // found a new card
			} 
			else {
				// missfit++;
			}
		}
	}
	// printf("missfits: %d\n", missfit);
};

Result:
missfits: 142, 78, 137, 221

5D JS 7D 8D 8S QS 3D TC
3H 4D 2D JH 7S 6C 2C 9D
TH QC 2S KH TS 9C 5S JC
7H TD 8C JD KS 6S QH AS
KD 4S AC 3S 4C 5H KC AD
4H 3C 6D 9H 6H 8H AH 5C
2H QD 9S 7C

However, there are sometimes a lot of failed attempts until the deck is filled.
 
Share this answer
 
v2
I can't recall ever adding two solutions to a question. Regardless, here is number two. As merano99 mentioned, I don't know what's up with the arrays of 128. Anyway, you have lots of literal values again. That is a really, really bad habit to get into. Use defined constants instead.

Here's another way to get a random deck of cards. I would use an array of integers to represent the deck. It's important to never repeat a card in a deck so I would check for them. Here's one way to do that.
C++
bool FindCard( int searchCard, int deck[], int count )
{
    bool found = false;
    for( int n = 0; n < count; ++n )
    {
        if( deck[ n ] == searchCard )
        {
            found = true;
            break;
        }
    }
    return found;
}


void ShuffleCards( int deck[] )
{
    // This assumes the PRNG is already seeded, as shown previously.

    for( int n = 0; n < MAXCARDS; ++n )
    {
        bool gotOne = false;
        while( ! gotOne )         // loop until a new card is found
        {
            int card = GetRandomInt( 0, MAXCARDS-1 );
            if(  ! FindCard( card, deck, n ) )
            {
                deck[ n ] = card;
                gotOne = true;    // found a new card
            }
        }
    }
};
 
Share this answer
 
I have only a vague idea of what you're talking about. But my general observation would be that it looks like you're explicitly coding lots of scenarios, so you need to find a far more general way to do this. Try to assign a profile to each card (its identifier, name, and various standard characteristics) so that you can determine the outcome of a head-to-head battle by comparing the standard characteristics, not the specific cards.
 
Share this answer
 
Comments
The Kings 8-Jun-22 0:13am    
Thank you for your reply. yes, I'm coding 10 card with a lot of scenarios but I'm just a beginner. so I'm very blur how to code it seems the knowledge still can't support me to code better, although I'm already find a lot of information about this type of situation. But some code I haven't learn yet so my homework not allowed me to use it.
Greg Utas 8-Jun-22 17:27pm    
Given that you have deleted your original question and entered a completely new one, my comment no longer makes sense. Do you realize how rude that is?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900