GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2003, 09:00
viperv80 viperv80 is offline
New Member
 
Join Date: Dec 2003
Posts: 1
viperv80 is an unknown quantity at this point
Question

Keeping track of a private variable outside the class


Hi,

I am making a Gin Rummy (card game) implementation in C++. I have created the deck of cards, shuffled it and now I need to deal the cards to the players.
Inside the DeckOfCards function I have a private integer variable that is supposed to keep track of the current card in the deck.
I instansiate 4 players and 1 deck in the main(). The problem comes when the deal function of the deckOfCards is called as the currentcard variable is resetted for each player, and thus all the players get the same cards.
Any clue how I could keep track of the currentcard so that it is not reset every time the DeckOfCards class is called?

Thanx for any answers in advance!
  #2  
Old 03-Dec-2003, 17:25
Ian Ian is offline
New Member
 
Join Date: Dec 2003
Posts: 1
Ian is an unknown quantity at this point
Hi Viper,

I am somewhat new to C++ programming but I think I know the solution. If you are using a class then you can define a static data member in the class to keep track of the current card. There will be only one copy

So in your class definition
CPP / C++ / C Code:
class DeckOfCards {

//...
public:
   static int currentCard;
//...

};

Then in the member-function definition section you'd put
int DeckOfCards::currentCard = 0;

or whatever you want to initialize it as. From your problem statement however I suspect there may be other issues because with only 1 deck in Main there should not be multiple copies of the currentcard variable being instantiated.

Hope that helps,
Ian
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 12:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:39.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.