Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AnEntity {
- static int lastID = -1;
- AWorld residence;
- String species;
- char symbol;
- int id;
- int xPos;
- int yPos;
- int energy;
- public String getSpecies() {
- return species;
- }
- public void setSpecies(String species) {
- this.species = species;
- }
- public char getSymbol(){
- return symbol;
- }
- public void setsymbol(char symbol)
- {
- this.symbol = symbol;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getX() {
- return xPos;
- }
- public void setX(int xPos) {
- this.xPos = xPos;
- }
- public int getY() {
- return yPos;
- }
- public void setY(int yPos) {
- this.yPos = yPos;
- }
- public int getEnergy() {
- return energy;
- }
- public void setEnergy(int energy) {
- this.energy = energy;
- }
- public String toString() {
- return species + "(" + xPos + "," + yPos + ")";
- }
- public String toText() {
- return id + " " + toString() + " Energy: " + energy;
- }
- AnEntity() {
- species = null;
- id = -1;
- xPos = -1;
- yPos = -1;
- energy = -1;
- };
- AnEntity(String _species, char _symbol, int _energy, int _xPos, int _yPos) {
- species = _species;
- symbol = _symbol;
- energy = _energy;
- xPos = _xPos;
- yPos = _yPos;
- lastID++;
- id = lastID;
- }
- }
- class AWorld {
- int xSize;
- int ySize;
- int currentEntities;
- AnEntity entities[];
- char[] symbolArray;
- public int getX() {
- return xSize;
- }
- public void setX(int xSize) {
- this.xSize = xSize;
- symbolArray = new char[xSize*ySize];
- }
- public int getY() {
- return ySize;
- }
- public void setY(int ySize) {
- this.ySize = ySize;
- symbolArray = new char[xSize*ySize];
- }
- public int getSize() {
- return entities.length;
- }
- public boolean setSize(int size) {
- if(size > xSize * ySize)
- {
- System.out.println("Maximum number of entities exceeds the size of the world.");
- return false;
- }
- entities = new AnEntity[size];
- return true;
- }
- public char[] getSymbolArray() {
- for(int i = 0; i < symbolArray.length; i++)
- {
- symbolArray[i] = ' ';
- }
- for (int i = 0; i < currentEntities; i++)
- {
- if(!(entities[i].getY() < 0 || entities[i].getX() < 0))
- {
- symbolArray[ (xSize * entities[i].getY()) + entities[i].getX()] = entities[i].getSymbol();
- }
- }
- return symbolArray;
- }
- public boolean addEntity(String species, char symbol, int energy, int x, int y) {
- if(currentEntities < entities.length) {
- if(x < xSize && y < ySize) {
- AnEntity entityToAdd = new AnEntity(species, symbol, energy, x, y);
- entities[currentEntities] = entityToAdd;
- currentEntities++;
- return true;
- }
- else
- {
- System.out.println("Co-ordinates of entity outside of maximum bounds!");
- }
- }
- else
- {
- System.out.println("Entities limit reached! Cannot add any more entities.");
- }
- return false;
- }
- public String[] getList() {
- String list[] = new String[currentEntities];
- for (int i = 0; i < currentEntities; i++)
- {
- list[i] = entities[i].toText();
- }
- return list;
- }
- public boolean fromText(String text) {
- String[] input = text.split(" ", 5);
- int x = Integer.parseInt(input[0]);
- int y = Integer.parseInt(input[1]);
- int foodNumber = (Integer.parseInt(input[2])*100)/(x*y);
- int obstNumber = (Integer.parseInt(input[3])*100)/(x*y);
- String[] input2 = input[4].split(" ");
- int total = 0;
- for (int i = 1; i < input2.length; i+=2)
- {
- total += Integer.parseInt(input2[i]);
- }
- total += foodNumber;
- total += obstNumber;
- if(total > entities.length)
- {
- System.out.println("This would require " +total+ " entities, whereas the maximum number that can be created is " + entities.length);
- return false;
- }
- xSize = x;
- ySize = y;
- for (int i = 0; i < foodNumber; i++)
- {
- addEntity("food", 'f', 5, -1, -1);
- }
- for (int i = 0; i < obstNumber; i++)
- {
- addEntity("obstacle", 'O', -1, -1, -1);
- }
- for (int i = 0; i < input2.length; i+=2)
- {
- for (int j = 0; j < Integer.parseInt(input2[i+1]); j++)
- {
- addEntity(input2[i], input2[i].charAt(0), 0, -1, -1);
- }
- }
- return true;
- }
- public
- AWorld() {
- xSize = 0;
- ySize = 0;
- currentEntities = 0;
- entities = null;
- symbolArray = null;
- }
- AWorld(int x, int y, int size) {
- xSize = x;
- ySize = y;
- currentEntities = 0;
- symbolArray = new char[x*y];
- if(size <= x*y) {
- entities = new AnEntity[size];
- }
- else {
- entities = new AnEntity[x*y];
- System.out.println("Maximum number of entities exceeds the size of the world. Size has been set to the maximum, " + entities.length);
- }
- }
- }
- import javax.swing.JOptionPane;
- class AnInterface {
- AWorld theWorld;
- public void addEntity(){
- String inputHolder = JOptionPane.showInputDialog("Please enter the entity species name, symbol, energy, x position, and y position; separated with spaces.");
- String[] input = inputHolder.split(" ");
- theWorld.addEntity(input[0], input[1].charAt(0), Integer.parseInt(input[2]), Integer.parseInt(input[3]), Integer.parseInt(input[4]));
- }
- public void configure(){
- String input = JOptionPane.showInputDialog("Please enter x size, y size, %food, %obstacles, and species plus the number");
- theWorld.fromText(input);
- }
- public void display(){
- char symbolArray[] = theWorld.getSymbolArray();
- String toPrint = "/";
- for (int i = 0; i < theWorld.getX(); i++)
- {
- toPrint += "-";
- }
- toPrint += "\\\n";
- for(int i = 0; i < theWorld.getY(); i++)
- {
- toPrint += "|";
- toPrint += ( new String( symbolArray, i*theWorld.getX(),theWorld.getX() ) );
- toPrint += "|\n";
- }
- toPrint += "\\";
- for (int i = 0; i < theWorld.getX(); i++)
- {
- toPrint += "-";
- }
- toPrint += "/\n";
- System.out.print(toPrint);
- }
- public void list(){
- String printList[] = theWorld.getList();
- for (int i = 0; i < printList.length; i++)
- {
- System.out.println(printList[i]);
- }
- }
- public static void main(String[] args)
- {
- AnInterface iface = new AnInterface();
- iface.theWorld = new AWorld(20,20,25);
- iface.configure();
- iface.list();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement