私はテキストベースの「リアルライフのゲーム」プログラムを作成しています。私は、ゲームボード(明確な開始点/終了点を持つリニアゲームボード)の各タイルによって与えられる指示を実行する方法を割り当てる効率的な方法を見つけることに苦労しています。ゲーム内の各プレイヤーは、さまざまなゲームの統計情報(ボード上のプレイスホルダーの値を含む)を含む変数「プレーヤー」のオブジェクトで表され、すべてのプレーヤーオブジェクトはハッシュマップに含まれます。各プレイヤーオブジェクトを循環するループを作成し、それぞれのプレースホルダー値に基づいて、ボードのそのタイルの特定のメソッドを参照したいと思います。どのようにしてクラスメソッドを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;
}
}
?
完全で*最小限の例を提供してください。ほとんどのコードはあなたの問題とは無関係のようですが、私たちが作業しなければならないのはあなたのタイトルと最後の文だけです。あなたの問題が何であるか、なぜハッシュマップと "プレースホルダ値"で何かをしたいのかは不明です。 – Zong
PlayerクラスのplaceHold整数値を使用して、それぞれのゲームタイルのメソッドをアクティブにするHashMapキーを直接参照します。それは私が見ることができる私の目標を達成するための最善の方法ですが、私はそれをうまく実装することができませんでした。 – SenorHoward