Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1. package buildingpb;
  2.  
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5. import java.util.Random;
  6.  
  7. /**
  8.  * @author shsmchlr
  9.  * Class for a building ... which will have rooms ...
  10.  */
  11. public class Building {
  12.     /**
  13.      * The building in which there are various rooms and objects
  14.      * Its size is defined by xSize,ySize
  15.      * Variables are used for actual rooms
  16.  
  17.      */
  18.     private int xSize = 10;                     // size of building
  19.     private int ySize = 10;
  20.     private ArrayList<Room> allRooms;           // array of rooms
  21.     private Person occupant;       
  22.     private Person occupant2;
  23.     private Person occupant3;
  24.     private Person occupant4;
  25.     private Person occupant5;
  26.     private Person occupant6;
  27.     private Random ranGen;                      // for generating random numbers
  28.  
  29.     /**
  30.      * Construct a building
  31.      */
  32.     public Building(String bs) {
  33.         allRooms = new ArrayList<Room>();       // create space for rooms
  34.         ranGen = new Random();                  // create object for generating random numbers
  35.         setBuilding(bs);                        // now set building using string bs
  36.     }
  37.  
  38.     /**
  39.      * set up the building, as defined in string
  40.      * @param bS        of form xS,yS;x1 y1 x2 y2 xd yd ds;  etc
  41.      *                  xS,yS define size, and for each room have locations of opposite corners, door and door size
  42.      */
  43.     public void setBuilding(String bS) {
  44.         allRooms.clear();
  45.         StringSplitter bSME = new StringSplitter(bS, ";");      // split strings by ;
  46.         StringSplitter bSz = new StringSplitter(bSME.getNth(0, "5 5"), " ");    // split first by space
  47.         xSize = bSz.getNthInt(0, 5);                        // get first of the first string, being xsize
  48.         ySize = bSz.getNthInt(1, 5);
  49.         for (int ct = 1; ct<bSME.numElement(); ct++)        // remaining strings define rooms
  50.             allRooms.add(new Room (bSME.getNth(ct, "")));   // add each in turn
  51.         addPerson();
  52.         addPerson2();  
  53.         addPerson3();
  54.         addPerson4();
  55.         addPerson5();
  56.         addPerson6();// now add a person
  57.     }
  58.     /**
  59.      * On arena size
  60.      * @return size in x direction of robot arena
  61.      */
  62.     public int getXSize() {
  63.         return xSize;
  64.     }
  65.    
  66.     /**
  67.      * On arena size
  68.      * @return size in y direction of robot arena
  69.      */
  70.     public int getYSize() {
  71.         return ySize;
  72.     }
  73.  
  74.     /**
  75.      * set new destination for person and path to it
  76.      * In this version puts person in random room and sets path from there to room's door
  77.      * @param occupant
  78.      */
  79.     void setNewRoom(Person occupant) {
  80.         // at this stage all this does is
  81.         int cRoom = whichRoom(occupant.getXY());
  82.         int dRoom = cRoom;
  83.         while (dRoom == cRoom) dRoom = ranGen.nextInt(allRooms.size()); // get another room randomlt
  84.        
  85.         occupant.clearPath();
  86.         occupant.setXY(allRooms.get(dRoom).getRandom(ranGen));
  87.         occupant.setPath(allRooms.get(dRoom).getByDoor(0));     // position by door
  88.        
  89.         occupant.setStopped(false);                             // say person can move
  90.     }
  91.    
  92.     /**
  93.      * calculate a random room number
  94.      * @return  number in range 0.. number of rooms
  95.      */
  96.     public int randRoom() {
  97.         return ranGen.nextInt(allRooms.size());
  98.     }
  99.     /**
  100.      * create new person and set path for it to follow
  101.      */
  102.     public void addPerson() {
  103.         occupant = new Person (allRooms.get(0).getRandom(ranGen));  // create person in first room
  104.         setNewRoom(occupant);
  105.     }
  106.     public void addPerson2() {
  107.         occupant2 = new Person (allRooms.get(0).getRandom(ranGen)); // create person in first room
  108.         setNewRoom(occupant2);
  109.     }
  110.     public void addPerson3() {
  111.         occupant3 = new Person (allRooms.get(0).getRandom(ranGen)); // create person in first room
  112.         setNewRoom(occupant3);
  113.     }
  114.    
  115.     public void addPerson4() {
  116.         occupant4 = new Person (allRooms.get(0).getRandom(ranGen)); // create person in first room
  117.         setNewRoom(occupant4);
  118.     }
  119.    
  120.     public void addPerson5() {
  121.         occupant5 = new Person (allRooms.get(0).getRandom(ranGen)); // create person in first room
  122.         setNewRoom(occupant5);
  123.     }
  124.    
  125.     public void addPerson6() {
  126.         occupant6 = new Person (allRooms.get(0).getRandom(ranGen)); // create person in first room
  127.         setNewRoom(occupant6);
  128.     }
  129.    
  130.     /**
  131.      * show all the building's rooms and person in the interface
  132.      * @param bi    the interface
  133.      */
  134.     public void showBuilding (BuildingGUI bi) {
  135.         for (Room r : allRooms) r.showRoom(bi);
  136.                 // loop through array of all rooms, displaying each
  137.         occupant.showPerson(bi);
  138.         occupant2.showPerson2(bi);
  139.         occupant3.showPerson3(bi);
  140.         occupant4.showPerson3(bi);
  141.         occupant5.showPerson3(bi);
  142.         occupant6.showPerson3(bi);
  143.     }
  144.    
  145.  
  146.     /**
  147.      * method to update the building
  148.      * Here it just deals with the occupant
  149.      */
  150.     public void update() {
  151.         if (occupant.getStopped()) setNewRoom(occupant); else occupant.update();
  152.         if (occupant2.getStopped()) setNewRoom(occupant2); else occupant2.update();
  153.         if (occupant3.getStopped()) setNewRoom(occupant3); else occupant3.update();
  154.         if (occupant4.getStopped()) setNewRoom(occupant4); else occupant4.update();
  155.         if (occupant5.getStopped()) setNewRoom(occupant5); else occupant5.update();
  156.         if (occupant6.getStopped()) setNewRoom(occupant6); else occupant6.update();
  157.     }
  158.  
  159.     /**
  160.      * method to determine which room position x,y is in
  161.      * @param xy
  162.      * @return  n, the number of the room or -1 if in corridor
  163.      */
  164.     public int whichRoom(Point xy) {
  165.         int ans = -1;
  166.         for (int ct = 0; ct<allRooms.size(); ct++)
  167.             if (allRooms.get(ct).isInRoom(xy)) ans = ct;
  168.         return ans;
  169.     }
  170.    
  171.    
  172.     /**
  173.      * method to return information bout the building as a string
  174.      */
  175.     public String toString() {
  176.         String s = "Building size " + getXSize() + "," + getYSize() + "\n";
  177.         for (Room r : allRooms) s = s + r.toString() + "\n";
  178.         s = s + occupant.toString();
  179.         s = s + occupant2.toString();
  180.         s = s + occupant3.toString();
  181.         s = s + occupant4.toString();
  182.         s = s + occupant5.toString();
  183.         s = s + occupant6.toString();
  184.         return s;
  185.     }
  186.  
  187.     public static void main(String[] args) {
  188.         Building b = new Building ("500 500;0 0 250 250 100 250 20;250 0 450 450 320 450 30;0 300 450 450 300 300 15");     // create building
  189.         System.out.println(b.toString());               // and print it
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement