LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   run into implementation problem with game (java) (https://www.linuxquestions.org/questions/programming-9/run-into-implementation-problem-with-game-java-189232/)

darkRoom 06-03-2004 09:09 AM

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

aaa 06-03-2004 01:57 PM

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

dave_starsky 06-03-2004 02:12 PM

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)

aaa 06-03-2004 02:46 PM

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.

Looking_Lost 06-03-2004 05:23 PM

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 :)

darkRoom 06-04-2004 08:00 AM

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

darkRoom 06-11-2004 08:49 PM

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);
                        }


Looking_Lost 06-12-2004 04:04 AM

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;
        }
       
}


Looking_Lost 06-12-2004 05:58 AM

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

darkRoom 06-12-2004 08:33 AM

Thanks looking_lost your last post clears up any problems. Its all working fine :)

Thanks to everyone else as well


All times are GMT -5. The time now is 05:50 PM.