Posted: Sat Jan 12, 2008 2:36 pm Post subject: Question Regarding a Card Combinations Program
Hello, I am a student studying first-year Java in high-school, and I was just wondering what would be the easiest solution to the following problem:
Definition of a Card:
Values {2-11}
Suits {5 Suits}
50 Cards in Total
I need to figure out (for a program for another class I am making) how to go through all possible 4 card VALUE combinations (suit is irrelevant) (order matters, hence if the values in a hand are 1,2,3,5 then the hand 5,3,1,2 would be identical, and should not be counted if 1,2,3,5 has already been counted).
The program I have right now (which is obviously incorrect) is:
Quote:
int[] values = new int [50];
//I know I can make the following code more efficient, but
//efficiency is not important for this program, I just need help on the next part
for (int value = 2 ; value < 12 ; value++)
{
values [value - 2] = value;
}
for (int value = 2 ; value < 12 ; value++)
{
values [value + 8] = value;
}
for (int value = 2 ; value < 12 ; value++)
{
values [value + 18] = value;
}
for (int value = 2 ; value < 12 ; value++)
{
values [value + 28] = value;
}
for (int value = 2 ; value < 12 ; value++)
{
values [value + 38] = value;
}
//Go through all possible value combinations
for (int first = 0 ; first < 50 ; first++)
{
for (int second = second + 1 ; second < 50 ; second++)
{
for (int third = second + 1 ; third < 50 ; third++)
{
for (int fourth = third + 1 ; fourth < 50 ; fourth++)
{
System.out.print (values [fourth]);
System.out.print (" ");
System.out.print (values [second]);
System.out.print (" ");
System.out.print (values [third]);
System.out.print (" ");
System.out.println (values [fourth]);
}
}
}
}
The problem is some hands are found multiple times .
Anxiously Waiting for Help,
Dan
Sponsor Sponsor
Tony
Posted: Sat Jan 12, 2008 5:10 pm Post subject: RE:Question Regarding a Card Combinations Program
I think you might have some typos in there...
code:
for (int second = second + 1 ; second < 50 ; second++)
There are different ways of preventing the same combinations from appearing. The least efficient, but probably the simplest to understand, method is to sort each hand and check if such order has previously appeared.
Posted: Sat Jan 12, 2008 9:16 pm Post subject: Re: Question Regarding a Card Combinations Program
You could simply ignore the first part of your code and the idea of 50 cards entirely. Make your for loops generate each value combination that is in sorted order. So value(card1) <= value(card2) <= value(card3) <= value(card4). We know that for each value we have enough cards for this to work.
_Dan_
Posted: Sat Jan 12, 2008 11:34 pm Post subject: Re: Question Regarding a Card Combinations Program
Quote:
method is to sort each hand and check if such order has previously appeared.
That's wayyy too inefficient, isn't it? I think the program would run for like a day :S, there are >300 million for loop runs, and each would have to check all of the hands before it.
Quote:
So value(card1) <= value(card2) <= value(card3) <= value(card4). We know that for each value we have enough cards for this to work.
I thought of that before, however the problem is that there are more than 4 suits, hence only four of the 5 2's would be placed in the first few hands (it should be 2,2,2,2 then 2,2,2,2 then 2,2,2,2 then 2,2,2,2 then 2,2,2,2 but the last hand will not appear using this method if I understood you correctly).
OneOffDriveByPoster
Posted: Sun Jan 13, 2008 12:06 am Post subject: Re: Question Regarding a Card Combinations Program
Quote:
I thought of that before, however the problem is that there are more than 4 suits, hence only four of the 5 2's would be placed in the first few hands (it should be 2,2,2,2 then 2,2,2,2 then 2,2,2,2 then 2,2,2,2 then 2,2,2,2 but the last hand will not appear using this method if I understood you correctly).
I thought you said suit does not matter. 2,2,2,2 should be listed only once, no?
Tony
Posted: Sun Jan 13, 2008 12:07 am Post subject: Re: Question Regarding a Card Combinations Program
_Dan_ @ Sat Jan 12, 2008 11:34 pm wrote:
That's wayyy too inefficient, isn't it?
"sorting" can be implemented via O(1) encoding
binary tree lookup is O(log n)
alternatively you could find everything, with repeats. Then sort that list O(nlogn) and remove any repeating elements O(n)
Posted: Sun Jan 13, 2008 12:12 am Post subject: Re: Question Regarding a Card Combinations Program
Wait, firstly I have to figure out how to even GET at least every hand in the array, and my current way does not do this, and secondly, how do I know whether or not to remove a hand from the array of hands (for example there are 5 (2,2,2,2) hands, and 100(2,2,3,3) hands)?
Quote:
I thought you said suit does not matter. 2,2,2,2 should be listed only once, no?
I meant that I did not need to store the suits of the cards, however there are still 5 different "types" of each card.
OneOffDriveByPoster
Posted: Sun Jan 13, 2008 1:33 am Post subject: Re: Question Regarding a Card Combinations Program
_Dan_ @ Sun Jan 13, 2008 12:12 am wrote:
I meant that I did not need to store the suits of the cards, however there are still 5 different "types" of each card.
Note about sorting: you do not really have to store all the hands to sort them. Consider an array 10x10x10x10 (which is quite reasonable) of counters. You generate a hand and then you can simply increment the associated counter (based on the card values as index values). You can go with your original idea for having the array of 50 cards--sort by value then by suit (you are currently sorting by suit then by value afaik).
Sponsor Sponsor
_Dan_
Posted: Sun Jan 13, 2008 1:41 am Post subject: Re: Question Regarding a Card Combinations Program
4 Dimensional Arrays :S? You lost me there, but I understand the incrementing part (though I still don't understand how I would use for loops to find each hand).
Could someone perhaps give me the code (with comments so I can understand how it works)?
OneOffDriveByPoster
Posted: Sun Jan 13, 2008 1:53 am Post subject: Re: Question Regarding a Card Combinations Program
_Dan_ @ Sun Jan 13, 2008 1:41 am wrote:
4 Dimensional Arrays :S? You lost me there, but I understand the incrementing part (though I still don't understand how I would use for loops to find each hand).
Your original code (if you fix the typos/whatever makes it slightly off) can find each hand with values in sorted order if you sort set values[] to have 2222233333...BBBBB instead of 23456789AB234... The four dimensional array has a counter for the hand 2222 at a[0][0][0][0] for example. Of course you can use a Map (but some people may not approve).
_Dan_
Posted: Sun Jan 13, 2008 2:02 am Post subject: Re: Question Regarding a Card Combinations Program
Ok I've got everything working, the only problem is that it takes too long to run, even if I only do one Poker-Hand type comparison (i.e. is every hand a flush?). Here's the code I have now:
/** The "Check" class.
* Purpose:
* @Author
* @Last Updated:
*/
public class Check
{
public static boolean isPrime (int value)
{
if (value < 4)
{
return true;
}
int sqrtOfValue = (int) Math.ceil (Math.sqrt (value));
for (int prime = 2 ; prime <= sqrtOfValue ; prime++)
{
if (value % prime == 0)
{
return false;
}
}
return true;
}
public static boolean allDivideC (int fCard, int sCard, int tCard, int foCard, int fiCard, int siCard, int seCard)
{
int values[] = {fCard, sCard, tCard, foCard, fiCard, siCard, seCard};
for (int f = 0 ; f < 7 ; f++)
{
for (int s = f + 1 ; s < 7 ; s++)
{
for (int t = s + 1 ; t < 7 ; t++)
{
for (int fo = t + 1 ; fo < 7 ; fo++)
{
int largest = values [f];
if (values [s] > largest)
largest = values [s];
if (values [t] > largest)
largest = values [t];
if (values [fo] > largest)
largest = values [fo];
if (largest % values [f] == 0 && largest % values [s] == 0 && largest % values [t] == 0 && largest % values [fo] == 0)
return true;
}
}
}
}
return false;
}
public static boolean flushC (int fCard, int sCard, int tCard, int foCard, int fiCard, int siCard, int seCard)
{
int suits[] = {fCard, sCard, tCard, foCard, fiCard, siCard, seCard};
for (int f = 0 ; f < 7 ; f++)
{
for (int s = f + 1 ; s < 7 ; s++)
{
for (int t = s + 1 ; t < 7 ; t++)
{
for (int fo = t + 1 ; fo < 7 ; fo++)
{
if (suits [f] == suits [s] && suits [f] == suits [t] && suits [f] == suits [fo])
return true;
}
}
}
}
return false;
}
public static boolean straightC (int fCard, int sCard, int tCard, int foCard, int fiCard, int siCard, int seCard)
{
int values[] = {fCard, sCard, tCard, foCard, fiCard, siCard, seCard};
for (int f = 0 ; f < 7 ; f++)
{
for (int s = f + 1 ; s < 7 ; s++)
{
for (int t = s + 1 ; t < 7 ; t++)
{
for (int fo = t + 1 ; fo < 7 ; fo++)
{
int[] hand = {values [f], values [s], values [t], values [fo] };
Arrays.sort (hand);
if (values [fo] == values [f] + 3 && values [t] == values [f] + 2 && values [s] == values [f] + 1)
return true;
}
}
}
}
return false;
}
public static boolean isPrimeC (int fCard, int sCard, int tCard, int foCard, int fiCard, int siCard, int seCard)
{
int values[] = {fCard, sCard, tCard, foCard, fiCard, siCard, seCard};
for (int f = 0 ; f < 7 ; f++)
{
for (int s = f + 1 ; s < 7 ; s++)
{
for (int t = s + 1 ; t < 7 ; t++)
{
for (int fo = t + 1 ; fo < 7 ; fo++)
{
if (isPrime (values [f]) && isPrime (values [s]) && isPrime (values [t]) && isPrime (values [fo]))
return true;
}
}
}
}
return false;
}
public static void main (String[] args)
{
int allDivide = 0;
int allPrime = 0;
int straight = 0;
int flush = 0;
int straightFlush = 0;
int primeFlush = 0;
int divideFlush = 0;
int total = 0;
int[] suits = new int [50];
for (int counter = 1 ; counter < 6 ; counter++)
{
for (int increment = 1 ; increment < 11 ; increment++)
{
suits [increment * counter - 1] = counter;
}
}
for (int f = 0 ; f < 50 ; f++)
{
for (int s = f + 1 ; s < 50 ; s++)
{
for (int t = s + 1 ; t < 50 ; t++)
{
for (int fo = t + 1 ; fo < 50 ; fo++)
{
for (int fi = fo + 1 ; fi < 50 ; fi++)
{
for (int si = fi + 1 ; si < 50 ; si++)
{
for (int se = si + 1 ; se < 50 ; se++)
{
total++;
boolean allDivideB = false;
boolean allPrimeB = false;
boolean straightB = false;
boolean flushB = false;
If you are too confused by it (I used poor variable names and didn't comment) and would like me to add comments and improve my variable names I will. But I need to find a way to GREATLY reduce the amount of time it takes to execute.
The program does the following:
Generates every possible 7 card combination, generates every possible 4 card combination within that 7 card combination, finds out if that 4 card combination is a special type of hand (i.e. a flush), and if it is increments that type of special hand integer (if it's a flush, increment flush by 1).
Example:
First Generated 7 card hand (2,2,2,2,2,3,3)
-First generated 4 card hand (2,2,2,2)
A 7 card hand (2,4,5,6,7,8,11)
-A 4 card hand from the 7 card hand (4,7,8,11)
OneOffDriveByPoster
Posted: Sun Jan 13, 2008 9:00 am Post subject: Re: Question Regarding a Card Combinations Program
What is this with 7-card hands all of a sudden? Please try to use [syntax="java"] tags to keep your code indentation, etc.
_Dan_
Posted: Sun Jan 13, 2008 11:35 am Post subject: Re: Question Regarding a Card Combinations Program
Ya I needed 7 card hands, not 4 card hands, the 7 card system I have works though (I tested it). I guess maybe a better way of explaining it is explaining the program I'm making.
It's for my Data Management (Probabilities Unit), we have to make Casino games, I chose to make mine on Java, some probabilities are too difficult to figure out by hand (confirmed this with my teacher), hence I'm making a program to calculate them for me.
The game is the following:
The user is dealt 7 cards.
The user must select 4 cards from the 7 cards he is given to create a "hand".
This hand is checked to be a flush, straight, among other types of hands.
So I need to make the following run Iuno, like 1000-10000 times faster :S? Or maybe I'll just resort to leaving it on overnight to run.