MyWorld

package uk.ac.reading.cs2ja16.rdrohan; import java.util.Random; import java.util.Scanner; public class AWorld { private int xSize; private int ySize; private int maxEntities; String speciesName; private int numOfEntities = 0; private AnEntity[] allEntities; //char symbol; AWorld() { xSize = 5; ySize = 5; maxEntities = 3; allEntities = new AnEntity[maxEntities]; } AWorld(int xSizeInput, int ySizeInput, int maxEntityInput) { xSize = xSizeInput; ySize = ySizeInput; maxEntities = maxEntityInput; allEntities = new AnEntity[maxEntities]; } public void addEntity() { if(numOfEntities < maxEntities) { Scanner reader = new Scanner(System.in); System.out.print("Enter species name:"); speciesName = reader.nextLine(); //symbol = speciesName.charAt(0); System.out.print("Enter horizontal position: "); int posXInput = Integer.parseInt(reader.nextLine()); while (posXInput > getxSize()) { //Error handling for incorrect input System.out.println("Entity cannot exist outside the world! Enter horizontal position again:"); posXInput = Integer.parseInt(reader.nextLine()); } System.out.print("Enter vertical position: "); int posYInput = Integer.parseInt(reader.nextLine()); while (posYInput > getySize()) { //Error handling for incorrect input System.out .println("Entity cannot exist outside the world! Enter vertical position again:"); posYInput = Integer.parseInt(reader.nextLine()); } System.out.print("Energy: "); int energyInput = Integer.parseInt(reader.nextLine()); allEntities[numOfEntities] = new AnEntity(speciesName, posXInput,posYInput,energyInput); numOfEntities++; } } public int getRobotAt(int x, int y) { /** * search array of robots to see if there is a robot at x,y * @return -1 if no robot, or location where robot is */ int ans = -1; // assume not found for (int i=0; i<numOfEntities; i++) // search all existing robots if (allEntities[i] != null) // check item in array not null if (allEntities[i].isHere(x,y)) ans = i; // if found, return index of found object return ans; // return the answer found } public int getxSize() { return xSize; } public void setxSize(int xSize) { this.xSize = xSize; } public int getySize() { return ySize; } public void setySize(int ySize) { this.ySize = ySize; } public int getMaxEntities() { return maxEntities; } public void setMaxEntities(int maxEntities) { this.maxEntities = maxEntities; } public void displayEntities() { for (int i = 0; i < maxEntities; i++) { System.out.println(allEntities[i].toText() + "\n"); // Outputs the entities attributes } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); AWorld world = new AWorld(); System.out.print("Max number of entities?: "); int maxNumEntities = Integer.parseInt(input.nextLine()); world.setMaxEntities(maxNumEntities); System.out.print("x size: "); int xSize = Integer.parseInt(input.nextLine()); world.setxSize(xSize); System.out.print("y size: "); int ySize = Integer.parseInt(input.nextLine()); world.setySize(ySize); for (int i =0 ; i< maxNumEntities; i++) { world.addEntity(); } world.displayEntities(); } public void showRobots(AnInterface r) { /** * show all the robots in the interface */ for (int ct=0; ct<numOfEntities; ct++) allEntities[ct].displayEntity(r); // loop through array of all robots, displaying each } }

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.