2016-04-27 12 views
-4

私はテキストベースの「リアルライフのゲーム」プログラムを作成しています。私は、ゲームボード(明確な開始点/終了点を持つリニアゲームボード)の各タイルによって与えられる指示を実行する方法を割り当てる効率的な方法を見つけることに苦労しています。ゲーム内の各プレイヤーは、さまざまなゲームの統計情報(ボード上のプレイスホルダーの値を含む)を含む変数「プレーヤー」のオブジェクトで表され、すべてのプレーヤーオブジェクトはハッシュマップに含まれます。各プレイヤーオブジェクトを循環するループを作成し、それぞれのプレースホルダー値に基づいて、ボードのそのタイルの特定のメソッドを参照したいと思います。どのようにしてクラスメソッドをHashMap内の値に割り当てることができますか?

私のメインクラス:

import java.util.Scanner; 
import java.util.HashMap; 

public class Main { 

public static void main(String[] args) { 
    Coin coin = new Coin(); 
    Die die = new Die(); 
    Scanner choice = new Scanner(System.in); 

    System.out.println("Choose player amount:"); 
    int n = choice.nextInt(); 

    /*This map contains master list of all players and player values*/ 
    HashMap<Integer, Player> playerList = new HashMap<Integer, Player>(); 

    /*This map contains just the true_false value of life for each player*/ 
    HashMap<Integer, Boolean> lifeCheck = new HashMap<Integer, Boolean>(); 

    /*This section defines number/sex/name of each player*/ 
    for (int y = 1; y <= n; y++) { 
     System.out.println("New player now flips a coin."); 
     coin.flip(); 
     if (coin.get() == true) { 
      System.out.println("You flipped heads! You are a man."); 
     } else { 
      System.out.println("You flipped tails! You are a woman."); 
     } 

     System.out.println("What is your name?"); 
     String user = choice.next(); 
     playerList.put(y, new Player(user)); 

     /*This adds a check functionality to ensure live players still exist*/ 
     lifeCheck.put(y, playerList.get(y).pulse()); 
    } 

    /*This invokes a new instance of our game board*/ 
    GameSpace gameBoard = new GameSpace();/*Not yet defined*/ 

    /*This do_while loop checks that a live player exists after each turn*/ 
    do { 
     /*This is where I think I would be putting the majority of 
        the game code*/ 

    } while (lifeCheck.containsValue(true)); 

    /*This tests to see if my player naming loop works properly*/ 
    /* 
    int x = 1; 
    for (int i = 1; i <= n; i++) { 
     System.out.println("Player number " + x + " is named " + playerList.get(i).name); 
     x++; 
    } 
    */ 





    choice.close(); 
} 
} 

マイプレイヤークラス:

public class Player { 
public boolean gender; 
public String name; 
public int wealthClass; 
public double income; 
public double wealth; 
public double health; 
public boolean parent = false; 
public boolean disabled = false; 
public boolean alive = true; 
public double placeHold = 1; 

public Player(String playerName) { 
    name = playerName; 
} 

public boolean getGender() { 
    return gender; 
} 
public String getName() { 
    return name; 
} 
public int getCaste() { 
    return wealthClass; 
} 
public double getIncome() { 
    return income; 
} 
public double getWealth() { 
    return wealth; 
} 
public double getHealth() { 
    return health; 
} 
public boolean getParent() { 
    return parent; 
} 
public boolean getDisabled() { 
    return disabled; 
} 
public boolean pulse() { 
    return alive; 
} 
public double getPosition() { 
    return placeHold; 
} 

public void move() { 
    Die die1 = new Die(); 
    die1.roll(); 
    placeHold += die1.get(); 
    System.out.println("You moved forward " + die1.get() + " spaces."); 
} 

public void main(String[] args) { 
    if (health <= 0) { 
     alive = false; 
    } 
} 
} 

マイコインクラス:ここから行くための最も論理的な場所でしょう

public class Coin { 
private boolean face; 

public Coin() { 
    flip(); 
} 
public void flip() { 
    double x = (Math.floor(Math.random()*2)); 
    if (x == 0) { 
     face = true; 
    } else { 
     face = false; 
    } 
} 
public boolean get() { 
    return face; 
} 
} 

+1

完全で*最小限の例を提供してください。ほとんどのコードはあなたの問題とは無関係のようですが、私たちが作業しなければならないのはあなたのタイトルと最後の文だけです。あなたの問題が何であるか、なぜハッシュマップと "プレースホルダ値"で何かをしたいのかは不明です。 – Zong

+0

PlayerクラスのplaceHold整数値を使用して、それぞれのゲームタイルのメソッドをアクティブにするHashMapキーを直接参照します。それは私が見ることができる私の目標を達成するための最善の方法ですが、私はそれをうまく実装することができませんでした。 – SenorHoward

答えて

0

のHashMapの値が呼び出したいメソッドの名前である場合は、リフレクションを使用することができるはずです:私は質問を理解していれば

HashMap<Integer, String> map = new HashMap<>(); 
map.put(0, "methodName1"); 
map.put(1, "methodName2");  

String methodName = map.get(player.getPosition()); 
Method method = Player.class.getDeclaredMethod(methodName); 
method.invoke(); 
0

、あなたが関連付けられたメソッドを格納する方法を求めていますボード上の各位置は、その位置で何をするかを指定します。もしそうなら、私はそれをその位置にカプセル化することをお勧めします。それは最高のインターフェイスとして定義されます:

interface Position { 
    void operateOnPlayer(Player player); 
} 

次に、あなたのボードは、Position実装に開始からの距離からマップ次のようになります。

Map<Integer,Position> board; 

は、Java 8を使用している場合は、あなたもすることができます位置を定義するためにラムダを使用します。それは、Javaの以前のバージョンで同じようにうまく動作します

board.put(17, player -> player.moveBack(4)); 
board.put(16, player -> player.skipATurn()); 

あなただけの(潜在的に匿名)の実装を作成する必要があります。これらの呼び出し

は、非常に簡単です:

for (Player player: players) 
    board.get(player.getPosition()).operateOnPlayer(player); 

または、より良い、あなたは位置フィールドを公開する必要はありませんので、Playerクラスにそれを追加します。

class Player { 
    public takeTurn() { 
     board.get(position).operateOnPlayer(this); 
    } 
}    

ありますされていますConsumer<Player>へのマッピングなどの他のメカニズムがありますが、明示的なインターフェイスを定義するのと同じように優れた設計とは思えません。

関連する問題