Results 1 to 8 of 8
  1. #1
    Starfall is offline Member
    Join Date
    Aug 2013
    Location
    Munich, Germany
    Posts
    3
    Rep Power
    0

    Default Card Sort Algorithm

    Hi,

    I am trying to write a program that is sorting a hand, containing 8 cards.
    Each card has a color and a value. The program should compare all 8 cards and start with the highest value. If 2 or more cards have the same value the color will decide the priority.
    I made a research on what possibilities I have in Java and found the compareTo-Solution, but it is not working, so maybe somebody can help me or give me the advise I need to complete my program.

    public int compareTo(Karte o) {

    if(this.getScore()<o.getScore())
    return -1;
    else if(this.getScore()==o.getScore())
    return 0;
    else
    return 1;

    }
    My idea was to give the method a cardList, then to save this list in a temporary list, clear the cardList and then fill it up again with the cards from the temporary list card by card. But I still dont understand how this CompareTo really works and how to get the next object in the list.

    public List<Karte> CardsSort(List<Card> cardList) {
    List<Karte> temp = new ArrayList<Karte>();
    for (Iterator iter = cardList.iterator(); iter.hasNext();) {
    Karte karte = (Karte) iter.next();
    temp.add(karte);
    }
    cardList.clear();


    if(card.compareTo(next)>0)
    cardList.add(card);
    else if(card.compareTo(next)==0)
    {
    if(next.getColor()!=Color.HERZ)
    cardList.add(card);
    else if(next.getColor()==Color.HERZ)
    cardList.add(next);
    else if(card.getScore()>next.getScore())
    cardList.add(karte);
    else
    cardList.add(next);
    }
    else
    { if(card.getColor()!=Color.HERZ)
    cardList.add(next);}
    }
    return cardList;
    }

    I would be very happy if someone could help me in this issue.

    Andrej

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Card Sort Algorithm

    Hi Andrej,

    I'd implement a Comparator, using the values and colors, then I'd deal the hand unsorted and last call Collections.sort(List, Comparator).
    There's no need to implement the sorting yourself.

    Phil
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Card Sort Algorithm

    Starfall, please go through these two pages and edit your post:
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    raz0r is offline Member
    Join Date
    Aug 2013
    Location
    Cuxhaven, Germany
    Posts
    5
    Rep Power
    0

    Default Re: Card Sort Algorithm

    If you implemented the class "Karte" change it to

    Java Code:
    public class Karte implements Comparable<Karte> {
        ...
    }
    Then you have to implement the method

    Java Code:
    @Override
    public int compareTo(Karte k) {
        return this.score.compareTo(k.score);
    }
    if type of score (i think it is int or double) implements Comparable (int and double do), or

    Java Code:
    @Override
    public int compareTo(Karte k) {
        return (this.score < k.score ? -1 : (this.score == k.score ? 0 : 1));
    }
    if type of score does not.

    [ return (a == b ? 1 : 0); is the same as if(a==b) return 1; else return 0; ]

    Then you just call

    Java Code:
    Collections.sort(cardList);
    after you inserted the cards.

    Regards,

    raz0r

    EDIT: Oh, its not int and double, but Integer and Double that implement Comparable...

    You could cast the score or use the second version of the method.

    compareTo with cast:

    Java Code:
    @Override
    public int compareTo(Karte k) {
        return ((Integer)this.score).compareTo((Integer)k.score);
    }
    EDIT2: Oh and i missed the card colors ...

    declare the colors in the right order:

    Java Code:
    public enum Color {
        KARO,
        HERZ,
        PIK,
        KREUZ;
    
        ...
    }
    then you just change your compareTo method to

    Java Code:
    @Override
    public int compareTo(Karte k) {
        int c = ((Integer)this.score).compareTo((Integer)k.score);
        return (c != 0 ? c : (this.color.compareTo(k.color)));
    }
    because all enums implement Comparable.
    Last edited by raz0r; 08-27-2013 at 02:50 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Card Sort Algorithm

    I don't see any advanced stuff in here so I moved the thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Starfall is offline Member
    Join Date
    Aug 2013
    Location
    Munich, Germany
    Posts
    3
    Rep Power
    0

    Default Re: Card Sort Algorithm

    Quote Originally Posted by PhHein View Post
    Hi Andrej,

    I'd implement a Comparator, using the values and colors, then I'd deal the hand unsorted and last call Collections.sort(List, Comparator).
    There's no need to implement the sorting yourself.

    Phil
    Many, many thanks@all. I have something that looks very similar to your answer raz0r.
    I still got strange errors, but I am confident to fix it.
    Yes maybe advanced was not best area.
    Andrej

  7. #7
    Starfall is offline Member
    Join Date
    Aug 2013
    Location
    Munich, Germany
    Posts
    3
    Rep Power
    0

    Default Re: Card Sort Algorithm

    Quote Originally Posted by Starfall View Post
    Many, many thanks@all. I have something that looks very similar to your answer raz0r.
    I still got strange errors, but I am confident to fix it.
    Yes maybe advanced was not best area.
    Andrej
    Hey again,
    I moved forward but still is not the result is was hoping for.
    I want the cards not only sorted by value and and grouped by color but also there is the difference between trumps and not-trumps.
    For instance. The "Unters"(4) and "Obers"(4) + all hearts are trumps so there are 14 at all. I want it sorted first by trumpf then by value grouped by color.

    Right now in my main I have:
    public static void main(String[] args) {

    TreeSet<Karte> handDesSpielers = new TreeSet<Karte>();
    cardHand.add(new Card(Color.HERZ,Wert.ACHT));
    cardHand.add(new Card(Color.SCHELLEN,Wert.SAU));
    cardHand.add(new Card(Color.GRAS,Wert.UNTER));
    cardHand.add(new Card(Color.GRAS,Wert.OBER));
    cardHand.add(new Card(Color.EICHEL,Wert.ZEHN));
    cardHand.add(new Card(Color.EICHEL,Wert.OBER));
    cardHand.add(new Card(Color.EICHEL,Wert.UNTER));
    cardHand.add(new Card(Color.EICHEL,Wert.ACHT));

    for(Card card:handDesSpielers)
    {

    System.out.println(card.getColor() +" "+ card.getWert());
    }
    }
    and the sorted result is:

    HERZ ACHT
    EICHEL OBER
    EICHEL UNTER
    EICHEL ZEHN
    EICHEL ACHT
    GRAS OBER
    GRAS UNTER
    SCHELLEN SAU

    but should be:
    EICHEL OBER
    GRAS OBER
    EICHEL UNTER
    GRAS UNTER
    HERZ ACHT
    EICHEL ZEHN
    EICHEL ACHT
    SCHELLEN SAU

    My compareTo() in the Class Card:
    public int compareTo(Karte o) {

    int c = (this.getColor()).compareTo(o.getColor());
    return (c != 0 ? c : (this.getPriority() - ((o.getPriority()))));
    }
    + Constructor
    public Card(Color color,Wert wert) {
    this.color = color;
    this.setWert(wert);
    switch (wert) {
    case SAU: this.score= 11; this.priority = 3;break;
    case ZEHN: this.score = 10; this.priority = 4;break;
    case KOENIG: this.score = 4; this.priority = 5;break;
    case OBER: this.score = 3; this.priority = 1;break;
    case UNTER: this.score = 2; this.priority = 2;break;
    case NEUN: this.score = 0; this.priority = 6;break;
    case ACHT: this.score = -1; this.priority = 7;break;
    case SIEBEN: this.score = -2; this.priority = 8;break;
    }
    }

    Am happy for any hint.
    Andrej

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Card Sort Algorithm

    Starfall, I gave you two links at #3. I expect you to go through them and edit your posts.

    Failure to comply may result in this thread being closed. Repeated failure may result in a ban.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Quick Sort Algorithm
    By Teareal in forum New To Java
    Replies: 2
    Last Post: 03-17-2013, 08:27 PM
  2. Bubble sort Algorithm
    By Hotspotmar in forum New To Java
    Replies: 1
    Last Post: 06-06-2012, 07:38 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. Help with sort algorithm
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:09 AM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •