Problem with Ordering an Arraylist of Comparable Objects.
I know I posted this problem in another thread but I thought it would be more appropriate to put it here, if one of the mods wants to close the other thread that's fine.
Basically I have a Video Poker game and I order the hand to make identifying pairs, straights etc easier.
However it seems to order hand objects in other classes which is absolutely confounding me, here are the classes and relevent code:
Card
Code :
public class Card implements Comparable<Card>
{
protected int rank;
protected int suit;
protected String[] suitStrings = {"Clubs", "Diamonds", "Spades", "Hearts"};
protected String[] rankStrings = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
public Card(int a, int b)
{
if (a >= 0 && a <= 3);
{
suit = a;
}
if (b <= 12 && b >= 0);
{
rank = b;
}
}
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}
public String suitAsString()
{
return suitStrings[suit];
}
public String rankAsString()
{
return rankStrings[rank];
}
public String cardAsString()
{
String value;
value = rankStrings[rank] + " of " + suitStrings[suit];
return value;
}
public String cardValueAsShortString()
{
String value;
value = suit + "_" + rank;
return value;
}
public int compareTo(Card c)
{
int result;
if(rank != c.getRank())
result = rank - c.getRank();
else
result = suit - c.getSuit();
return result;
}
}
CardCollection
Code :
import java.util.ArrayList;
import java.util.Collections;
public class CardCollection
{
public ArrayList<Card> cards = new ArrayList<Card>();
public CardCollection()
{}
public void orderCards()
{
Collections.sort(this.cards);
System.out.println("ORDER");
}
public void printCollectionLong()
{
for(int i = 0; i < cards.size(); i++)
{
System.out.print(cards.get(i).cardAsString() + ", ");
if(i%5 == 4)
System.out.print("\n");
}
System.out.print("\n\n\n\n");
}
}
Hand
Code :
public class Hand extends CardCollection
{
public void replaceCard(int i, Card c)
{
cards.remove(i);
//cards.add(i,c);
cards.add(c);
}
}
VideoPoker
Code :
public class VideoPoker
{
public CardDeck deck;
public Hand playerHand, h2;
protected StrengthChecker sc;
public int determineStrength()
{
System.out.println("detStr 1:");
playerHand.printCollectionLong();
h2 = playerHand;
sc = new StrengthChecker(h2);
System.out.println("detStr 2:");
playerHand.printCollectionLong();
sc.setStrength();
System.out.println("detStr 3:");
playerHand.printCollectionLong();
h2.printCollectionLong();
deck.printCollectionLong();
return sc.getStrength();
}
}
I put h2 in here for test purposes on the first run through the prints in the first block are in random order while the ones in the second are in order(not what I want) and the hands in the third are in order but the deck is not.
Also a new deck is initialised after the first deal of a game. However the print in the first block of determineStrength are always in order after the first run through.
StrengthChecker
Code :
public class StrengthChecker
{
Hand hand1;
Hand hand2;
int strength;
public StrengthChecker(Hand h)
{
hand1 = h;
hand2 = h;
System.out.println("SC cons 1:");
hand2.printCollectionLong();
hand1.orderCards();
System.out.println("SC cons 2:");
hand2.printCollectionLong();
}
public int getStrength()
{
return strength;
}
public void setStrength()
{
if (isStraightFlush() == true)
strength = 8;
else if (Has4OfKind() == true)
strength = 7;
else if (isFullHouse() == true)
strength = 6;
else if (isFlush() == true)
strength = 5;
else if (isStraight() == true)
strength = 4;
else if (Contains3OfAKind() == true)
strength = 3;
else if (Contains2Pair() == true)
strength = 2;
else if (ContainsPair() == true)
strength = 1;
else
strength = 0;
}
}
As a test I put in the second hand and invoked it's method to check the order of the cards the first time before I order hand1 hand2 shows the cards in a random order however afterwards it is ordered this really doesn't make sense to me since I only invoked hand1's orderCards method.
There are other methods and variables but I put in everything I think is relevent to this problem.
Now this is the first time I've tried to order objects in an array so maybe it's not unusual but it sure as hell has me stumped any help is appreciated. And sorry for the lack of comments just something I never got into(keep meaning to but I keep forgetting :P)
Re: Problem with Ordering an Arraylist of Comparable Objects.
Have you tried debugging the code by adding some println() statements to see what is happening and how the values of variables are changing? Since your problem is with things in the wrong order, start with the compareTo() method. Make sure its giving the correct results.
Re: Problem with Ordering an Arraylist of Comparable Objects.
Fairly basic stuff, I did try that a load of places but the compareTo() method didn't occur to me. Tried it out and it helped me identify the problem it was in the GUI class I made, a bit of a hangover from a way I was planning on implementing.
Code :
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x > DECK_X && x < DECK_X + CARD_WIDTH && y > DECK_Y && y < DECK_Y + CARD_HEIGHT)
{
game.deal();
repaint();
game.determineStrength();//This is the line that caused the problem as deal invokes it as well.
}
Cheers man.
Now how do I change the title to show this is solved.
Re: Problem with Ordering an Arraylist of Comparable Objects.
Thread Tools -> Mark this thread as solved
You can find this at the top of the page.
// Json
Re: Problem with Ordering an Arraylist of Comparable Objects.
Cheers man.
I have it seems messed up though and left it a bit too hastily. I was positive I checked and that it sorted a hand once but it turns out I didn't have the determineStrength method anywhere in the deal method
This is the secondDeal method
Code :
public void secondDeal()
{
int winnings;
System.out.println("secondDeal");
for(int i=0; i<5;i++)
{
if (held[i] == false)
{
playerHand.replaceCard(i, deck.dealTopCard());
System.out.print(i+":"+held[i]);
}
held[i] = false;
}
winnings = returns[determineStrength()] * bet; // This is a new line
increaseCredit(winnings);
stageOne = true;
}
and this is the modified determineStrength method
Code :
public int determineStrength()
{
sc = new StrengthChecker(playerHand);
//System.out.println("detStr 2:");
//playerHand.printCollectionLong();
sc.setStrength();
//System.out.println("detStr 3:");
//playerHand.printCollectionLong();
//h2.printCollectionLong();
//deck.printCollectionLong();
if (sc.getStrength() == 0 && sc.getImpCardH().getRank() < 9)
return 0;
else
return sc.getStrength();
}
I mean my understanding of what happens here is that I create a StrengthChecker object called sc which then has a Hand object hand1 which is distinct to playerHand from the VideoPoker object so why are both being sorted when I invoke
Argh I was full sure I had it before I must have just wanted it to be right so much I tricked myself :P
EDIT: Is it something to do with clone() and implements Cloneable? I'm just trying that out but I seem to be getting the same result.
Re: Problem with Ordering an Arraylist of Comparable Objects.
Quote:
Is it something to do with clone() and implements Cloneable
how are you using those?
Re: Problem with Ordering an Arraylist of Comparable Objects.
OK well I've just gone back to it tonight it seems mad to me I've tried a few ways. At the moment I have this bit of code in there to see if it will work.
Code :
//h2 = new Hand(playerHand.getCards());
//h2 = new Hand(playerHand.clone());
h2 = playerHand.clone();playerHand.printCollectionLong();//I've tried this way and the two above all to the same effect
h2.orderCards();
playerHand.printCollectionLong();
This is the Hand class
Code :
import java.util.ArrayList;
import java.util.Collections;
//import java.util.Comparator;
//import java.io.Serializable;
public class Hand extends CardCollection implements Cloneable
{
public Hand()
{}
public Hand clone()
{
System.out.println("ADAFEGSDVBBH BNSF");
Hand c = new Hand();
c.setCards(this.getCards());
return c;
}
public Hand(Hand another)
{
this(another.getCards()); // you can access
System.out.print("RAR2");
}
public Hand(ArrayList<Card> c)
{
this.setCards(c);
}
public void replaceCard(int i, Card c)
{
cards.remove(i);
cards.add(i,c);
}
}
Every time the println statements show the playerHand to have been sorted even though I only called the method orderCards in h2.
Re: Problem with Ordering an Arraylist of Comparable Objects.
Add more println() statements to show who is calling the method to do the sorting.
Re: Problem with Ordering an Arraylist of Comparable Objects.
Ah I got it, I think :P. At least everything seems to be working how I would expect it I had tried doing a similar thing with the cards but not quite both together which seems to have worked like so.
I created a constructor for Hand that took in an ArrayList of cards
Code :
public Hand(ArrayList<Card> c)
{
cards.clear();
int i = 0;
while(i<c.size())
{
cards.add(c.get(i));
i++;
}
}
Haha I'm just looking at my code I thought I had did something with the Card class but now I can see I didn't this seems to work I mean it's sorting the arraylist I expect it to and no others as you can see it was by passing the cards one by one rather then just using the whole thing all a bit odd and contrary to how I assumed it would work.
I mean if you had say
Then y would be 10 but x would still be 5 I guess it's different for objects.
Anyways that problem has taken up more time then the rest of the code :P I'll probably try to jazz it up a bit now work on a few graphics or something. I'll post it to the café at some stage too if anyone wants to take a look at it.