Jump to content
  • Advertisement
Sign in to follow this  
loachman

Java sort cards tips?

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am making a card game (Poker) I have everything implemented up to the final sort. What I plan on doing is sorting the arrayList by numbers using substring, and then after the sort.Putting the suit into on array and the number into another. I was wondering the best way to go about this a bubble sort of some sort? Here is my psuedo code of sorts.
private void bubsort(){
     // I have an array List defined
     //private List<String> finalHand = new ArrayList<String>();
     //Which contains the following
     //{h9,c7,h13,h3,c2}
     //hearts=h,clubs=c,d=diamonds,spades=s although no spades or c in this hand
     I was wondering if I could bubble sort this using a substring(?,?)so I would sort the cards on only the numbers.

Then when I break it into two arrays would be
   array1=[c,h,c,h,h]
   array2=[2,3,7,9,13]
This makes is easy to check for flushes and straights etc....
Hope that makes sense. I know the basic theory to do this I think, Btw I am coming over to java from actionscript. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Hi,
Thanks for the reply. I had looked into the collections before.

public static void sort(List list,Comparator c)

But I am not clear on how it works? The comparator I assume is what I want to compare but how would I get it to compare by the numbers and not the aplha part ot the "string/card".

I think maybe I would have to do a cast to an int perhaps?

Thanks

Share this post


Link to post
Share on other sites
I am too far into my game to re-write it really.

I am nearly finished. the reason I posted into the beginners forum was to get help with what I've got.

A know a String is not a card. But the string represents a card to me.

I simply want help to sort what I've got.

I'm not trying to sound rude but really this is for learning and I'll get to the card class eventually.


Thanks

Share this post


Link to post
Share on other sites
Okay, I'm working on it. What happens if you have the same numbers of different suits, let's say four sevens. How must they be sorted, or does it not matter?

Share this post


Link to post
Share on other sites
OK, then all you need is a custom comparator that only compares the numbers (which have to be parsed from the string), then you can use Collections.sort.
After that you can extract the relevant information into two arrays. Don't copy and paste, see if you can understand it and then write it yourself:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class PokerComparator implements Comparator<String>
{
public int compare(String s1, String s2)
{
int rank1 = Integer.parseInt(s1.substring(1));
int rank2 = Integer.parseInt(s2.substring(1));
return rank1 - rank2;
}
}

public class Poker
{

public static void main(String[] args)
{
List<String> finalHand = new ArrayList<String>(Arrays.asList(new String[] { "h9", "c7", "h13", "h3", "c2" }));

Collections.sort(finalHand, new PokerComparator());
System.out.println(finalHand);

final int numberOfCards = finalHand.size();
char suits[] = new char[numberOfCards];
int ranks[] = new int[numberOfCards];
for (int i = 0; i < numberOfCards; ++i)
{
String s = finalHand.get(i);
suits = s.charAt(0);
ranks = Integer.parseInt(s.substring(1));
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by loachman
Hi,
Thanks for the reply. I had looked into the collections before.

public static void sort(List list,Comparator c)

But I am not clear on how it works? The comparator I assume is what I want to compare but how would I get it to compare by the numbers and not the aplha part ot the "string/card".

I think maybe I would have to do a cast to an int perhaps?

Thanks


You have to write a comparator which will compare the strings representing cards yourself. Something like:


class CardComparator implements Comparator
{
public int compare(Object left, Object right)
{
String leftStr = (String)left;
String rightStr = (String)right;

int leftCardNum = Integer.parseString( leftStr.substr(1, 2);
int rightCardNum = Integer.parseString( rightStr.substr(1, 2);

return rightCardNum - leftCardNum;
}

Collections.sort(cardsList, new CardComparator());




(disclaimer: typed off the top of my head, probably doesn't work but should be enough to get you started)

Edit: I type too slowly. [grin]

Share this post


Link to post
Share on other sites
Why not make an array [1..52] of a Byte. Representing the deck.
Sort these. http://www.gamedev.net/community/forums/topic.asp?topic_id=430793

A card has properties:
number
suit


Afterwards, you can set up data that points to these cards.
make a player class.

A player card has the properties:
pointer (to a card of the deck)
etc, etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!