3 Replies - 781 Views - Last Post: 24 September 2008 - 08:13 AM Rate Topic: -----

#1 flamingpenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-September 08

Need help removing random integers for a card game program

Posted 24 September 2008 - 05:54 AM

Hey guys, I'm working on this card game program for my class. I have to generate random integers for 2 5-card hands for poker. The only problem is that I still have duplicates in my output. My code is as follows:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


int num_card(int value_par);
int suit_card(int value_par);
int main () {
	int  value, value1=0, value2=0, value3=0, value4=0, value5=0, value6=0, value7=0, value8=0, value9=0, value10=0, count=1, seed=0, numval, shape;

	
	seed= time(0);
	srand(seed);
	
	cout << "First Hand:" << endl;
	
	while (count <6) {
			value = rand()%52;
			value+= 1;
		
			switch (count) {
				case 1:
					value1= value;
					break;
				case 2:
					value2= value;
					if (value2=value1){
						value= rand()%52+1;
					value2= value;}
					break;
				case 3:
					value3= value;
					if (value3= value2 || value1){
						value= rand()%52+1;
					value3= value;}
					
					break;
				case 4: 
					value4= value;
					if (value4= value3 || value2 || value1){
						value= rand()%52+1;
					value4= value;}
						break;
				case 5:
					value5= value;
					if (value5= value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value5= value;}
					break;
				case 6:
					value6= value;
					if (value6= value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value6= value;}
					break;
				case 7:
					value7= value;
					if (value7= value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value7= value;}
					break;
				case 8:
					value8= value;
					if (value8= value7 || value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value8= value;}
					break;
				case 9:
					value9= value;
					if (value9= value8 || value7 || value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value9= value;}
					break;
				case 10: 
					value10= value;
					if (value10= value9 || value8 || value7 || value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value10= value;}
					break;
			}
			

				
	

			numval = num_card(value);
				

			shape = suit_card(value);
			switch (shape) {
				case 1: 
					cout<< numval << " of Spades" << endl;
					break;
				case 2:
					cout<< numval << " of Hearts" << endl;
					break;
				case 3:
					cout<< numval << " of Diamonds" << endl;
					break;
				case 4:
					cout<< numval << " of Clubs" << endl;
					break;
			}

				
			count++;
			
	}
	cout<< "Second Hand:"<< endl;

	count=1;
	while (count <6) {
			value = rand()%52;
			value+= 1;
		
			switch (count) {
				case 1:
					value1= value;
					break;
				case 2:
					value2= value;
					if (value2=value1){
						value= rand()%52+1;
					value2= value;}
					break;
				case 3:
					value3= value;
					if (value3= value2 || value1){
						value= rand()%52+1;
					value3= value;}
					
					break;
				case 4: 
					value4= value;
					if (value4= value3 || value2 || value1){
						value= rand()%52+1;
					value4= value;}
						break;
				case 5:
					value5= value;
					if (value5= value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value5= value;}
					break;
				case 6:
					value6= value;
					if (value6= value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value6= value;}
					break;
				case 7:
					value7= value;
					if (value7= value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value7= value;}
					break;
				case 8:
					value8= value;
					if (value8= value7 || value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value8= value;}
					break;
				case 9:
					value9= value;
					if (value9= value8 || value7 || value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value9= value;}
					break;
				case 10: 
					value10= value;
					if (value10= value9 || value8 || value7 || value6 || value5 || value4 || value3 || value2 || value1){
						value= rand()%52+1;
					value10= value;}
					break;
			}
			

				
	

			numval = num_card(value);
				

			shape = suit_card(value);
			switch (shape) {
				case 1: 
					cout<< numval << " of Spades" << endl;
					break;
				case 2:
					cout<< numval << " of Hearts" << endl;
					break;
				case 3:
					cout<< numval << " of Diamonds" << endl;
					break;
				case 4:
					cout<< numval << " of Clubs" << endl;
					break;
			}

				
			count++;
			
	}
				
			
		

		return 0;

}
int num_card(int value_par) {
	int num;

	num=value_par%13+1;

	return num;
		
}		
int suit_card(int value_par) {
	int suit;

	
	suit= value_par%4+1;

	 
			

	return suit;

}

	
	


	


Any help would be greatly appreciated! Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help removing random integers for a card game program

#2 DoubleFission  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 223
  • Joined: 20-September 08

Re: Need help removing random integers for a card game program

Posted 24 September 2008 - 06:45 AM

We had to do one of these last semester:

We expressed each card as an integer which we would then manipulate as we were printing:

ie:

card 1 - 12 = Spades
card 13 - 25 = Clubs
card 26 - 38 = Hearts
cards 39 - 52 = Diamonds

((Note these may be wrong, It's getting late and I can't be bothered digging up my old files on this))

so the easiest way to express that is a switch statement

switch(card/13)
{
case 0:
		  cout <<" of Spades" << endl;
		  break;
case 1:
		 cout << " of Clubs" << endl;
		 break;
case 2:
		 cout << " of Hearts" << endl;
		 break;
case 3:
		 cout << " of Diamonds" << endl;
		 break;
}



And the card number/value would be expressed similarly but using modulus (%) instead

But we would first create a 'deck' of cards which was just an array 52 long with the numbers 1 - 52 in it.

int deck[52];

for(int i=0; i<52; i++)
{
deck[i] = i+1;
}



Then you have to shuffle this deck of cards;

To do this you have to:
- Go through each element of the array of cards
- generate a random number between 0 and 51
- Swap the cards in those array positions

One sweep of the entire deck should be enough to do that.

Once you have your cards in a random order, you can just deal them out.

ie.
hand1[0] = deck[0];
hand1[1] = deck[2];
...
hand2[4] = deck[9];



Then print them out using the switch statement style I used above...

~~~

That was a basic outline of how to do it, and I have only given you a couple of (not very good) lines of code

Hope that gives you an idea on how to do it... And hopefully given you an insight into designing good programs
Was This Post Helpful? 0
  • +
  • -

#3 F!st!cuffs  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 153
  • Joined: 15-July 08

Re: Need help removing random integers for a card game program

Posted 24 September 2008 - 06:51 AM

If you want to remove a card after it is delt you'll probably have to use an array or a list. Your best bet would probably be to use a data structure with an integer for the card number then maybe a string for the suit. You could populate the list or array then shuffle it up then just deal straight off the top.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Need help removing random integers for a card game program

Posted 24 September 2008 - 08:13 AM

Try my snippet:
http://www.dreaminco...snippet2075.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1