P
Piotre Ugrumov
I have a museum map of this type:
7-8-3
| | |
6-4-2
| | |
5-0-1
the | and the - are the corridors between the rooms of the museum.
I have created a class Room, every object of this class has an id, a name,
an array where are inserted the near room(for example for the room 2 the
near are 3, 1, 4 this id are inserted in the array vicini), an array
percorsi where are memorized the path to all the rooms of the musem (for
example, in the room 0 the path to go to the room 8 is memorized in
percorsi[8] and is 0 4 8).
I have in a main an array of room that rapresent the museum.
I have a lot of problems in the determination of the path.
Now I pass you the class Room with the implementation of the method that
determinates the path.
Class Room:
public class Room {
private int id;
private String nome; //the name of the room
private String percorsi[]= new String[9]; //in this array the are the
path for all the destination
private int vicini[]=new int[4]; //this array contains the near room
private boolean verifica[]=new boolean [9]; //if verifica[x] is set to
true means that there is the path to the destination x
public Room() {
id=100;
nome="";
for(int i=0; i<9; i++){
percorsi="";
}
}
public Room(int a, String s, String p, String o){
setId(a);
setNome(s);
for(int i=0; i<4; i++)
vicini=100; //the 100 means that there are not near room
for(int i=0; i<9; i++){
percorsi="";
percorsi+=id;
}
}
public void setRoom(int a, String s, String p, String o){
setId(a);
setNome(s);
for(int i=0; i<9; i++){
percorsi="";
percorsi+=id;
}
}
public void setId(int a){
id=a;
verifica[id]=true; //the path to itself is always true
}
public void setNome(String s){
nome=s;
}
public void addVicino(int v){
for(int i=0; i<4; i++){
if(vicini==100 && v!=100){
vicini=v;
verifica[v]=true;
percorsi[v]+=" " + v; //this line of code permit to set the
path to the near room in fact the path is, for example for the room 3 if is
inserted the near room 2, 3 2
break;
}
}
}
public int getVicino(int v){
return vicini[v];
}
public int getId(){
return id;
}
public String getNome(){
return nome;
}
public void Percorsi(Room museo[]){//crea i percorsi
for(int l=0; l<museo.length-1; l++){ //the cycle is repeated to
determinate all the path
for(int j=0; j<verifica.length; j++){
if(verifica[j]==false){//verify if the path to the
destination j is determinated
for(int h=0; h<vicini.length; h++){
if(vicini[h]!=100 &&
museo[getVicino(h)].verifica[j]){ //if the near room is valid(that is the
number in the array vicini is different from 100) and if the near room has
the path to the destination j
percorsi[j]+= " " +
museo[getVicino(h)].getPercorsi(j); //in the path to j(in this path there
is already the id of the room, for example all the path of the room 0 start
with 0) is added the path to j through the near room tha permit to arrive to
j, for example if I am in the room 8 I want go to the room 2(j), the path
is to the path to 3 of 8 (that is 8) and the path to 2 of the room 3(that is
3 2)
the path complete is 8 3 2. This method seems work but there are some little
problems, if I try to run this method I have seen that the path from the
room 0
to the rooms 3 and 7 is uncorrect in fact the path that I receive is equal
to 0. If I try with the room 3 to see the path to the room 6, the method run
but the path that I receive is 3 2 1 0 4 6, this path is considerably longer
than 3 8 7 6. How can I resolve thes problems?
verifica[j]=true; //it signals that now there is
the path
j=0; //permit to start from 0 again the cycle,
in this way all the messagges are swapped
}
}
}
}
}
}
public String getPercorsi(int i){
return percorsi;
}
public boolean verifyVicino(int v){
for(int i=0; i<4; i++){
if(vicini!=100 && vicini==v && v!=100)
return true;
}
return false;
}
public boolean percorsoC(int v){
return verifica[v];
}
}
Thanks.
7-8-3
| | |
6-4-2
| | |
5-0-1
the | and the - are the corridors between the rooms of the museum.
I have created a class Room, every object of this class has an id, a name,
an array where are inserted the near room(for example for the room 2 the
near are 3, 1, 4 this id are inserted in the array vicini), an array
percorsi where are memorized the path to all the rooms of the musem (for
example, in the room 0 the path to go to the room 8 is memorized in
percorsi[8] and is 0 4 8).
I have in a main an array of room that rapresent the museum.
I have a lot of problems in the determination of the path.
Now I pass you the class Room with the implementation of the method that
determinates the path.
Class Room:
public class Room {
private int id;
private String nome; //the name of the room
private String percorsi[]= new String[9]; //in this array the are the
path for all the destination
private int vicini[]=new int[4]; //this array contains the near room
private boolean verifica[]=new boolean [9]; //if verifica[x] is set to
true means that there is the path to the destination x
public Room() {
id=100;
nome="";
for(int i=0; i<9; i++){
percorsi="";
}
}
public Room(int a, String s, String p, String o){
setId(a);
setNome(s);
for(int i=0; i<4; i++)
vicini=100; //the 100 means that there are not near room
for(int i=0; i<9; i++){
percorsi="";
percorsi+=id;
}
}
public void setRoom(int a, String s, String p, String o){
setId(a);
setNome(s);
for(int i=0; i<9; i++){
percorsi="";
percorsi+=id;
}
}
public void setId(int a){
id=a;
verifica[id]=true; //the path to itself is always true
}
public void setNome(String s){
nome=s;
}
public void addVicino(int v){
for(int i=0; i<4; i++){
if(vicini==100 && v!=100){
vicini=v;
verifica[v]=true;
percorsi[v]+=" " + v; //this line of code permit to set the
path to the near room in fact the path is, for example for the room 3 if is
inserted the near room 2, 3 2
break;
}
}
}
public int getVicino(int v){
return vicini[v];
}
public int getId(){
return id;
}
public String getNome(){
return nome;
}
public void Percorsi(Room museo[]){//crea i percorsi
for(int l=0; l<museo.length-1; l++){ //the cycle is repeated to
determinate all the path
for(int j=0; j<verifica.length; j++){
if(verifica[j]==false){//verify if the path to the
destination j is determinated
for(int h=0; h<vicini.length; h++){
if(vicini[h]!=100 &&
museo[getVicino(h)].verifica[j]){ //if the near room is valid(that is the
number in the array vicini is different from 100) and if the near room has
the path to the destination j
percorsi[j]+= " " +
museo[getVicino(h)].getPercorsi(j); //in the path to j(in this path there
is already the id of the room, for example all the path of the room 0 start
with 0) is added the path to j through the near room tha permit to arrive to
j, for example if I am in the room 8 I want go to the room 2(j), the path
is to the path to 3 of 8 (that is 8) and the path to 2 of the room 3(that is
3 2)
the path complete is 8 3 2. This method seems work but there are some little
problems, if I try to run this method I have seen that the path from the
room 0
to the rooms 3 and 7 is uncorrect in fact the path that I receive is equal
to 0. If I try with the room 3 to see the path to the room 6, the method run
but the path that I receive is 3 2 1 0 4 6, this path is considerably longer
than 3 8 7 6. How can I resolve thes problems?
verifica[j]=true; //it signals that now there is
the path
j=0; //permit to start from 0 again the cycle,
in this way all the messagges are swapped
}
}
}
}
}
}
public String getPercorsi(int i){
return percorsi;
}
public boolean verifyVicino(int v){
for(int i=0; i<4; i++){
if(vicini!=100 && vicini==v && v!=100)
return true;
}
return false;
}
public boolean percorsoC(int v){
return verifica[v];
}
}
Thanks.