Sono un po 'nuovo in Java. Attualmente sono impegnato con un progetto. L'obiettivo è creare un mondo virtuale in 2D con entità viventi (persone) e entità non viventi (ad esempio vulcanos). Le persone dovrebbero essere in grado di muovere ogni passo temporale (solo per le celle adiacenti) e possono essere uccise o influenzate in altri modi dalle entità non viventi.Aggiungi oggetti persona alla griglia bidimensionale
Ho pensato che fosse una buona idea iniziare con la creazione di una classe di persone e inserirle nella griglia. Queste sono le mie classi finora. Tuttavia, sto lottando con l'archiviazione di persone nella griglia (dovrebbe essere possibile memorizzare più persone in una cella).
Questa è la mia classe person:
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();
Questa è la mia classe di terreno. Sto cercando di mettere gli elementi in posti casuali.
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);
}
E questa è la mia classe lovation:
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;
}
}
quindi, qual è esattamente il problema, cosa non funziona? –
Non so come aggiungere oggetti persona alla mia griglia – StievieG
cosa è "la griglia"? hai già un terreno, con il vuoto statico pubblico addPerson –