Results 1 to 8 of 8
Thread: Card Sort Algorithm
- 08-27-2013, 12:31 PM #1
Member
- Join Date
- Aug 2013
- Location
- Munich, Germany
- Posts
- 3
- Rep Power
- 0
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
- 08-27-2013, 12:41 PM #2
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.
PhilMath problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-27-2013, 02:24 PM #3
Re: Card Sort Algorithm
Starfall, please go through these two pages and edit your post:
http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum - Learn Java Programming
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-27-2013, 02:33 PM #4
Member
- Join Date
- Aug 2013
- Location
- Cuxhaven, Germany
- Posts
- 5
- Rep Power
- 0
Re: Card Sort Algorithm
If you implemented the class "Karte" change it to
Java Code:public class Karte implements Comparable<Karte> { ... }
Java Code:@Override public int compareTo(Karte k) { return this.score.compareTo(k.score); }
Java Code:@Override public int compareTo(Karte k) { return (this.score < k.score ? -1 : (this.score == k.score ? 0 : 1)); }
[ 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);
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); }
declare the colors in the right order:
Java Code:public enum Color { KARO, HERZ, PIK, KREUZ; ... }
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))); }
Last edited by raz0r; 08-27-2013 at 02:50 PM.
- 08-27-2013, 04:42 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Card Sort Algorithm
I don't see any advanced stuff in here so I moved the thread.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-28-2013, 12:21 AM #6
Member
- Join Date
- Aug 2013
- Location
- Munich, Germany
- Posts
- 3
- Rep Power
- 0
- 09-01-2013, 02:41 PM #7
Member
- Join Date
- Aug 2013
- Location
- Munich, Germany
- Posts
- 3
- Rep Power
- 0
Re: Card Sort Algorithm
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
- 09-01-2013, 04:51 PM #8
Similar Threads
-
Quick Sort Algorithm
By Teareal in forum New To JavaReplies: 2Last Post: 03-17-2013, 08:27 PM -
Bubble sort Algorithm
By Hotspotmar in forum New To JavaReplies: 1Last Post: 06-06-2012, 07:38 AM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Help with sort algorithm
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:09 AM -
Insertion sort algorithm
By Albert in forum Advanced JavaReplies: 2Last Post: 06-28-2007, 08:26 PM
Bookmarks