2012-03-01 7 views
0

私は迷路ゲームを作成しています。そのために私は4つの別々のクラスファイルを作成しました。私は()メソッドだけでなく、それを読むだけでなく、それに追加して、それを上書きしPlayer.class移動にMaze.classの受刑者にアクセスする方法を知りたい:別のクラスの変数を使用するには?

Maze.class

package y1; 

import java.util.*; 

@SuppressWarnings(value = "all") 

public class Maze { 
private Room entry; 
private Map <Room, ArrayList<Player>> inmates=new HashMap<Room,ArrayList<Player>>(); 
/** 
* 
* @param r The room that is going to be the entry 
*/ 
public void setEntry(Room r){ 
    this.entry=r; 
    } 
/** 
* 
* @return Returns the entry room 
*/ 
    public Room getEntry(){ 
     return this.entry; 
    } 
    /** 
    * Method adds a player to the maze 
    * @param p Player to be added to the maze 
    */ 
    public void addPlayer(Player p){ 
     ArrayList<Player> players=new ArrayList<Player>(); 
     p.setRoom(getEntry()); 
     if (this.inmates.isEmpty()==true){ 
     players.add(p); 
     this.inmates.put(this.entry,players); 
     } 
     else{ 
     players=this.inmates.get(this.entry); 
     players.add(p); 
     } 

     this.inmates.put(p.getRoom(), players); 

    } 
    public void getPlayers(Room r){ 
     if(this.inmates.get(r)!=null){ 
     System.out.println(Arrays.asList(this.inmates.get(r))); 
     } 
     else{ 
     System.out.println("Ruum on tühi"); 

     } 
    } 
    public Map getInmates(){ 
     return this.inmates; 
    } 
} 

Room.class

package y1; 

import java.util.*; 

@SuppressWarnings(value = "all") 
public class Room { 

private String name; 
private Room north; 
private Room east; 
private Room west; 
private Room south; 
private boolean isExit = false; 
private boolean isPortal = false; 
private Maze maze; 

/** 
* @return Returns the name of the room 
*/ 
public String getName() { 
    return this.name; 
} 

/** 
* Sets room name 
* 
* @param name 
*/ 
public void setName(String name) { 
    this.name = name; 
} 

/** 
* Gets northern room if any 
* 
* @return pointer to northern room if any, otherwise <code>null</code> 
*/ 
public Room getNorth() { 
    return this.north; 
} 

/** 
* Sets the door to the next room to the north in that room and in the other 
* room sets southern door as connecting back to that room 
* 
* @param otherRoom 
*/ 
public void setNorth(Room otherRoom) { 
    this.north = otherRoom; 
    otherRoom.south = this; 
} 
public Room getSouth() { 
    return this.south; 
} 

/** 
* Sets the door to the next room to the south in that room and in the other 
* room sets northern door as connecting back to that room 
* 
* @param otherRoom 
*/ 
public void setSouth(Room otherRoom) { 
    this.south = otherRoom; 
    otherRoom.north = this; 
} 
public Room getEast() { 
    return this.east; 
} 

/** 
* Sets the door to the next room to the east in that room and in the other 
* room sets western door as connecting back to that room 
* 
* @param otherRoom 
*/ 
public void setEast(Room otherRoom) { 
    this.east = otherRoom; 
    otherRoom.west = this; 
} 
public Room getWest() { 
    return this.west; 
} 

/** 
* Sets the door to the next room to the west in that room and in the other 
* room sets eastern door as connecting back to that room 
* 
* @param otherRoom 
*/ 
public void setWest(Room otherRoom) { 
    this.west = otherRoom; 
    otherRoom.east = this; 
} 
/** 
* Returns the room in the given direction 
* 
* @param Which way to move? 
* @return The room in that direction. 
*/ 
public Room get(String direction){ 
    Room dir=this; 
    if(direction=="N" && this.north!=null){ 
     dir=dir.getNorth(); 
     return dir; 
    } 
    else if(direction=="W" && this.west!=null){ 
     dir=dir.getWest(); 
     return dir; 
    } 
    else if(direction=="E" && this.east!=null){ 
     dir=dir.getEast(); 
     return dir; 
    } 
    else if(direction=="S" && this.south!=null){ 
     dir=dir.getSouth(); 
     return dir; 
    } 
    else{ 
     return dir; 
    } 


} 
/** 
* Returns the room that givens coordinates point to 
* 
* @param dirlist List of directions 
* @return returns Returns the room it stops in 
*/ 
public Room get(List<String> dirlist){ 
    Room dir=this; 
    if (validate(dirlist)==true){ 
     for(int i=0;i<dirlist.size();i++){ 
      if(dirlist.get(i)=="N"){ 
       dir=dir.getNorth(); 
      } 
      else if(dirlist.get(i)=="W"){ 
       dir=dir.getWest(); 
      } 
      else if(dirlist.get(i)=="E"){ 
       dir=dir.getEast(); 
      } 
      else if(dirlist.get(i)=="S"){ 
       dir=dir.getSouth(); 
      } 
     } 
    } 
    return dir;  
} 


/** 
* creates a new room to the north and connects back to this room 
* 
* @param toa nimi 
*   0 
* @return uus tuba 
*/ 
public Room createNorth(String name) { 
    Room otherRoom = null; 

    // Creates new room only when no room lies ahead in this direction 
    if (this.getNorth() == null) { // Checks north - if nothing there then new room is create 
     otherRoom = new Room(); // Creates new room 
     this.setNorth(otherRoom); // Creates door between rooms 
     otherRoom.setName(name); // Names the room 

    } else { // If room already exists then prints alert message 
     System.out.println("Room already exist!"); 
    } 

    return otherRoom; 
} 
public Room createSouth(String name) { 
    Room otherRoom = null; 
    if (this.getSouth() == null) { 
     otherRoom = new Room(); 
     this.setSouth(otherRoom); 
     otherRoom.setName(name); 

    } else { 
     System.out.println("Room already exists!"); 
    } 

    return otherRoom; 
} 
public Room createEast(String name) { 
    Room otherRoom = null; 
    if (this.getEast() == null) { 
     otherRoom = new Room(); 
     this.setEast(otherRoom); 
     otherRoom.setName(name); 

    } else { 
     System.out.println("Room already exists!"); 
    } 

    return otherRoom; 
} 
public Room createWest(String name) { 
    Room otherRoom = null; 
    if (this.getWest() == null) { 
     otherRoom = new Room(); 
     this.setWest(otherRoom); 
     otherRoom.setName(name); 

    } else { 
     System.out.println("Room already exists!"); 
    } 

    return otherRoom; 
} 
public void setExit(){ 
    this.isExit=true; 
} 

public void setPortalA(){ 
    this.isPortal=true; 
} 
public boolean validate(List<String> pathList){ 
    Room check = this; 
    boolean value=false; 
    for(int i = 0;i<pathList.size();i++){ 
     if(pathList.get(i)=="N" && check.north!=null){ 
      check=check.north; 
      value=true; 
     } 
     else if(pathList.get(i)=="S" && check.south!=null){ 
      check=check.south; 
      value=true; 
     } 
     else if(pathList.get(i)=="W" && check.west!=null){ 
      check=check.west; 
      value=true; 
     } 
     else if(pathList.get(i)=="E" && check.east!=null){ 
      check=check.east; 
      value=true; 
     } 
     else{ 
      value = false; 
      System.out.println("Can't move in the given directions!"); 
     } 
    } 
    return value;  
} 

@Override 
public String toString() { 
    return this.getName(); 
} 

}

とPlayer.class

package y1; 

import java.util.*; 

public class Player { 
private String name; 
private Room location; 

public void setRoom(Room r){ 
    this.location=r; 
} 
public Room getRoom(){ 
    System.out.println(this.name+" is at " +this.location); 
    return this.location; 

} 


public Room move(String dir){ 
    Player p=this; 
    if(dir=="N" && this.location.getNorth()!=null){ 
     p.location=p.location.getNorth(); 
    } 
    else if(dir=="S" && this.location.getSouth()!=null){ 
     p.location=p.location.getSouth(); 
    } 
    else if(dir=="W" && this.location.getWest()!=null){ 
     p.location=p.location.getWest(); 
    } 
    else if(dir=="E" && this.location.getEast()!=null){ 
     p.location=p.location.getEast(); 
    } 
    else{ 
     System.out.println("There's a wall in the way!"); 
    } 
    return this.location; 
} 
public Room move(List<String> dirList){ 
    Player player=this; 
     for(int i=0 ; i<dirList.size() ; i++){ 
      if(dirList.get(i)=="N" && player.location.getNorth()!=null){ 
       player.location=player.location.getNorth(); 
      } 
      else if(dirList.get(i)=="S" && player.location.getSouth()!=null){ 
       player.location=player.location.getSouth(); 
      } 
      else if(dirList.get(i)=="W" && player.location.getWest()!=null){ 
       player.location=player.location.getWest(); 
      } 
      else if(dirList.get(i)=="E" && player.location.getEast()!=null){ 
       player.location=player.location.getEast(); 
      } 
      else{ 
       System.out.println("Wall in the way. Stopping!"); 
       break; 
      } 
     } 
    return this.location; 
} 

public void setName(String n){ 
    this.name=n; 
} 


@Override 
public String toString() { 
    return this.name; 
} 

}

答えて

2

プレーヤーは、getInmates()メソッド(またはその他の修飾メソッド)を呼び出すために、何らかの方法でMazeオブジェクトへの参照を持つ必要があります。

どのようにリファレンスを取得するかはあなた次第であり、さまざまな方法があります。 1つの方法は、プレイヤーの場所(ルームオブジェクト)に参照を提供させることです。これを行うには、ルームの迷路を返すルームオブジェクトにgetMaze()メソッドを実装できます。 Playerのmove()メソッドでは、Playerの 'location'プロパティ(Room)でgetMaze()を呼び出し、返されたMazeオブジェクトでgetInmates()を呼び出すことができます。

その後、Mazeオブジェクトの修飾子/リセットメソッドをどのように実装したいのかだけです。

+0

ありがとうございます!それは私が探していた解決策です!それが私の目の前にあったとは信じられない。私はルームクラスにもう対処する必要はないと考えていました。しかし、エスケープがないようです:D –

+0

私はそれを考えて、私にとって最良の方法は、プレーヤーに「mazeloc」などと呼ばれる別の場所プロパティを割り当てることであると認識しました。 addPlayer()メソッドを使用すると初期化されます。このプロパティは、プレイヤーが現在どの迷路にいるかを示します。 –

+0

@ Rauno、それはうまく動作しますが、 "適切な"デザインという点では最良の方法ではないかもしれません。問題は、あなたがそれを行うことで、より多くの「簿記」を導入しているということです。プレイヤーが動くたびに、「場所」に加えて「mazeloc」を更新する必要があります。 – JKomusin

3

モディファイアメソッドを作成する必要があります。

public void resetInmates(){ 
    this.inmates = new Map(); 
} 

public void addInmate(Room r, ArrayList<Player> players){ 
} 

public void removeInmate(Room r){ 
} 

など

+0

おそらくゲッターメソッドより優れていますが、他のクラスがどれくらいのアクセスを必要としているかは少し異なります。 –

+0

'public'を使ってそれらのメソッドを公開したくないかもしれないことに注意してください。それらのメソッドがパッケージの外部へのアクセスを目的としていない場合は、パッケージの可視性で十分です。 –

0

単に代わりに(おそらくも見えパッケージ)同じパッケージ内の他のクラスのそれが読みやすくなりますが、getterメソッドをcreataする良いデザインだろう受刑者フィールドからprivateを削除します。

+1

これは非常に悪い習慣です。他のクラスでは、「受刑者」の実施方法やアクセス/変更方法についての詳細については心配する必要はありません。 – scibuff

+1

真実ですが、質問には答えます。また、パッケージの中に隠れていて外部に露出していなければ、そのメリットがあるかもしれません。私は既にscibuffの答えが良いと述べました。 –

関連する問題