如何将类方法分配给HashMap中的值?

时间:2016-04-27 22:42:05

标签: java loops hashmap

我正在创建一个基于文本的"现实生活中的游戏"程序。我正在努力寻找一种有效的方法来分配方法,这些方法将执行游戏板(具有明确的开始/结束点的线性游戏板)给出的指示。游戏中的每个玩家都由一个类的对象表示" Player"包含不同游戏统计数据的变量(包括其在棋盘上的位置的占位符值),并且每个玩家对象都包含在哈希映射中。我想创建一个循环,循环遍历每个播放器对象,并根据相应的占位符值,引用该板块的特定方法。

我的主要课程:

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;
}
}

离这里最合乎逻辑的地方在哪里?

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

如果我理解了这个问题,你就会问如何存储与棋盘上每个位置相关的方法,指明在该位置做什么。如果是这样,我建议你把它封装在这个位置。最好将其定义为接口:

Map<Integer,Position> board;

然后你的电路板成为从开始到位置实施的距离的地图:

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

如果您使用的是Java 8,那么您甚至可以使用lambdas来定义位置:

Position

它在以前的Java版本中也可以正常工作,您只需要创建for (Player player: players) board.get(player.getPosition()).operateOnPlayer(player); 的(可能是匿名的)实现。

调用这些是非常简单的:

Player

或者,更好的是,将其添加到class Player { public takeTurn() { board.get(position).operateOnPlayer(this); } } 课程,这样您就不需要公开位置字段了:

Consumer<Player>

还有其他机制,例如映射到{{1}},但我真的不认为它们与定义显式接口一样好;