2016-04-29 14 views
0

私のゲームには、部屋、ランプ、胸、ジャワ、プレーヤー、キー、マップを含むいくつかのクラスがあります。これらはすべてテストされており、正しいので、今はプログラムのドライバーである自分の冒険クラスを書いています。私は選手の部屋の場所を[0] [0]に設定する必要があり、私はどのように把握できません。ここに私の部屋と冒険クラスでこれまで持っているものがあります。テキストベースの冒険ゲームの冒険クラス

 
public class Adventure { 

    Scanner in = new Scanner(System.in); 
    private Room room; 

    public Adventure() { 
     Player player = new Player(); 
     Map map = new Map(); 
     player.setX(0); 
     player.setY(0); 
     int x = 0; 
     int y = 0; 
     map.getRoom(x, y).getDescription(); 
    } 
} 

public class Room { 

    private String description; 
    private boolean north; 
    private boolean south; 
    private boolean east; 
    private boolean west; 
    private boolean isDark; 

    private Lamp theLamp; 
    private Key theKey; 
    private Chest theChest; 

    /** 
    * Returns the text description of this room 
    */ 
    public String getDescription() { 
     return description; 
    } 

    /** 
    * Returns true if the player can go north from this room 
    */ 
    public boolean canGoNorth() { 
     return north; 
    } 

    /** 
    * Returns true if the player can go south from this room 
    */ 
    public boolean canGoSouth() { 
     return south; 
    } 

    /** 
    * Returns true if the player can go east from this room 
    */ 
    public boolean canGoEast() { 
     return east; 
    } 

    /** 
    * Returns true if the player can go west from this room 
    */ 
    public boolean canGoWest() { 
     return west; 
    } 

    /** 
    * Returns the lamp object in this room. 
    * If no lamp is present, returns null 
    */ 
    public Lamp getLamp() { 
     return theLamp; 
    } 

    /** 
    * Sets the lamp variable in this room to null 
    */ 
    public void clearLamp() { 
     theLamp = null; 
    } 

    /** 
    * Returns the key object in this room. 
    * If no key is present, returns null 
    */ 
    public Key getKey() { 
     return theKey; 
    } 

    /** 
    * Sets the key variable in this room to null 
    */ 
    public void clearKey() { 
     theKey = null; 
    } 

    /** 
    * Returns the chest object in this room. 
    * If no chest is present, returns null 
    */ 
    public Chest getChest() { 
     return theChest; 
    } 

    /** 
    * Returns true if there is no light in this room, 
    * veeeeeeeery dangerous! 
    */ 
    public boolean isDark() { 
     return isDark; 
    } 


    /** 
    * Hey wassup dawg? I'ma constructor. I make the objects round these parts, 
    * sometimes without even trying, knowwhatimssayin? 
    * Yall don't haveta worry 'bout me for this'ere game, but look me up in 
    * Chapter 6 sometime. Kay? 
    * 
    */ 
    public Room(String description, boolean north, boolean south, boolean east, 
      boolean west, boolean isDark, Lamp theLamp, Key theKey, 
      Chest theChest) { 
     super(); 
     this.description = description; 
     this.north = north; 
     this.south = south; 
     this.east = east; 
     this.west = west; 
     this.isDark = isDark; 
     this.theLamp = theLamp; 
     this.theKey = theKey; 
     this.theChest = theChest; 
    } 
} 

マップクラスの説明が印刷されるように、ルームの場所を0,0に設定する必要があります。

+0

セッターとゲッターを含むRoomクラスのxとy変数を作成しますか?あなたがPlayerクラスで同じことをしたようです。しかし、多分私は問題が何であるか完全に理解していないでしょう。 – Mumfi

+1

マップapiとはプレイヤーと部屋にはおそらくマップ上の場所があります。 – plalx

+0

あなたの部屋のクラスのプレーヤーに参照を追加してみませんか? "playersIn"リストのようなものです。そこにはセッターでプレーヤーオブジェクトを追加します。 – Matt

答えて

0

あなたは既にルームオブジェクトを持っていますので、実際に各ルームのX/Y座標を保存する必要がありますか? North/South/East/West Roomオブジェクト(該当する場合)を介して相互にリンクされたルームオブジェクトの構造を作成することができます。

"currentRoom"や "location"などのプレーヤークラス内に変数を設定することができます。この位置を取得して設定する関数を含めると、キャラクタの現在の位置を設定してアクセスできるようになりました。

X/Yがあなたにとって本当に重要なのであれば、あなたの冒険クラスでは、ルームオブジェクトの2次元配列を作成し、X/Y座標を使ってこの構造内の部屋を見つけることができますx "は行を参照し、" y "は配列に格納された位置を特定する列を参照します)。

しかし、退室/ルームポインタを使用して他のルームオブジェクトに移動してリンクすると、ルームオブジェクトを作成するのが簡単になり、無駄が少なく、拡張性が向上する可能性があります。

public class Player { 
    private Room location = null; 
    public void setLocation(Room newLocation) { 
     location = newLocation; 
    } 
} 

public class Room { 
    private Room NorthExit = null; 

    public Room getNorthExit() { 
     return NorthExit; 
    } 

    public setNorthExit(Room newRoom) { 
     NorthExit = newRoom; 
    } 
} 


// In main somewhere... 
Room room1 = new Room(); 
Room room2 = new Room(); 
room1.setNorthExit(room2); 

Player player1 = new Player(); 
player1.setLocation(room1); 

我々は、ユーザーがその場所にROOM1を設定し、その場所のオブジェクトからROOM1に関するすべての情報にアクセスすることができます最初の部屋にいるならば、我々は、2つの部屋を作成して、リンクの上にあなたはコード内で見ることができます。また、ロケーションオブジェクトから、各出口のロケーションオブジェクト/参照を介して、隣の部屋にアクセスすることもできます。

関連する問題