Hello all ,
I am just new to programming! I want to store the total number(deck.value) of several players evth having loop(players depend on user how many want).I tried many ways for that like vector and array.I dont know why I couldnt!Another thing,if user wants more cards,I want to give cards from old deck coz i dont want duplicates. anybody help me to solve out! :sad:


cout<<"how many player u want"<<endl;
	cin>>n;
	for ( i = 0; i < 3*n; ) {
	int urtotal=0;
	cout << " player" << n<< '\n';
		for ( int j = 0; i < 56 && j < 3; i++, j++ ){
		cout << deck[i].suit << deck[i].face <<" ";
		urtotal+=deck[i].value;
		}
		cout<<urtotal;
		/*cout<<"want more"<<endl;
		cin>>cond;
		while(cond=='y'){
		urtotal	=urtotal+rand()%1;
		}
		if(urtotal>42)
		urtotal=urtotal-30;
		else if(urtotal>31&&urtotal<42)
		urtotal=urtotal-20;
		else if(urtotal>21&&urtotal<32)
		urtotal=urtotal-10;
		cout<<urtotal;*/
		}

:sad:

This is an endless loop:

cin>>cond;
while(cond=='y'){
        urtotal    =urtotal+rand()%1;
}

You should have some sort of cond assignment inside the while() loop.

I'm somewhat confused by your code; partly due to the fact that I basically know nothing about the card game you are trying to implement. Also, I fail to see why you use 3*n in loop. Don't you just want to loop once through each player? Here's what I think you should do:

Stack player_stack[number_players];
 
for (int i=0; i < number_players; i++)
{
 
   player_stack[i].add(Deck.grab_random_card()); // whatever, do your stuff here
   // ...
}

You could even have a static member inside Stack that holds the number of players, so you could do something like this:

Stack::number_players = number_players;
Stack player_stack[number_players];

Just a thought.

I'd consider developing another class to represent a Player and Game. Maybe something conceptually along these lines:

Player
  Card hand[5];
  int numCards;
  int handValue;
  int runningTotal;
  valueHand();

Game
  vector<Player> players;
  int numHandsPlayed;
  housePays();
  determineWinner();

I'd consider developing another class to represent a Player and Game. Maybe something conceptually along these lines

Definitely. Of course, it's probably better to declare hand as Stack, because that class has already been implemented, and has such functions as add(), remove(), so you would have more flexibility with that than a static array. Or you could make it a Card vector, in which case you could use the built-in functions for adding and removing cards.

Thank a lot for helping me! I have one more Q..how can i get back to the old deck from that? coz each player ll get 3 cards first ,but if they want more I want to go back to old deck ,no duplicates.
Sorry my level is just a beginner ! I dont eth really..:-|

#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <string>

struct {
char suit;
std::string face;
int value;
}deck[52];

struct {
 const char *face;
 int value;
}
cardValues[] = {
  {"2", 2},{"3", 3},{"4", 4},
  {"5", 5},{"6", 6},{"7", 7},
  {"8", 8},{"9", 9},{"10", 10},
  {"J", 13},{"Q", 12},{"K", 11},
  {"A", 1}
};

const char cardSuits[] = {'D','C','H','S'};

/*Player
  Card hand[5];
  int numCards;
  int handValue;
  int runningTotal;
  valueHand();

Game
  vector<Player> players;
  int numHandsPlayed;
  housePays();
  determineWinner();*/

int main()
{
using namespace std;
int ur=0,number_players=0,i=0;
//char cond;

  // Build the deck
for ( int suit = 0; suit < 4; suit++ ){
    for( int face = 0; face < 14; face++ ){
      deck[i].face = cardValues[face].face;
      deck[i].value = cardValues[face].value;
      deck[i].suit = cardSuits[suit];
      ++i;
    }
  }

	// Shuffle the deck
  srand ( (unsigned int)time(0));
  random_shuffle( deck, deck + 52 );


 // Pop off hands of at most 5 until the deck is empty
  cout<<"how many player u want"<<endl;
  cin>>number_players;
Stack::number_players = number_players;
Stack player_stack[number_players];
for (int i=0; i < number_players; i++)
{
	for ( i = 0; i < 3 ) {
	int urtotal=0;
	cout << " player" << n<< '\n';
		for ( int j = 0; i < 56 && j < 3; i++, j++ ){
		cout << deck[i].suit << deck[i].face <<" ";
		urtotal+=deck[i].value;
		}
		cout<<urtotal;
		/*cout<<"want more"<<endl;
		cin>>cond;
		while(cond=='y'){//cond ,if want more back .
		urtotal	=urtotal+rand()%1;//adding next number
		}
		if(urtotal>42)//sth test for total
		urtotal=urtotal-30;
		else if(urtotal>31&&urtotal<42)
		urtotal=urtotal-20;
		else if(urtotal>21&&urtotal<32)
		urtotal=urtotal-10;
		cout<<urtotal;*/
		}
}
	
  return 0;
}

Hmm... since a Deck is really an advanced Stack, I say you should derive Deck from Stack. It's not really required or anything, but it makes the 2 types more compatible.

Secondly, I think you should add the following member functions to Stack (also to Deck, assuming that Deck is derived from Stack):

  • bool Stack::add_card(Card)
  • Card Stack::remove_card(int card_number)

Deck shouldn't really be an array, it should be a class that has its own sub-array that holds all the cards. It should then have a member function like Deck::shuffle().

You should have a class named Card, whose only purpose is to hold some static enums so your cards can have names:

Card::Clubs::Jack

Why? Let me show you how easy it would be to manage your card game with this implementation:

// btw, you should add a constructor which automatically fills in the cards
Deck deck;

// same as usual
Stack playercards[number_players];

// shuffling the deck
deck.shuffle();

// adding a card
playercards[0].add_card(Card::Clubs::Jack);

// removing a card
playercards[0].remove_card(3);

// dealing a card from the deck to the first player
playercards[0].add_card( deck.remove_card(51) );

I hope you understand what I'm trying to say. Take special notice of the last line; it uses the both techniques so that the deck gets cards removed, returns the removed card, which is then added to a player's stack. Hopefully this will simplify your code a bit.

how can i get back to the old deck from that?

If you want to keep a copy of the old deck, simply add a copy constructor, which copies all the data from one object to another. Something like this you might want for Deck:

Deck::Deck(Deck &new_deck) {

// eg.
new_deck.number_cards = number_cards;

// ...
}

Then to make a copy of the deck, all you have to do is like this:

Deck original_deck(deck);

See how easy that was?

so sorry... u might be too upset with me! sorry again!
I try my best upon wh u pointed out to me!
Thanks a million!:mrgreen:

so sorry... u might be too upset with me! sorry again!
I try my best upon wh u pointed out to me!

You have nothing to worry about. You're certainly not breaking any rules and you're showing effort like it describes in the "homework policy". And it's actually fun helping someone in a programming forum who's actually trying... Hey, we were all newbies once!

I had the same problem with a VB6 blackjack game, used classes in the end which wasnt very practical.

>>..how can i get back to the old deck from that? coz each player ll get 3 cards first ,but if they want more I want to go back to old deck ,no duplicates.

Each game has only one deck (or a finite number of decks). If you use a stack to represent the deck then each card will be dealt from the top and when it is dealt it will be removed from the stack so when you go back there can't be any duplicates. It is possible to replicate this functionality by using an array and an index integer to keep track of where you are in the deck. Then, if you feel the top of the deck is the highest integer representing a valid element within the deck, then count backward and if you feel the top of the deck is the lowest integer representing a valid element within the deck then count up. As long as you never reverse course within the scope of any given hand, it works fine.

declare deck
determine number of players
start outer loop
shuffle deck
start inner loop number 1 
for each player
  deal three cards from top of deck
end loop number 1
start inner loop number 2
for each player 
  allow each player to take 0, 1, or 2 more cards from the top of the deck 
end inner loop number 2
determine winner(s) of hand
end outer loop
This article has been dead for over six months. Start a new discussion instead.