2016-04-27 34 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

請嘗試提供一個完整的*最小*示例。大部分代碼似乎與您的問題無關,我們唯一需要處理的是您的標題和最後一句。目前還不清楚你的問題到底是什麼,你爲什麼想用hashmaps和「佔位符值」做些什麼。 – 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); 
} 

那麼你的主板成爲與距離的地圖,從一開始的位置實現:

Map<Integer,Position> board; 

如果您使用的是Java 8,那麼你甚至可以使用lambda表達式定義的位置:在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>,但我真的不認爲它們與定義顯式接口一樣好;

相關問題