Results 1 to 17 of 17
- 07-10-2011, 01:18 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
Help with an algorytm of 3 methods
Hi all, I'm doing a project with Eclipse, near to be finished..but there is a problem with a method that don't go.This methods have to select a "target" from an Arraylist of possible targets, looking for "nearest target","target with less life points","more distant target".
The methods select no target and so when methods "attaccoRavvicinato" and "attaccoDistante" are called an exception is generated and catched as well as defined.
I'll copy all the class where the problem occurred, but the part of interest reguards only methods:
-selectTargetVicino
-selectTargetLontano
-selectTargetFerito
and, as a consequence, methods "attaccoRavvicinato" and "attaccoDistante".
Tanks for any help.
Java Code:import java.util.ArrayList; public class Guerriero implements PersonaggioEsteso { protected int x; protected int y; protected int vita; protected int forza; protected int destrezza; protected int costituzione; protected boolean libero; protected Nemico bersaglio; protected int livello; protected int esperienza; protected ArrayList<Nemico> nemici; protected ArrayList<IPozione> pozioni; public Guerriero(){ pozioni=new ArrayList<IPozione>(); nemici=new ArrayList<Nemico>(); libero=true; esperienza=0; livello=0+(int)(esperienza/100); vita=(int)(15000+livello/2); forza=(int)(200+livello/2); destrezza=(int)(100+livello/2); costituzione=(int)(150+livello/2); x=(int)Math.random()*100; y=(int)Math.random()*100; } @Override public int getX() { // TODO Auto-generated method stub return x; } @Override public int getY() { // TODO Auto-generated method stub return y; } public int getDistanza(Personaggio n){ return getDistanzaX(n)+getDistanzaY(n); } public int getDistanzaX(Personaggio n){ int x1; if(n.getX()>x){ x1=n.getX()-x; }else{ x1=x-n.getX(); } return x1; } public int getDistanzaY(Personaggio n){ int y1; if(n.getY()>y){ y1=n.getY()-y; }else{ y1=y-n.getY(); } return y1; } @Override public int getVita() { // TODO Auto-generated method stub return vita; } public void setVita(int v){ vita=v; } @Override public int getForza() { // TODO Auto-generated method stub return forza; } public void setForza(int v){ forza=v; } @Override public int getDestrezza() { // TODO Auto-generated method stub return destrezza; } public void setDestrezza(int v){ destrezza=v; } @Override public int getCostituzione() { // TODO Auto-generated method stub return costituzione; } public void setCostituzione(int v){ costituzione=v; } @Override public void destra(int j)throws Exception { if(x+j>100 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ x=x+j; } System.out.println("una trappola impedisce al personaggio di muoversi"); // TODO Auto-generated method stub } @Override public void sinistra(int j)throws Exception { if(x-j<0 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ x=x-j; } System.out.println("una trappola impedisce al personaggio di muoversi"); // TODO Auto-generated method stub } @Override public void alto(int j)throws Exception { if(y+j>100 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ y=y+j; } System.out.println("una trappola impedisce al personaggio di muoversi"); // TODO Auto-generated method stub } @Override public void basso(int j)throws Exception { if(y-j<0 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ y=y-j; } System.out.println("una trappola impedisce al personaggio di muoversi"); // TODO Auto-generated method stub } @Override public void selectTargetVicino() { int i=0; for(;i<nemici.size()-1;i++){ if(getDistanza(nemici.get(i))<getDistanza(nemici.get(i+1))){ bersaglio=nemici.get(i); } bersaglio=nemici.get(i+1); // TODO Auto-generated method stub } } @Override public void selectTargetLontano() { int i=0; for(;i<nemici.size()-1;i++){ if(getDistanza(nemici.get(i))>=getDistanza(nemici.get(i+1))){ bersaglio=nemici.get(i); } bersaglio=nemici.get(i+1); // TODO Auto-generated method stub } } @Override public void selectTargetFerito() { int i=0; for(;i<nemici.size()-1;i++){ if((nemici.get(i).getVita())<=(nemici.get(i+1).getVita())){ bersaglio=nemici.get(i); } bersaglio=nemici.get(i+1); } // TODO Auto-generated method stub } @Override public void attaccoRavvicinato()throws Exception { if(bersaglio==null || getDistanza(bersaglio)>1){ throw new Exception(); } bersaglio.setVita((int)bersaglio.getVita()-50-(int)this.forza+(int)bersaglio.getCostituzione()/5); esperienza=esperienza+1; // TODO Auto-generated method stub } @Override public void attaccoDistanza()throws Exception { if(bersaglio==null){ throw new Exception(); } bersaglio.setVita((int)bersaglio.getVita()-50-(int)this.destrezza+(int)bersaglio.getCostituzione()/5); esperienza=esperienza+1; // TODO Auto-generated method stub } @Override public void liberati() { int j=(int)Math.random()*5; if(j>2){ libero=true; } // TODO Auto-generated method stub } @Override public boolean isLibero() { // TODO Auto-generated method stub return libero; } public void setLibero(boolean b){ libero=b; } @Override public void usaPozione(PersonaggioEsteso z) { for(IPozione p: pozioni){ p.usa(z); pozioni.remove(p); //Così uso tutte le pozioni che ho nell'ArrayList. } // TODO Auto-generated method stub } public void aggiungiPozione(){ int j=(int)Math.random()*200; if(j>160 && j<170){ IPozione p=new PozioneA(); pozioni.add(p); } if(j>170 && j<180){ IPozione p=new PozioneB(); pozioni.add(p); } if(j>180 && j<190){ IPozione p=new PozioneC(); pozioni.add(p); } if(j>190 && j<200){ IPozione p=new PozioneC(); pozioni.add(p); } } public void aggiungiNemico(Nemico n){ nemici.add(n); } public Nemico restituisciBersaglio(){ return bersaglio; } }Last edited by Yeshol; 07-10-2011 at 01:27 PM.
- 07-10-2011, 01:23 PM #2
You code dumped on us. You are going to find that not many people are going to wade through your uncommented code. Also using [code][/code] tags will help preserve your formatting.
You will find you will get better responses with a http://sscce.org/- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-10-2011, 01:28 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
Ok, sorry..I've modified the post
- 07-10-2011, 01:29 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Specific questions are always helpful too. What don't you understand? I believe much of this code is in a foreign(to me) language and without comments it's more difficult to see what's going on.
- 07-10-2011, 01:41 PM #5
Also I'm willing to bet that there are some things in your code that don't directly relate to your problem. A SSCCE will help us focus on your problem so that we can help you solve it quickly.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-10-2011, 01:59 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
I'll post the corrected code with comments, I'll hope there aren't languages errors.
Java Code:import java.util.ArrayList; public class Guerriero implements PersonaggioEsteso { protected int x; protected int y; protected int vita; protected int forza; protected int destrezza; protected int costituzione; protected boolean libero; protected Nemico bersaglio; protected int livello; protected int esperienza; protected ArrayList<Nemico> nemici; protected ArrayList<IPozione> pozioni; public Guerriero(){ pozioni=new ArrayList<IPozione>(); nemici=new ArrayList<Nemico>(); libero=true; esperienza=0; livello=0+(int)(esperienza/100); vita=(int)(15000+livello/2); forza=(int)(200+livello/2); destrezza=(int)(100+livello/2); costituzione=(int)(150+livello/2); x=(int)Math.random()*100; y=(int)Math.random()*100; } @Override public int getX() { // TODO Auto-generated method stub return x; } @Override public int getY() { // TODO Auto-generated method stub return y; } public int getDistanza(Personaggio n){ //Return the distance (coord x+y) from Player to an enemy return getDistanzaX(n)+getDistanzaY(n); } public int getDistanzaX(Personaggio n){ //distance in coord x from player to enemy int x1; if(n.getX()>x){ x1=n.getX()-x; }else{ x1=x-n.getX(); } return x1; } public int getDistanzaY(Personaggio n){ //distance in coord y from player to enemy int y1; if(n.getY()>y){ y1=n.getY()-y; }else{ y1=y-n.getY(); } return y1; } @Override public int getVita() { //return player's life points // TODO Auto-generated method stub return vita; } public void setVita(int v){ //set player's life points vita=v; } @Override public int getForza() { //return player's strong caracteristic // TODO Auto-generated method stub return forza; } public void setForza(int v){ //set player's strong caracteristic forza=v; } @Override public int getDestrezza() { //return another player's caracteristic // TODO Auto-generated method stub return destrezza; } public void setDestrezza(int v){ //set another player's caracteristic destrezza=v; } @Override public int getCostituzione() { //return another player's caracteristic // TODO Auto-generated method stub return costituzione; } public void setCostituzione(int v){ //set another player's caracteristic costituzione=v; } @Override public void destra(int j)throws Exception { //move player to the right if(x+j>100 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ //only if player is free, so if a trap is not attacked to him x=x+j; } System.out.println("una trappola impedisce al personaggio di muoversi"); // TODO Auto-generated method stub } @Override public void sinistra(int j)throws Exception { //move player to the left if(x-j<0 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ x=x-j; } System.out.println("una trappola impedisce al personaggio di muoversi"); // TODO Auto-generated method stub } @Override public void alto(int j)throws Exception { //move player to the top if(y+j>100 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ y=y+j; } System.out.println("una trappola impedisce al personaggio di muoversi"); //a trap not allowed the player to move // TODO Auto-generated method stub } @Override public void basso(int j)throws Exception { //move player down if(y-j<0 || j>(int)(destrezza/100)*5){ throw new Exception(); } if(libero==true){ y=y-j; } System.out.println("una trappola impedisce al personaggio di muoversi"); // TODO Auto-generated method stub } @Override public void selectTargetVicino() { int i=0; for(;i<nemici.size()-1;i++){ if(getDistanza(nemici.get(i))<getDistanza(nemici.get(i+1))){ //if distance from the enemy is less than from the next enemy.. bersaglio=nemici.get(i); //set the target to this enemy } bersaglio=nemici.get(i+1); //else set the target to the next enemy // TODO Auto-generated method stub } } @Override public void selectTargetLontano() { int i=0; for(;i<nemici.size()-1;i++){ if(getDistanza(nemici.get(i))>=getDistanza(nemici.get(i+1))){ //if distance from the enemy is more or equal from the next enemy.. bersaglio=nemici.get(i); //set the target to this enemy } bersaglio=nemici.get(i+1); //else set the target to the next enemy // TODO Auto-generated method stub } } @Override public void selectTargetFerito() { int i=0; for(;i<nemici.size()-1;i++){ if((nemici.get(i).getVita())<=(nemici.get(i+1).getVita())){ //if the life points of the enemy are less or equal than from the next enemy.. bersaglio=nemici.get(i); //set the target to this enemy } bersaglio=nemici.get(i+1); //else set the target to the next enemy } // TODO Auto-generated method stub } @Override public void attaccoRavvicinato()throws Exception { if(bersaglio==null || getDistanza(bersaglio)>1){ //if the target is null or the distance beetween player and enemy are more than 1 an exception is generated throw new Exception(); } bersaglio.setVita((int)bersaglio.getVita()-50-(int)this.forza+(int)bersaglio.getCostituzione()/5); //else the life points of the enemy are setted esperienza=esperienza+1; // TODO Auto-generated method stub } @Override public void attaccoDistanza()throws Exception { if(bersaglio==null){ //if the target is null an exception is generated throw new Exception(); } bersaglio.setVita((int)bersaglio.getVita()-50-(int)this.destrezza+(int)bersaglio.getCostituzione()/5); //else the life point of the enemy are setted esperienza=esperienza+1; // TODO Auto-generated method stub } @Override public void liberati() { //try to free the player if a trap is attacked to him int j=(int)Math.random()*5; if(j>2){ libero=true; } // TODO Auto-generated method stub } @Override public boolean isLibero() { //look if the player is free or not // TODO Auto-generated method stub return libero; } public void setLibero(boolean b){ //set the state of freedom of the player libero=b; } @Override public void usaPozione(PersonaggioEsteso z) { //the player use potions for(IPozione p: pozioni){ p.usa(z); pozioni.remove(p); //use all the potions that are in the ArrayList. } // TODO Auto-generated method stub } public void aggiungiPozione(){ //add a potion to the arrayList of potions of the player int j=(int)Math.random()*200; //random selection of the type of potion to add if(j>160 && j<170){ IPozione p=new PozioneA(); pozioni.add(p); } if(j>170 && j<180){ IPozione p=new PozioneB(); pozioni.add(p); } if(j>180 && j<190){ IPozione p=new PozioneC(); pozioni.add(p); } if(j>190 && j<200){ IPozione p=new PozioneC(); pozioni.add(p); } } public void aggiungiNemico(Nemico n){ //add an enemy to the arrayList of enemies of the player nemici.add(n); } public Nemico restituisciBersaglio(){ //return the target selected return bersaglio; } }
- 07-10-2011, 02:02 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
Have a look at the min( ... ) and max( ... ) methods in the Collections class. All you have to do is write two Comparator<Target> interface implementations.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-10-2011, 04:15 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
sorry, but I don't know how to make the comparator..I've never done before.
I'd like to compare the "distance" (method "getDistanza") from one enemy of the arrayList of enemies ("nemici" in my case) and the next enemy of this list, to establish who is nearest..for example.
And also compare the life in the same way, with the call of the method "getVita".
- 07-10-2011, 04:21 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
If you never do what you've never done before you'll never do it. A Comparator is a simple class that compares two objects of class T (here: a Target object). It should implementthe Comparator<Target> interface (read the API documentation); all you have to do is implement that interface two times (one for those 'life points' and one for the distance) and you can let the Collections.min/max() methods do the hard work.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-10-2011, 04:55 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
the errors are: "The method min(Collection<Nemico>, int) is undefined for the type Guerriero" and also "The method get(int) is undefined for the type Guerriero".Java Code:import java.text.Collator; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; public class Guerriero implements PersonaggioEsteso, Comparator<Nemico> { protected int x; protected int y; protected int vita; protected int forza; protected int destrezza; protected int costituzione; protected boolean libero; protected Nemico bersaglio; protected int livello; protected int esperienza; protected Collection<Nemico> nemici; //ArrayList<Nemico> protected ArrayList<IPozione> pozioni; .... public void selectTargetVicino() { bersaglio=min(nemici,compare(((ArrayList<Nemico>) nemici).get(0), bersaglio)); //error: } public int compare(Nemico n1, Nemico n2) { // TODO Auto-generated method stub if(getDistanza(n1)<getDistanza(n2)){ return 0; } return 1; }
Also how can implement compare another time with the same arguments?
When "protected Collection<Nemico> nemici;" was "protected ArrayList<Nemico> nemici;" the method get(int) was defined..and I could use that for look to the specified element of the list..but now how I can do that?Last edited by Yeshol; 07-10-2011 at 04:58 PM.
- 07-10-2011, 05:07 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
There is something fundamentily wrong with your design: how can a 'Guerriero' object compare two 'Nemico' objects? Also, can you please tell in simple English what your goal is? What is a 'Gerriero'? You have told us already what a 'Nemico' is (an enemy). Do you want to find the nearest enemy? w.r.t. what object? Distance takes two points at least; you need an 'anchor' point and find the object nearest to that anchor point.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-10-2011, 05:37 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
Guerriero is a warrior, an implementation of the interface "PersonaggioEsteso" that means ExtendedCharacter.
When the methods of Guerriero are called (selectTargetVicino,selectTargetLontano,selectTarg etFerito) I want to look all the enemy of the list (arrayList<Nemico> nemici) and to compare their distances, life points...in order to establish a target (variable "bersaglio" refers to "target" and is initially a null value) for my warrior: that can be the nearest or one of the nearest (if the are more then 1), the more distant or one of them, the more hurted (with less life point) or one of them.
For doing this i thought to compare 2 enemies, the first and the second...the second and the third..while I haven't looked all elements of the list, and to select as target who is aswer to my research.
I don't know if this structure is correct, but I hope it is.
What is your opinion?
- 07-10-2011, 05:53 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
A question for you: is there only one 'Guerriero'? If so, that simplifies matter a lot. The Comparator<Nemico> implementation should be somewhere else in a separate class; implementation should calculate the distance of two 'Nemico's and your single 'Guerrio' and return -1, 0 or 1 based on the result. If you make your 'Guerriero' object implement this interface it messes up the responsibilities of the different classes too much. So in pseudo code, the class should look like this:
kind regards,Java Code:public class DistanceComparator implements Comparator<Nemico> { private Guerriero g; public DistanceComparator(Guerriero g) { this.g= g; } // pass in the single Guerriero public int compareTo(Nemico n1, Nemico n2) { // pseudo code: double d1= |n1-g|; double d2= |n2-g|; return (d1 > d2)?1:(d1 < d2)?-1:0; } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-10-2011, 05:58 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
- 07-10-2011, 07:47 PM #15
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
I create the class DistanceComparator in this way:
but in the class Guerriero, writing this:Java Code:import java.util.Comparator; public class DistanceComparator implements Comparator<Nemico> { private PersonaggioEsteso g; public DistanceComparator(PersonaggioEsteso g) { this.g= g; } // pass in the single Guerriero @Override public int compare(Nemico n1, Nemico n2) { // TODO Auto-generated method stub // pseudo code: double d1= (n1.getDistanza(g)); double d2= (n2.getDistanza(g)); return (d1 > d2)?1:(d1 < d2)?-1:0; } }
generate an error, I can't implement DistanceComparator..Java Code:public class Guerriero implements PersonaggioEsteso,implements DistanceComparator {
you talk about interface, but DistanceComparator is defined as a concrete class..so how i've to do?
I have written "private PersonaggioEsteso g;" instead of "Private Guerriero g;" because there is also another class as Guerriero ("Mago" or Mage) that implements the interface "PersonaggioEsteso" and so the methods to return the distance.
- 07-10-2011, 08:32 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
A DistanceComparator is a clas; it implements the Comparator<Nemico> interface; you can't 'implement' another class; but if you want your Guerriero class implement the Comparator<Nemico> interface, do it like this:
I don't know what a PersonaggioEsteso is ...Java Code:class Guerriero implements Comparator<Nemico> { // all your other code goes here ... public int compare(Nemico n1, Nemico n2) { double d1= (n1.getDistanza(this)); // distance of n1 to this Guerriero double d2= (n2.getDistanza(this)); // ditto return (d1 > d2)?1:(d1 < d2)?-1:0; } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-10-2011, 10:02 PM #17
Member
- Join Date
- Jul 2011
- Posts
- 17
- Rep Power
- 0
PersonaggioEsteso means ExtendedCharacter and is the interface whose methods are implemented by "Guerriero" (warrior) and another class ("Mago" or Mage).
For the life point I've to create another method to compare the life of "Nemico n1" and "Nemico n2"?I can't use compare i think because it will override the method "compare(Nemico n1, Nemico n2) {" so can I use:
?Java Code:public int compare2(Nemico n1,Nemico n2){ double d1=(n1.getVita()); double d2=(n2.getVita()); return (d1 > d2)?1:(d1 < d2)?-1:0; }
Or is wrong?
And..(i hope is my last question, thank for your patience) after all, I've to use this two methods in a for cicle in order to compare all enemies of ArrayList nemici or the "min" method of collection? In the second case I've to convert the ArrayList type to Collection type I suppose and after to understand how to use the min in my specific case (Is not clear for me how this method work).
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Set ang get methods
By jcm2302 in forum New To JavaReplies: 1Last Post: 03-06-2010, 09:09 PM -
Methods
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 03-02-2010, 12:14 AM -
Methods
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 02-27-2010, 11:49 AM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks