我正在创建一个基于文本的“现实生活游戏”计划。我正在努力寻找一种有效的方法来分配方法,这些方法将执行游戏板的每个贴图(具有确定的开始/结束点的线性游戏板)给出的指示。游戏中的每个玩家都由一个包含不同游戏统计变量(包括他们在棋盘上的位置的占位符值)的类“玩家”的对象来表示,并且每个玩家对象都包含在一个哈希映射中。我想创建一个循环,循环遍历每个玩家对象,并根据各自的占位符值,为该棋盘的该图块引用特定的方法。如何将类方法分配给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;
}
}
会在哪里,从这里去最合理的地方?
请尝试提供一个完整的*最小*示例。大部分代码似乎与您的问题无关,我们唯一需要处理的是您的标题和最后一句。目前还不清楚你的问题到底是什么,你为什么想用hashmaps和“占位符值”做些什么。 – Zong
我想使用Player类的placeHold整数值来直接引用一个HashMap键,该键激活相应游戏图块的方法。这是实现我可以看到的目标的最佳方式,但我无法成功实施。 – SenorHoward