# card game problem

problem 1: wen i random the cards, there are some repeated card i.e. 2 of hearts appear twice
problem 2: how do i start teh point system

any help would b great

[code]
#include
#include
#include
using namespace std;

int main()
{

const int card = 13;
const int suit = 4;
char *cards[card] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
char suits[suit] = {'S', 'C', 'D', 'H'};

int i, points = 0, total = 0, Card, Suit;

srand(time(NULL));

cout << "Note: S is Spades, C is Clubs, H is Hearts and D is Diamonds

";
//player 1
cout << "

Player 1

";
for (int i = 0; i < 13; i++)
{
Card = rand() % 13;
Suit = rand() % 4;
cout << cards[Card] << " of " << "[" << suits[Suit] << "]" << ", ";
}
cout << "
Total Points: ";
cout << "
-------------------------------------------";

//player 2
cout << "

Player 2

";
for (int i = 0; i < 13; i++)
{
Card = rand() % 13;
Suit = rand() % 4;
cout << cards[Card] << " of " << "[" << suits[Suit] << "]" << ", ";
}
cout << "
Total Points: ";
cout << "
-------------------------------------------";
[/code]

## Comments

• ok first try not to post more than once.. but im over that already

Next you will have repeat cards very often that way..
its not your code its your algorithm....
now .. point system..
i assume you are saying if player 1 has a higher card reward him with a point... ???

i will continue under that assumption...

when i use cards i create a whole deck with a class

[code]
class CARD{
public:
int point_value; // this will determine what card is better
char* face;
}card[52];
[/code]

now using your card class create a function that will loop and fill in the information for each card.
or do it manually...

card[x].point_value = 13; card[x++].face = "ACE Of Spades";

now that we have all the cards completed lets make a shuffle function to
(shuffle) the deck...
now we cant just shuffle the definitions ... so we make a variable called
DECK[52] that we will draw cards from.. i.e. DECK[0] will be the first
card in the deck.. DECK[20] will be the 19th card in the deck..

CARD DECK[52];
simple enough right?

now to shuffle just randomly pull numbers between 0 and 51 and take that card[number] and store it in the deck... then we need to keep track of this so we don't get the same card again. do that by making a variable that monitors the used numbers.

bool used[52];

before using used[52] make sure you clear it all to false

[code]
for(int i = 0; i < 52; i++)
used[i] = false;
[/code]

almost done now just shuffle them up

[code]
CARD DECK[52];
bool used[52];

void shuffle(){
int X, counter = 0;
for(int i = 0; i < 52; i++)
used[i] = false;

while(counter < 52){
X = rand()%52; // pick a card, ANY card
if(used[X]) { /** sorry that card is taken **/ }
else{ /*** WE HAVE A UNUSED CARD! ***/
used[X] = true; // keep track of what one we used
DECK[counter++] = card[X]; // Store card[X] into the DECK[]
}
}
}
[/code]

there you are .. now just give each player the next card in the deck

[code]
// well first you need a player to hand it to
class PLAYER{
public:
CARD crd;
int points;
}player1, player2;

int DECK_counter; // and a counter so you know what card you are grabbing

/********************************************************************/
/* somewhere in your program where the player will recive a card */
/* This is and infinate loop ... put a stop somewhere or you will */
/* go mad at runtime */
/********************************************************************/

while(blah){
if(DECK_counter < 50) {shuffle();} /* a safe place to reshuffle the deck
you can change it to 52 if you want */
player1.crd = DECK[DECK_counter++]; // give player 1 the next card
player2.crd = DECK[DECK_counter++]; // give player 2 the next card

// compare the cards
if(player1.crd.point_value == player2.crd.point_value){
cout << "Player1: " << player1.crd.face << endl;
cout << "Player2: " << player2.crd.face << endl;
cout << "Both players have an equal value card! Oh NOES!

";
// get new card
}else if(player1.crd.point_value < player2.crd.point_value){
cout << "Player1: " << player1.crd.face << endl;
cout << "Player2: " << player2.crd.face << endl;
cout << "[Player2] has the higher card!

";
player2.points++;
}else if(player1.crd.point_value > player2.crd.point_value){
cout << "Player1: " << player1.crd.face << endl;
cout << "Player2: " << player2.crd.face << endl;
cout << "[Player1] has the higher card!

";
player1.points++;
}
}
[/code]

I HOPE THIS HELPS!

sorry i basically re wrote your program for you...
Sign In or Register to comment.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!