我是Java的新手。目前,我正忙着一个项目。目标是创建一个包含生物实体(人)和非生命权利(例如vulcanos)的虚拟2D世界。人们应该能够每一步移动(仅限于相邻的细胞)并且可以被非生物体以其他方式杀死或受到影响。
我认为从创建人员课程并将其放入网格开始是个好主意。到目前为止,这些是我的课程。但是,我正在努力将人员存储在网格中(应该可以在一个单元格中存储多个人)。
这是我的人类:
public void setName (String name) {//give the person a name
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {// give the person an age
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) { // set gender of the person
this.gender = gender;
}
public String getSexualPreference() {
return sexualPreference;
}
public void setSexualPreference(String sexualPreference) { // set sexual preference of the person
this.sexualPreference = sexualPreference;
}
public String getLifeGoals() {
return lifeGoals;
}
public void setLifeGoals(String lifeGoals) { // set lifegoals of the person
this.lifeGoals = lifeGoals;
}
public boolean isMarried() {
return isMarried;
}
public void setMarried(boolean isMarried) {
this.isMarried = isMarried;
}
public int getAgressionLevel() {
return agressionLevel;
}
public void setAgressionLevel(int agressionLevel) {
this.agressionLevel = agressionLevel;
}
public Terrain get_terrain(){// specify terrain in which the person lives.
return (Terrain) get_terrain();
}
public Person(boolean randomAge){ //creates a person with a random age and a agression level.
if(randomAge==true){
setAge(ThreadLocalRandom.current().nextInt(0,max_age +1));// this gives the person a random age between 0 and the chosen maximum age.
setAgressionLevel(ThreadLocalRandom.current().nextInt(0,max_agressionLevel +1));//this gives the person a random agression level, scaled from 0-10.
}else{
setAge(0);
}
}
public void act(){ // this method specifies what a person can do all the time
increaseAge();
if(isAlive() == true)
{
int births = reproduce();
for(int b = 0; b < births; b++)
{
Location place = get_terrain().random_space(getX(), getY());
if(place != null)
{
Person newpers = new Person(false);
get_terrain().addObject(newpers, place.getX(), place.getY());
}
}
}
}
private int reproduce(){
int births = 0;
if(canReproduce() && isMarried() && Math.random()*100 <= reproducing_prob){
births +=1;
} return births;
}
private void increaseAge(){// increases the person's age, this can result in its dead
setAge(getAge() + 1);
if(getAge() > max_age)
{
setDead();
}
}
private boolean canReproduce(){
return getAge() >= adultAge;
}
public String toString()
{
return "Person, age " + getAge();
这是我的地形类。我试图将元素放在随机的地方。
import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
public class Terrain {
private static int width;
private static int height;
private final static int nrOfPersons = 200;
public Terrain()
{
addPerson(null, null, null);
}
public static void addPerson(Person p, Location x){ //adds the persons to the terrain
ArrayList<Person> listOfPersons = new ArrayList<Person>();
Terrain.addPerson(p, x);
}
public Location random_space(int x, int y){
int x1 = ThreadLocalRandom.current().nextInt(0, width +1);
int y1 = ThreadLocalRandom.current().nextInt(0, height +1);
if(x1 < 0 || x1 >= width || y1 < 0 || y1 >= height) //check if the location is within the terrain
{
System.out.println("Your location is not within the boundaries of the world");
}
return random_space(x1, y1);
}
这是我的爱情课:
public class Location { //corresponds to a place on the terrain
private int x; //horizontal coordinate
private int y; //vertical coordinate
/**
* Constructor for objects of class place
*/
public Location(int x, int y)
{
this.x = x;
this.y = y;
}
public boolean equals(Object obj)
{
if(obj instanceof Location)
{
Location other = (Location)obj;
return x == other.getX() && y == other.getY();
}
else
{
return false;
}
}
public int getX() //return x coordinate
{
return x;
}
public int getY() //return y cordinate
{
return y;
}
}
答案 0 :(得分:2)
这当然是假的:
public static void addPerson(Person p, Location x){ //adds the persons to the terrain
ArrayList<Person> listOfPersons = new ArrayList<Person>();
Terrain.addPerson(p, x);
}
每次都不应该重新创建listOfPersons:它会在结束时清除并销毁它:然后你什么也不做。
1在构造函数中执行此操作:
List<Person> listOfPersons = new ArrayList<Person>();
public Terrain()
{
// WHAT ?
// addPerson(null, null, null);
}
2 addPerson =&gt; addPerson执行无限循环
你也必须保留这些信息。
最简单的方法是Map:Location =&gt;人
Map<Location,Person> grid=new HashMap<Location,Person>();
你这样说:
grid.put(one_location,one_person);
你这样测试:
grid.contains(one_location); => get true/false
你是这样的:
grid.get(one_location); => get one_person
重要的是:你可以在一个地方只有一个人,有一张地图。有了这个,你就覆盖了老人。
顺便说一句,你不再需要listOfPersons了。将多个人留在同一地点的其他策略:
获取地图:Person =&gt;地点。当你需要某个地方的人时,你必须进行迭代。
其他可能性:还要保留Map&lt;位置,设置&LT;人&GT;&GT; :对于每个位置,您都有设置。
答案 1 :(得分:2)
addPerson
方法应该是Terrain类的实例方法,而不是static
方法。此外,它应该作用于类的字段而不是方法的局部变量。
您还需要一种将人物连接到某个位置的方法 - 可以是很多东西,表示整个网格的二维数组,从坐标到人物对象的地图,或{{1}人物对象上的字段。让我们假设您使用地图,那么它看起来就像这样......
location
将人员与位置连接起来的最佳机制实际上取决于您在其他计划中尝试实现的目标。