2016-05-24 10 views
1

プレーヤーが入力するステップに基づいて、それぞれの隣接方向を返したいとします。入力はcharにすることはできません。intにする必要があります(ちょうど南に行くと言うことはできませんが、地図の階段を下に置く必要があります)。 nextAreaメソッドで失われた気分。 私には答えを書かないでください。ちょっとしたガイダンスが必要です。switchステートメントからすべての可能な結果を​​返します

これらは)

public void walk(){ 
    current = gameOne.getCurrentArea(); 

    walk = sc.nextInt(); 

    // pick a direction to walk into 
    // from that space show connected areas 
} 


public void pickMove(){ 
System.out.print("walk shoot, quit or reset?"); 
answer = sc.nextLine(); 
//walk 
//if area not connected: alert 
//if into village: alert that they are healed 
//if containing wolf: altert they are bitten, if already bitten game ends 


switch(answer) { 
       case "walk":System.out.print("from " + current + " a direction to walk into south(3), east(1), west(2), north(6),"); 
          this.walk(); 
          gameOne.tryWalk(walk); 
          gameOne.getCurrentArea(); 
         //what I want>>>get current areas to s, e, w and n 

私の現在の試みはWereWolfenstein2Dクラスで次のようになりますgameOne.nextArea(GameWorldクラス

/** 
* Returns true if areas s1 and s2 are connected, false otherwise. 
* Also returns false if either area is an invalid area identifier. 
* @param s1 the first area 
* @param s2 the second area 
* @return true if areas are connected, false otherwise 
*/ 
private boolean areasConnected(int s1, int s2) { 
    if (Math.min(s1, s2) >= 0 && Math.max(s1, s2) < numAreas) { //valid areas... 
     //...but are they connected? 
     return east[s1] == s2 || north[s1] == s2 || west[s1] == s2 || south[s1] == s2; 
    } 
    //Either s1 or s2 is not a valid area identifier 
    return false; 
} 


/** 
* Determine ID number of an adjacent area given its direction from the 
* current area. 
* @param direction the direction to look (n for north, e for east, s for south, w for west) 
* @return number of the area in that direction 
* @throws IllegalArgumentException if direction is null 
*/ 
    public int nextArea(char direction) { 
    int nextIs; // area number of area in indicated direction 

    //Valid values 
    final char N = 'n', E = 'e', S = 's', W = 'w'; 

    trace("nextArea() starts..."); 

    // examine adjacent areas 
    switch (direction) { 
     case N: trace("determining number of area to the north"); 
       nextIs = north[currentArea]; 
       break; 
     case E: trace("determining number of area to the east"); 
       nextIs = east[currentArea]; 
       break; 
     case S: trace("determining number of area to the south"); 
       nextIs = south[currentArea]; 
       break; 
     case W: trace("determining number of area to the west"); 
       nextIs = west[currentArea]; 
       break; 
     default: throw new IllegalArgumentException("Direction must be one of " + N + ", " + E + ", " + S + " or " + W); 
    } 

    trace("...nextArea() ends with value for '" + direction + "' of " + nextIs); 

    return nextIs; 
} 

からメソッドです。

     break;  
       case "shoot": 


         break; 
       case "quit": System.out.print("would you like to start again? (yes/no)"); 
          restart = sc.nextLine(); 
          if (restart.equals("yes")) { 
           gameOne.reset(); 
          } 
          else { 
           System.out.println("Thanks for playing"); 
          } 
        break; 

       case "reset": //gameOne.reset(); 
          this.pickDifficulity(); 
         break; 
          } 

}

これは私が正しくあなたの質問を解釈していた場合

//This map is _deliberately_ confusing, although it actually a regular layout 
private int[] east = {1,2,0,4,5,3,7,8,6}; // areas to east of current location (index) 
private int[] west = {2,0,1,5,3,4,8,6,7}; // areas to west of current location (index) 
private int[] north = {6,7,8,0,1,2,3,4,5}; // areas to north of current location (index) 
private int[] south = {3,4,5,6,7,8,0,1,2}; // areas to south of current location (index) 
private int numAreas = south.length;  // number of areas in the "world" 
+0

もう少し詳しく説明できますか?入力が、配列内のインデックスである「場所」のセットであり、各ステップを実行するために取らなければならない指示を返す必要があることを意味しますか? – sprinter

+0

入力は、人が向かいたい場所です。リターンには、ロケーション配列(またはマップ)に従って、どの領域番号を囲むかを示す必要があります。 – Button

+0

入力は、人が向かいたい場所です。リターンには、ロケーション配列(またはマップ)に従って、どの領域番号を囲むかを示す必要があります。 – Button

答えて

0

は、その後、あなたがのリストを変更する方法を求めているマップを持っていGameWorldクラスからメソッドですそれらの位置に到達するために取られなければならないステップへのインデックス(位置を表す)を含む。

あなたはコードではなくヒントを求めてきましたので、いくつかの一般的な情報を提供します。詳細を知りたい場合はお知らせください。

あなたのルートをenumに変換すると、あなたの解決策がわかりやすくなります。そうすれば、方向に関連するすべてのコードがその列挙型の中にあり、switchステートメントを他の場所で避けることができます。例えば

public enum Direction { 
    N(north), S(south), E(east), W(west); 

    private final int[] locations; 
    Direction(int[] locations) { 
     this.locations = locations; 
    } 

    public int next(int current) { 
     return locations[current]; 
    } 
} 

あなたはDirection.valueOf(dir.toUpper())を使用して名前から方向を取得し、dir.next(current)を使用して方向に次の位置を取得することができます。

あなたのコメントは、反対方向に進む必要があると述べています。ポジションを取ってそのポジションにつながるポジションを返します。これには反復が必要です。配列内のその位置を検索する必要があります。この場合も、列挙型のメソッドになります。ここでは例です:

public int previous(int current) { 
    for (int i = 0; i < locations.length; i++) { 
     if (locations[i] == current) 
      return i; 
    } 
    return -1; 
} 

は、それでは北3のヘッド位置がN.previous(3)です。

可能な限りすべての場所を取得するには、地図を場所からその方向に戻すことをお勧めします(現在の場所に移動するには、その場所から移動すると解釈されます)。

Map<Integer,Direction> allPrevious(int location) { 
    Map<Integer,Direction> map = new HashMap<>(); 
    for (Direction dir: Direction.values()) { 
     int prev = dir.previous(location); 
     if (prev >= 0) 
      map.put(prev, dir); 
    } 
} 

あなたは、そのマップを持っていたら、あなたは簡単にすべての以前の位置(map.keySet())と指定された場所(map.get(location))から追随する方向性を得ることができます。

関連する問題