package uk.ac.reading.cs2ja16.rdrohan.Week3;
import java.util.Scanner;
public class AWorld {
private int sizeX = 10; // World size X
private int sizeY = 10; // World size Y
private int maxNumOfEntitys = 5;
public AWorld() { //Constructor with default values
sizeX = 10;
sizeY = 10;
maxNumOfEntitys = 5;
}
public AWorld(int xInput, int yInput, int maxEntityInput) { //Constructor with user defined values
sizeX = xInput;
sizeY = yInput;
maxNumOfEntitys = maxEntityInput;
}
public int getSizeX() { //Get the horizontal size of the world
return sizeX;
}
public int getSizeY() { //Get the vertical size of the world
return sizeY;
}
public static AWorld createWorldFromUserInput() {
Scanner s = new Scanner(System.in); // Scanner for inputs
AWorld worldObject = new AWorld(); // Initialise object for the world
AnEntity[] worldEntityObjects = new AnEntity[worldObject.maxNumOfEntitys]; // Array for the entity objects
System.out.print("What is the max number of entitys in the world? ");
int maxNumOfEntitys = Integer.parseInt(s.nextLine()); // User enters the max num of entitys
for (int i = 0; i < maxNumOfEntitys; i++) {
System.out.print("Enter species: ");
String speciesInput = s.nextLine();
System.out.print("Enter horizontal position: ");
int posXInput = Integer.parseInt(s.nextLine());
while (posXInput > worldObject.getSizeX()) { //Error handling for incorrect input
System.out.println("Entity cannot exist outside the world! Enter horizontal position again:");
posXInput = Integer.parseInt(s.nextLine());
}
System.out.print("Enter vertical position: ");
int posYInput = Integer.parseInt(s.nextLine());
while (posYInput > worldObject.getSizeY()) { //Error handling for incorrect input
System.out
.println("Entity cannot exist outside the world! Enter vertical position again:");
posYInput = Integer.parseInt(s.nextLine());
}
System.out.print("Enter energy: ");
int energyInput = Integer.parseInt(s.nextLine());
System.out.println("Generating Species...\nSpecies Generated!");
System.out.println("Now for the next species...");
worldEntityObjects[i] = new AnEntity(speciesInput, posXInput, posYInput, energyInput); // Store object in object array
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Scanner s = new Scanner(System.in); // Scanner for inputs
//AWorld worldObject = new AWorld(); // Initialise object for the world
// AnEntity[] worldEntityObjects = new AnEntity[worldObject.maxNumOfEntitys]; // Array for the entity objects
// System.out.print("What is the max number of entitys in the world? ");
// worldObject.maxNumOfEntitys = Integer.parseInt(s.nextLine()); // User enters the max num of entitys
// for (int i = 0; i < worldObject.maxNumOfEntitys; i++) {
createWorldFromUserInput();
/*System.out.print("Enter species: ");
String speciesInput = s.nextLine();
System.out.print("Enter horizontal position: ");
int posXInput = Integer.parseInt(s.nextLine());
while (posXInput > worldObject.getSizeX()) { //Error handling for incorrect input
System.out.println("Entity cannot exist outside the world! Enter horizontal position again:");
posXInput = Integer.parseInt(s.nextLine());
}
System.out.print("Enter vertical position: ");
int posYInput = Integer.parseInt(s.nextLine());
while (posYInput > worldObject.getSizeY()) { //Error handling for incorrect input
System.out.println("Entity cannot exist outside the world! Enter vertical position again:");
posYInput = Integer.parseInt(s.nextLine());
}
System.out.print("Enter energy: ");
int energyInput = Integer.parseInt(s.nextLine());
System.out.println("Generating Species...\nSpecies Generated!");
System.out.println("Now for the next species...");
worldEntityObjects[i] = new AnEntity(speciesInput, posXInput, posYInput, energyInput); // Store object in object array */
}
for (int i = 0; i < worldObject.maxNumOfEntitys; i++) // Prints out the details of each object in array
{
System.out.println(worldEntityObjects[i].toText());
System.out.println();
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.