英文:
Creating array of objects; Best way to do it?
问题
下午好。
我正试图制作一个受《Among Us》启发的小游戏。如果有7名或更多玩家,游戏将有两名歹徒;如果玩家数量为6名或更少,则游戏只有一名歹徒。
然而,我遇到了一个问题,因此我正在尝试创建多个构造函数,以下是我设想的布局。
控制台 -> 通过逗号分隔输入名称 -> String[] 名称 -> newPlayer 构造函数
然后,newPlayer 构造函数将确定玩家数量,并将它们分类到各自的构造函数中,使用 Math.random 来确定歹徒。
歹徒路径
newPlayer 构造函数 -> 歹徒构造函数 -> 玩家构造函数
牛仔路径
newPlayer 构造函数 -> 歹徒构造函数 -> 玩家构造函数
问题在于,将数据传递给玩家构造函数后,我想创建一个 Player[] 类型的数组,这样我就可以轻松地访问其中的数据,而不需要创建单独的玩家变量,然后使这个数组对包中的其他类可访问。我正在寻找最简洁的方法来做到这一点。
尽管我觉得这可能不需要代码来提供上下文,但为了回答我的问题,我还是会提供一些代码。
歹徒类
package Sorting;
public class Bandit {
private int playerID;
private String playerName;
public Bandit(int ID, String name) {
this.playerName = name;
this.playerID = ID;
Player newBandit = new Player(playerID,true,playerName);
}
}
牛仔类
package Sorting;
public class Cowboy {
private int playerID;
private String playerName;
public Cowboy(int ID, String name) {
this.playerID = ID;
this.playerName = name;
Player newCowboy = new Player(this.playerID,false,this.playerName);
}
}
新玩家类
package Sorting;
public class NewPlayer {
private String name;
private int playerID;
private int impostor1, impostor2;
NewPlayer (int ID, String name, int playerCount) {
// ... (你的代码在这里)
}
}
玩家类
package Sorting;
public class Player {
public int playerCount;
private int playerID;
private boolean playerBandit;
private String playerName;
public Player(int id, boolean bandit, String name) {
// ... (你的代码在这里)
}
}
我认为 Tester 类可能不需要来回答这个问题,但我还是会提供它。请注意,这部分代码还没有完成。
package Sorting;
import java.util.*;
public class Tester {
private static int playerCount;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter player names, separated by a comma: ");
String nameList[] = input.nextLine().split(",");
playerCount = nameList.length;
Assigner.Assignment(nameList);
}
public int getPlayerCount() {return playerCount;}
}
我希望在 Tester 类中创建一个 Player 数组,并使其对包中的其他类可访问。
最佳方法是什么?
英文:
Good afternoon.
I am attempting to make a small game inspired by Among Us. The game will feature two bandits if there are 7 or more players, and one bandit if the player count is 6 or less.
I have run into a problem though, so I am attempting to create multiple constructors, here is how I imagine the layout to be.
Console -> Input names separated by comma -> String[] names -> newPlayer Constructor
The newPlayer constructor will then determine the number of players, and sort them into their respective constructor, using Math.random to determine the bandits.
Bandit route
newPlayer Constructor -> Bandit Constructor -> Player Constructor
Cowboy route
newPlayer Constructor -> Bandit Constructor -> Player Constructor
The problem is that after the data is given to the Player constructor, I'd like to create an array of type Player[], so I can easily access the data within, without creating invidual Player variables, and then making this array accessible to the rest of the classes within the package. I'm looking for the most concise way to do it.
Although I feel like this may not require code for context, I'll provide it in case it helps answer my question.
Bandit Class
package Sorting;
public class Bandit {
private int playerID;
private String playerName;
public Bandit(int ID, String name) {
this.playerName = name;
this.playerID = ID;
Player newBandit = new Player(playerID,true,playerName);
}
}
Cowboy Class
package Sorting;
public class Cowboy {
private int playerID;
private String playerName;
public Cowboy(int ID, String name) {
this.playerID = ID;
this.playerName = name;
Player newCowboy = new Player(this.playerID,false,this.playerName);
}
}
newPlayer Class
package Sorting;
public class NewPlayer {
private String name;
private int playerID;
private int impostor1, impostor2;
NewPlayer (int ID, String name, int playerCount) {
if(playerCount<=6) {int impostor1 = (int)Math.random()*playerCount;
if(impostor1==this.playerID) {
Bandit bandit1 = new Bandit (this.playerID, this.name);}
}
else {
impostor1 = (int)Math.random()*playerCount;
impostor2 = (int)Math.random()*playerCount;
while (impostor1 == impostor2) {
impostor2 = (int)Math.random()*playerCount;
}
if(impostor1==this.playerID) {
Bandit bandit1 = new Bandit (this.playerID, this.name);}
if(impostor2==this.playerID) {
Bandit bandit2 = new Bandit (this.playerID, this.name);}
}
for(int i=0;i<playerCount;i++) {
if ((this.playerID!=impostor1)&&(this.playerID!=impostor2)) {
Cowboy newCowboy = new Cowboy(this.playerID,this.name);
}
}
}
}
Player Class
package Sorting;
public class Player {
public int playerCount;
private int playerID;
private boolean playerBandit;
private String playerName;
public Player(int id, boolean bandit, String name) {
this.playerID = id;
this.playerBandit = bandit;
this.playerName = name;
playerCount++;
}
public String getPlayerName() {return this.playerName;}
public boolean getBandit() {return this.playerBandit;}
public int getPlayerID() {return this.playerID;}
}
I do not think the Tester class would be required to answer this question, but I'll provide it. Be aware, this part is not finished.
package Sorting;
import java.util.*;
public class Tester {
private static int playerCount;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter player names, separated by a comma: ");
String nameList[] = input.nextLine().split(",");
playerCount = nameList.length;
Assigner.Assignment(nameList);
}
public int getPlayerCount() {return playerCount;}
}
I'd like the array of Players to be created within the tester class, and accessible to the rest of the classes within the package.
What is the best way to do this?
答案1
得分: 1
"I'd like the array of Players to be created within the tester class":
我建议使用 List 接口代替原生的 Java 数组(使用起来更方便)。类似这样:List<Player> players = new ArrayList<>()
,然后你可以使用 players.add( /*player*/ )
和 players.get(/*idx*/)
来添加和检索。
"accessible to the rest of the classes within the package":
默认类属性对于同一包内的类是可访问的。像这样:List<Player> players;
或者已经初始化:List<Player> players = new ArrayList<>();
。不要使用任何修饰符如 private
、public
或者 protected
,这将意味着该属性具有 "default" 访问权限(在同一包内)。参考:https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
英文:
"I'd like the array of Players to be created within the tester class": I would suggest using List interface instead of native java array (much easier to use). Something like this: List<Player> players = new ArrayList<>()
and you can use players.add( /*player*/ )
and players.get(/*idx*/)
to add and retrieve.
"accessible to the rest of the classes within the package": a default class property is accessible for classes within the same package. Like this: List<Player> players;
or already initialized: List<Player> players = new ArrayList<>();
. Don't use any modifier like private
, public
or protected
and it will mean this property is "default" access (within same package). See: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论