2016-04-27 48 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>,但我真的不认为它们与定义显式接口一样好;

相关问题