LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2004, 09:09 AM   #1
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Rep: Reputation: 30
run into implementation problem with game (java)


Hi
I have an arraylist which contains card objects. Each object has a method which will return its value. I want to sort the array in numerical order by object value. I also need to remove and add objects to the list. Ideally I would like a structure which would order the objects as they are added to the list.

I'm still at a point where I could replace the arraylist with a different structure. Can anyone suggest a good technique to solve this problem ? And can anyone point me in the right direction for a tutorial which goes through the use of data structures such as sortedLists, arrayLists, maps etc

Thanks
 
Old 06-03-2004, 01:57 PM   #2
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
All all you need to do is have the objects implement the Comparable interface the way you want properly. Then you can just use the static Collections.sort() on the ArrayList.
For more info, look into the Java Collections Framework:
http://java.sun.com/docs/books/tutorial/collections/

Don't forget http://java.sun.com/api

Last edited by aaa; 06-03-2004 at 01:58 PM.
 
Old 06-03-2004, 02:12 PM   #3
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
if you wanted to sort as you added each element you could create a class (say SortedArraylist) which extends ArrayList, then you can add a new method (or override the old one) to sort the list after adding each card using aaa's solution.

Code:
public class SortedArrayList extends ArrayList
{
    public boolean addSorted(Comparable addedObject)
    {
         add(addedObject);
         Collections.sort(this);
    }
}
Something like that should do the trick (I think)

Last edited by dave_starsky; 06-03-2004 at 02:25 PM.
 
Old 06-03-2004, 02:46 PM   #4
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
There is also the SortedSet interface. Any class that implements this (like TreeSet) will automatically sort it's objects. The problem is that it's a Set, and there can be no two or more identical objects in a Set.

ArrayList implements the List interface. So, it is a good idea to do this:
List list = new ArrayList();
By doing this, you can switch to any other List on a whim (performance reasons, etc), without changing much code (perhaps just one line):
List list = new Vector();
You can switch to using a Vector easily, without changing lots of code. You may not be able to do this if you extend ArrayList and add a new method not in the List interface. If you do extend it, I suggest that you override one (or more) of the methods already in List, to retain compatibility.
 
Old 06-03-2004, 05:23 PM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
As said a TreeSet would be ideal with a Comparator if you are dealing with only one pack of cards in your game - as each card would be unique - You couldn't just sort them by value (at least I don't think you'd want to ?) as there would be four duplicate values for each card. Of course, we don't know what the rules of your game will be

Last edited by Looking_Lost; 06-03-2004 at 05:27 PM.
 
Old 06-04-2004, 08:00 AM   #6
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Original Poster
Rep: Reputation: 30
Thanks for all the advice - you people are the best

Sorry i neglected to say that at most the array could contain 4 duplicate values. The value returned is between 2-14 representing the numerical value of the card, this is going to mean i can't use some structures, such as the sets.

The game involves numerical comparison of cards, suite value is unimportant. If the cards are sorted it is easier for me to write the code that the comp uses to pick a good hand or to select a card to play.

Each card could return a unique id, i could associate the id to the numerical value using a hash table. This would solve the sorting problem with the sets (if i sort by the id) but ideally i'd like to find a sorting solution without having to do this.

Im going to do some reading about the comparable interface. Feel free to throw in a few more suggestions now you know about the duplicates.

Thanks all
 
Old 06-11-2004, 08:49 PM   #7
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Original Poster
Rep: Reputation: 30
Hi again
I've written a method which sorts the arraylist as i need but i can't figure out how i can have the list sorted as i add objects to it. This is what i've come up with.

(Card object implements Comparable)
Code:
        public int compareTo(Object test){
        final int BEFORE = -1;
        final int EQUAL = 0;
        final int AFTER = 1;
        Card temp = (Card)test;
        if (this.returnValue() == temp.returnValue()) return EQUAL;
        if (this.returnValue() > temp.returnValue()) return BEFORE;
        if (this.returnValue() < temp.returnValue()) return AFTER;
        return EQUAL;                
        }
(SortedArrayList extends ArrayList)
Code:
public boolean add(Comparator cardToSort)
        {
         if(add(cardToSort)==true){
                Collections.sort(this);
                return true;}
        return false;
        }
There is no error returned, but when i check the list it is not ordered. When i call this method from the class which creates the SortedArrayList the list is sorted without problem.

Code:
//order the cards in reverse numerical order. 
public void orderCompHand(){
    Collections.sort(compHand);
                        }

Last edited by darkRoom; 06-11-2004 at 08:53 PM.
 
Old 06-12-2004, 04:04 AM   #8
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Can't say for certain but seeing as I don't know exactly what the orginal code in the overriden add method is (could probably search and find it), when you call add you are calling your own created add, it's probably easier just to create another method that uses the orginal add, something like this (assuming your card class is called Card), then just use myDeckOfCards.insert(a_card):


Code:
import java.util.ArrayList;
import java.util.Collections;

public class SortedArrayList extends ArrayList{

	
	public  boolean insert(Object cardToSort)
        {
		
		if(cardToSort instanceof Card)
		   { 	
		
		     if(this.add((Card)cardToSort)){
			 Collections.sort(this);
			 return true;
			 }
                    }
       
           return false;
        }
	
}

Last edited by Looking_Lost; 06-12-2004 at 04:06 AM.
 
Old 06-12-2004, 05:58 AM   #9
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Uhmm, well, just realised that was probably my worst and inaccurate explanation of anything yet on these boards After I actually thought about it and remembered the principles of Object Orientated Programming - this:


Code:
import java.util.ArrayList;
import java.util.Collections;

public class SortedArrayList extends ArrayList {

	
	public  boolean add(Card newCard) //Add a new Card object
         {

		     if(this.add((Object)newCard)){    //Cast to Object ensures we're using the original add method
			
			  Collections.sort(this);
 
			  return true;
			 }
                 
       
           return false;
        }
	
}
This is only good for using and sorting Card objects, but then that's the only type that will be being added
 
Old 06-12-2004, 08:33 AM   #10
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Original Poster
Rep: Reputation: 30
Thanks looking_lost your last post clears up any problems. Its all working fine

Thanks to everyone else as well
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Swing Card Game kermitthefrog91 Programming 11 08-18-2005 11:46 AM
java / game install prob GoreSpilr Linux - Newbie 2 09-25-2004 07:59 PM
possible to run a game thats already installed? icebreaker Linux - Games 4 05-13-2004 12:09 AM
getting game etc to run on solaris 9. fiff Solaris / OpenSolaris 2 12-25-2003 09:44 PM
Wine - The game will not run under Windows 95 shermang Linux - Software 3 09-04-2003 01:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration