我正在創建一個基於文本的「現實生活遊戲」計劃。我正在努力尋找一種有效的方法來分配方法,這些方法將執行遊戲板的每個貼圖(具有確定的開始/結束點的線性遊戲板)給出的指示。遊戲中的每個玩家都由一個包含不同遊戲統計變量(包括他們在棋盤上的位置的佔位符值)的類「玩家」的對象來表示,並且每個玩家對象都包含在一個哈希映射中。我想創建一個循環,循環遍歷每個玩家對象,並根據各自的佔位符值,爲該棋盤的該圖塊引用特定的方法。如何將類方法分配給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