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
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
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
public class Hand extends CardCollection { public void replaceCard(int i, Card c) { cards.remove(i); //cards.add(i,c); cards.add(c); } }
VideoPoker
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
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 )