Problem with my poker program
This section of code is where I am testing the algorithm to catch a winning hand i.e. two pair, flush, straight, etc... It is able to find everything except a full house following an XXYYY pattern and two pairs. I'm not getting any errors and doesn't seem to be reaching the part of the code where it searches for that full house and two pair. How should I change the code to find it?
Code :
package chapter7Project;
import java.util.ArrayList;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<String> hand = new ArrayList<String>(); //hand I will use for testing for two pair
hand.add("13 Spades"); //change the card values here for finding out other combos (1-13 or ace-king)
hand.add("1 Diamonds");
hand.add("2 Spades");
hand.add("2 Spades");
hand.add("13 Spades");
ArrayList<Integer> hCard = new ArrayList<Integer>();//ArrayLists for testing for combos
ArrayList<String> hSuit = new ArrayList<String>();
//separate cards and suits
for(String i : hand)
{
i = i.trim();//removes spaces at the beginning
int j = 0;
boolean done = false;
String acard = "";
while(!done)
{
if(i.substring(j, j+1).equalsIgnoreCase(" "))//changes face cards to numbers
{
if(acard.equalsIgnoreCase("Jack"))
{
acard = "11";
}
if(acard.equalsIgnoreCase("Queen"))
{
acard = "12";
}
if(acard.equalsIgnoreCase("King"))
{
acard = "13";
}
if(acard.equalsIgnoreCase("Ace"))
{
acard = "1";
}
hCard.add(Integer.parseInt(acard.trim()));
done = true;
}
acard += i.substring(j, j+1);
j++;
}
}
//gets each suit
for(String i : hand)
{
i = i.trim();
int j = i.length()-1;
String asuit = "";
boolean done = false;
while(!i.substring(j, j+1).equalsIgnoreCase(" "))
{
j--;
}
j++;
while(!done)
{
asuit += i.substring(j, j+1);
if(i.substring(j, j+1).equalsIgnoreCase(" ")||j==i.length()-1)
{
hSuit.add(asuit.trim());
done = true;
}
j++;
}
}
//puts the cards in numerical order
int j = 0;
ArrayList<Integer> greatest = new ArrayList<Integer>();//contains the cards placed in numerical order
ArrayList<String> boobs = new ArrayList<String>();
boobs.add("");
boobs.add("");
boobs.add("");
boobs.add("");
boobs.add(""); //fills the arraylists with 5 slots
greatest.add(0);
greatest.add(0);
greatest.add(0);
greatest.add(0);
greatest.add(0);
int i = 0;
//algorithm for putting the cards in numerical order
//by adding 1 to 'j' for each card its greater than
//sorts from least to greatest
while(greatest.contains(0))
{
for(int a : hCard)
{
j = 0;
int index = i;
for(int c : hCard)//checks if one card is bigger than another
{
if(a > c)
j++;
if ( a == c)//if the cards are the same, it looks for the next available slot
{
int b = greatest.indexOf(a);
if(b == 4)
{
j = 3;
break;
}
else if (b==3)
{
if(greatest.get(4) == 0){
j = 4;
}
else
{
j = 2;
}
break;
}
else if (b==2)
{
if(greatest.get(3) == 0)
{
j = 3;
}
else if(a == greatest.get(3))
{
j = 4;
}
else
{
j = 1;
}
break;
}
else if(b==1)
{
if(greatest.get(2) == 0)
{
j = 2;
}
else if(a == greatest.get(2))
{
j=3;
if(a == greatest.get(3))
{
j=4;
}
}
else
{
j = 1;
}
break;
}
else if(b==0)
{
if(a == greatest.get(1))
{
j = 2;
if( a == greatest.get(2))
j=3;
}
else
{
j = 1;
}
break;
}
}
}
greatest.set(j,a);//puts the card in the slot where it's supposed to be
boobs.set(j, hSuit.get(index));//gets the suit where the card is and puts it in the same index as the card put in greatest
i++;
}
hCard.removeAll(hCard);//removes all cards from hcCArd
hCard.addAll(greatest);//puts the sorted cards in hCard
hSuit.removeAll(hSuit);//does the same for the suits
hSuit.addAll(boobs);
System.out.println(hCard);
System.out.println(hSuit);
}
boolean pair = false;
boolean twoPair = false;
boolean threeOfAKind = false;
boolean straight = false;
boolean flush = false;
boolean fullHouse = false;
boolean fourOfAKind = false;
boolean straightFlush = false;
boolean royalFlush = false;
//checks for royal flush
if(hCard.contains(1)&&hCard.contains(10)&&hCard.contains(11)&&hCard.contains(12)&&hCard.contains(13))//checks for 10-ace
{
int check = 0;
String k = hSuit.get(0);
for(String a : hSuit)//checks if the suits are all the same
{
if(a.equalsIgnoreCase(k))
{
check++;
}
if(check==5)
{
royalFlush = true;
}
}
}
//checks for pair, 2 pair, 3 of a kind, full house, and four for of a kind
int l = 0;
while( l <= hCard.size()-1)//finds the first matching pair
{
if(hCard.get(l) != hCard.get(l+1))
l++;
else
{
break;
}
}
if(hCard.get(l) == hCard.get(l+1)&&l<hCard.size())//if there's a pair, it goes through here to search for other possibilities
{
System.out.println(l);
if(l <= 1)
{
l++;
System.out.println(l);
if(hCard.get(l) == hCard.get(l+1))//three of a kind if true
{
if(l < 2)
{
l++;
System.out.println(l);
if(hCard.get(l) != hCard.get(l+1))// checks for a full house
{
if(l < 3)
{
l++;
System.out.println(l);
if(hCard.get(l) == hCard.get(l+1))//if three of a kind then pair, XXXYY, then full house is true
{
fullHouse = true;
}
}
}
if(hCard.get(l) == hCard.get(l+1))//checks for four of a kind
{
fourOfAKind = true;
}
else//else it's a three of a kind
{
threeOfAKind = true;
}
}
else// if 'l' is too big then three of a kind is true
{
threeOfAKind = true;
}
}
else if(l < 1)//looks for two pair and full house patter, yyxxx
{
l++;
System.out.println(l);
//section where i need help
if(hCard.get(l) != hCard.get(l-1)&&hCard.get(l) == hCard.get(l+1)) //checks if the next card is equal to the last and is equal to the next
{
if(l < 2)
{
l++;
System.out.println(l);
if(hCard.get(l) == hCard.get(l+1)&&hCard.get(l+1)==hCard.get(l+2))//checks for a three of a kind
{
fullHouse = true;
}
}
else if(fullHouse == false)
{
twoPair = true;
}
}
}
}
else
{
if(hCard.get(l) != hCard.get(l-1)&&hCard.get(l) == hCard.get(l+1))//looks specifically for a two pair
{
if(l < 2)
{
l++;
System.out.println(l);
if(hCard.get(l) == hCard.get(l+1)&&hCard.get(l+1)!=hCard.get(l+2))//looks if the two pair is seperated or together
{
twoPair = true;
}
if(hCard.get(l) != hCard.get(l+1)&&hCard.get(l+1)==hCard.get(l+2))
{
twoPair = true;
} //down to here
}
}
}
if(twoPair == false && fullHouse == false && threeOfAKind == false && fourOfAKind == false)//if everything else is false, pair is at least true
pair = true;
}
//looks for a flush
int check = 0;
String k = hSuit.get(0);
for(String a : hSuit)
{
if(a.equalsIgnoreCase(k))
{
check++;
}
}
if(check==5)
{
flush = true;
}
//looks for a straight
if(hCard.get(1) == (hCard.get(0)+1) && hCard.get(2)== (hCard.get(1)+1) && hCard.get(3) == hCard.get(2)+1 && hCard.get(4) == hCard.get(3)+1)
{
straight = true;
}
else if(hCard.contains(1) && hCard.contains(10) && hCard.contains(11) && hCard.contains(12) && hCard.contains(13))
{
straight = true;
}
//if straight and flush are true, then you have a straight flush
if(straight == true && flush == true && royalFlush == false)
{
straightFlush = true;
straight = false;
flush = false;
}
//looks if anything is one
if(royalFlush == true)
{
System.out.println("HOLY FUCK BALLS, ROYAL FLUSH!!");
}
if(pair == true)
{
System.out.println("pair");
}
if(twoPair == true)
{
System.out.println("two pair");
}
if(threeOfAKind == true)
{
System.out.println("three of a kind");
}
if(fullHouse == true)
{
System.out.println("full house");
}
if(fourOfAKind == true)
{
System.out.println("four of a kind");
}
if(flush == true)
{
System.out.println("flush");
}
if(straight == true)
{
System.out.println("straight");
}
if(straightFlush == true)
{
System.out.println("straight flush");
}
}
}
Re: Problem with my poker program
One problem I see with the code is that there are no comments explaining the logic.
Re: Problem with my poker program
I added comments, could you help me now?
Re: Problem with my poker program
Some other items:
l is a very hard variable name to see because it looks like a 1
There is no way to tell which println statement was executed when you print out a number without a label:
System.out.println(l);
You have lots of these. You need to add a label to each one so you can tell which one executed:
System.out.println("l1=" +l);
changing the 1 to 2 etc so they are all different. Then you can find which one was executed.
Re: Problem with my poker program
Can you explain the logic you want to use to find a full house, three of a kind or two pairs?
Your comments don't explain what logic you want to use.
The question is:
Do you have good logic in your design but have not coded the program to follow the logic
Or the logic is bad.
Re: Problem with my poker program
The logic I'm using is:
If there is a pair, look at the next cardB.
If cardB is not equal to cardA, then see if the next 2 cards are the same as cardB.
If they are, then it is a full house.
For the two pair:
If there is a pair, look at the next card.
If the next card is not equal to the pair, then check to see if the next card is equal to it.
If not that, then go to the next to cards and check if they are equal.
Two pair if either statements are true.
My logic isn't flawed, but I don't quite know how to fix the code to match the logic flow. Another problem is that the code is hard to read because of all the braces, sorry about that.
Re: Problem with my poker program
I think you need to consider the possible hands based on where the beginning of the first pair is found.
Re: Problem with my poker program
That's what this block of code does:
Code :
int l = 0;
while( l <= hCard.size()-1)//finds the first matching pair
{
if(hCard.get(l) != hCard.get(l+1))
l++;
else
{
break;
}
}
It goes through the hand and sees if cardA == cardB
if it doesn't, then it goes to the next card.
Once it finds a matching pair, it breaks the while loop
Re: Problem with my poker program
One flaw in the logic you posted: it not possible to have a full house if the first card of the found pair is the second card.
The results of the loop you just posted is what I am talking about. You need to used the index to the first card in the pair to control the possible hands. If the index is 3, there is only a pair.
Re: Problem with my poker program
It checks for that here:
Code :
if(hCard.get(l) != hCard.get(l-1)&&hCard.get(l) == hCard.get(l+1)) //checks if the next card is equal to the last and is equal to the next
{
if(l < 2)
{
l++;
System.out.println(l);
if(hCard.get(l) == hCard.get(l+1)&&hCard.get(l+1)==hCard.get(l+2))//checks for a three of a kind
{
fullHouse = true;
}
}
else if(fullHouse == false)
{
twoPair = true;
}
}
if l < 2 then that means it is currently looking at the 2nd card. So there is no way that it will look for a full house if it == equals 2 or more
Re: Problem with my poker program
Your logic listed in post #6 leaves those detail out.
Re: Problem with my poker program
My bad for that, before every check for a win if statement is another if statement that checks for where the index is. Do you see any outstanding issues with the code now?
Re: Problem with my poker program
The issue for me is the logic. I would write straight forward code based on the first pair index value:
Code :
0/5 - fh & 4oak & 3oak & 2p & p xxxyy xxyyy xxxx? xxx?? xxyy? xx?yy xx???
1/4 - 40ak & 3oak & 2p & p xxxx xxx? xxyy xx??
2/3 - 3oak & p xxx xx?
3/2 - p xx
4/1 - hc -
When that code worked, then I might consider optimizing it.
Re: Problem with my poker program
I think that's what I did. It looks at the index, and then based off of that, it looks for the combinations afterwards.
Code :
//that's what these if statements are for
if(l<x)
{
...
}
x being some number between 1 and 3
Re: Problem with my poker program
My code would look like:
Code :
if(idx == 3) {
// pair only
}else if (idx == 2) {
// 3oak or pair
etc
Not a range of index values, a specific index value.
Re: Problem with my poker program
OOOHHHH, I see what you mean. Give me a few minutes
Re: Problem with my poker program
Here's the updated code:
Code :
package help;
import java.util.ArrayList;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<String> hand = new ArrayList<String>(); //hand I will use for testing for two pair
hand.add("1 Spades"); //change the card values here for finding out other combos (1-13 or ace-king)
hand.add("2 spades");
hand.add("11 Spades");
hand.add("12 Spades");
hand.add("13 Spades");
ArrayList<Integer> hCard = new ArrayList<Integer>();//ArrayLists for testing for combos
ArrayList<String> hSuit = new ArrayList<String>();
//separate cards and suits
for(String i : hand)
{
i = i.trim();//removes spaces at the beginning
int j = 0;
boolean done = false;
String acard = "";
while(!done)
{
if(i.substring(j, j+1).equalsIgnoreCase(" "))//changes face cards to numbers
{
if(acard.equalsIgnoreCase("Jack"))
{
acard = "11";
}
if(acard.equalsIgnoreCase("Queen"))
{
acard = "12";
}
if(acard.equalsIgnoreCase("King"))
{
acard = "13";
}
if(acard.equalsIgnoreCase("Ace"))
{
acard = "1";
}
hCard.add(Integer.parseInt(acard.trim()));
done = true;
}
acard += i.substring(j, j+1);
j++;
}
}
//gets each suit
for(String i : hand)
{
i = i.trim();
int j = i.length()-1;
String asuit = "";
boolean done = false;
while(!i.substring(j, j+1).equalsIgnoreCase(" "))
{
j--;
}
j++;
while(!done)
{
asuit += i.substring(j, j+1);
if(i.substring(j, j+1).equalsIgnoreCase(" ")||j==i.length()-1)
{
hSuit.add(asuit.trim());
done = true;
}
j++;
}
}
//puts the cards in numerical order
int j = 0;
ArrayList<Integer> greatest = new ArrayList<Integer>();//contains the cards placed in numerical order
ArrayList<String> boobs = new ArrayList<String>();
boobs.add("");
boobs.add("");
boobs.add("");
boobs.add("");
boobs.add(""); //fills the arraylists with 5 slots
greatest.add(0);
greatest.add(0);
greatest.add(0);
greatest.add(0);
greatest.add(0);
int i = 0;
//algorithm for putting the cards in numerical order
//by adding 1 to 'j' for each card its greater than
//sorts from least to greatest
while(greatest.contains(0))
{
for(int a : hCard)
{
j = 0;
int index = i;
for(int c : hCard)//checks if one card is bigger than another
{
if(a > c)
j++;
if ( a == c)//if the cards are the same, it looks for the next available slot
{
int b = greatest.indexOf(a);
if(b == 4)
{
j = 3;
break;
}
else if (b==3)
{
if(greatest.get(4) == 0){
j = 4;
}
else
{
j = 2;
}
break;
}
else if (b==2)
{
if(greatest.get(3) == 0)
{
j = 3;
}
else if(a == greatest.get(3))
{
j = 4;
}
else
{
j = 1;
}
break;
}
else if(b==1)
{
if(greatest.get(2) == 0)
{
j = 2;
}
else if(a == greatest.get(2))
{
j=3;
if(a == greatest.get(3))
{
j=4;
}
}
else
{
j = 1;
}
break;
}
else if(b==0)
{
if(a == greatest.get(1))
{
j = 2;
if( a == greatest.get(2))
j=3;
}
else
{
j = 1;
}
break;
}
}
}
greatest.set(j,a);//puts the card in the slot where it's supposed to be
boobs.set(j, hSuit.get(index));//gets the suit where the card is and puts it in the same index as the card put in greatest
i++;
}
hCard.removeAll(hCard);//removes all cards from hcCArd
hCard.addAll(greatest);//puts the sorted cards in hCard
hSuit.removeAll(hSuit);//does the same for the suits
hSuit.addAll(boobs);
System.out.println(hCard);
System.out.println(hSuit);
}
boolean pair = false;
boolean twoPair = false;
boolean threeOfAKind = false;
boolean straight = false;
boolean flush = false;
boolean fullHouse = false;
boolean fourOfAKind = false;
boolean straightFlush = false;
boolean royalFlush = false;
//checks for royal flush
if(hCard.contains(1)&&hCard.contains(10)&&hCard.contains(11)&&hCard.contains(12)&&hCard.contains(13))//checks for 10-ace
{
int check = 0;
String k = hSuit.get(0);
for(String a : hSuit)//checks if the suits are all the same
{
if(a.equalsIgnoreCase(k))
{
check++;
}
if(check==5)
{
royalFlush = true;
}
}
}
//checks for pair, 2 pair, 3 of a kind, full house, and four for of a kind
int l = 0;
while( l < hCard.size()-1)//finds the first matching pair
{
if(hCard.get(l) != hCard.get(l+1))
l++;
else
{
break;
}
}
if(l == 3)
{
pair = true;
}
else if(l==2)
{
if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)==hCard.get(l+2))
{
threeOfAKind = true;
}
else
{
pair = true;
}
}
else if(l==1)
{
if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l+2)==hCard.get(l)&&hCard.get(l+3)==hCard.get(l))
{
fourOfAKind = true;
}
else if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)==hCard.get(l+2)&&hCard.get(l+3)!=hCard.get(l))
{
threeOfAKind = true;
}
else if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)!=hCard.get(l+2)&&hCard.get(l+2)==hCard.get(l+3))
{
twoPair = true;
}
else
{
pair = true;
}
}
else if(l==0)
{
if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l+2)==hCard.get(l)&&hCard.get(l+3)==hCard.get(l))
{
fourOfAKind = true;
}
else if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)==hCard.get(l+2)&&hCard.get(l+3)!=hCard.get(l)&&hCard.get(l+3)==hCard.get(l+4))
{
fullHouse = true;
}
else if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)!=hCard.get(l+2)&&hCard.get(l+2)==hCard.get(l+3)&&hCard.get(l+3)==hCard.get(l+4))
{
fullHouse = true;
}
else if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)==hCard.get(l+2)&&hCard.get(l+3)!=hCard.get(l))
{
threeOfAKind = true;
}
else if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)!=hCard.get(l+2)&&hCard.get(l+2)==hCard.get(l+3))
{
twoPair = true;
}
else if(hCard.get(l)==hCard.get(l+1)&&hCard.get(l)!=hCard.get(l+2)&&hCard.get(l+2)!=hCard.get(l+3)&&hCard.get(l+3)==hCard.get(l+4))
{
twoPair = true;
}
else
{
pair = true;
}
}
//looks for a flush
int check = 0;
String k = hSuit.get(0);
for(String a : hSuit)
{
if(a.equalsIgnoreCase(k))
{
check++;
}
}
if(check==5 && royalFlush == false)
{
flush = true;
}
//looks for a straight
if(hCard.get(1) == (hCard.get(0)+1) && hCard.get(2)== (hCard.get(1)+1) && hCard.get(3) == hCard.get(2)+1 && hCard.get(4) == hCard.get(3)+1)
{
straight = true;
}
else if(hCard.contains(1) && hCard.contains(10) && hCard.contains(11) && hCard.contains(12) && hCard.contains(13)&&royalFlush == false)
{
straight = true;
}
//if straight and flush are true, then you have a straight flush
if(straight == true && flush == true && royalFlush == false)
{
straightFlush = true;
straight = false;
flush = false;
}
//looks if anything is one
if(royalFlush == true)
{
System.out.println("HOLY FUCK BALLS, ROYAL FLUSH!!");
}
if(pair == true)
{
System.out.println("pair");
}
if(twoPair == true)
{
System.out.println("two pair");
}
if(threeOfAKind == true)
{
System.out.println("three of a kind");
}
if(fullHouse == true)
{
System.out.println("full house");
}
if(fourOfAKind == true)
{
System.out.println("four of a kind");
}
if(flush == true)
{
System.out.println("flush");
}
if(straight == true)
{
System.out.println("straight");
}
if(straightFlush == true)
{
System.out.println("straight flush");
}
}
}
Thank you, it works flawlessly now. Would you like the code for the whole game now, as a reward?
Re: Problem with my poker program
Glad you got it working.
No thanks on the code.
Re: Problem with my poker program