コード例 #1
0
 /** Tests the move method in the organism class */
 @Test
 public void testMove() {
   Cell middle = new Cell(2, 2);
   system.add(rabbit, middle);
   rabbit.move(middle, system);
   system.findFirstEmptyNeighbor(middle, 0);
   String now = "xxMxxxxxx";
   assertEquals(now, printX());
 }
コード例 #2
0
 /** Tests the eat method in the organism class */
 @Test
 public void testEat() {
   Cell middle = new Cell(2, 2);
   Cell west = new Cell(1, 2);
   system.add(rabbit, middle);
   system.add(insect, west);
   rabbit.eat(middle, system);
   String now = "xxxxxMxxx";
   assertEquals(now, printX());
 }
コード例 #3
0
  public void move() {
    // remove from current ecosystem
    getParent().remove(this);
    // get the adjacent ecossytems
    ArrayList<Ecosystem> destinations = getAdjacent();

    // randomly select a neighbouring ecosystem
    int choice = (int) (Math.random() * destinations.size());
    // set the destination as the new parent, and add self to the destination
    Ecosystem newparent = destinations.get(choice);
    newparent.add(this);
    setParent(newparent);
  }
コード例 #4
0
 /**
  * Private method that prints the current ecosystem grid as a string
  *
  * @return the string printout
  */
 private String printX() {
   String s = "";
   for (int i = 0; i < 3; i++)
     for (int j = 0; j < 3; j++) {
       Item x = system.getItemAt(new Cell(i, j));
       if (x != null) s += x.getSymbol();
       else s += "x";
     }
   return s;
 }
コード例 #5
0
 /** Tests the breed method in the organism class */
 @Test
 public void testBreed() {
   assertEquals(0, rabbit.getTimeSinceLastBreed());
   rabbit.incrementTimeSinceLastBreed();
   rabbit.incrementTimeSinceLastBreed();
   rabbit.incrementTimeSinceLastBreed();
   rabbit.incrementTimeSinceLastBreed();
   rabbit.incrementTimeSinceLastBreed();
   rabbit.incrementTimeSinceLastBreed();
   rabbit.incrementTimeSinceLastBreed();
   assertEquals(7, rabbit.getTimeSinceLastBreed());
   Cell middle = new Cell(2, 2);
   Cell west = new Cell(1, 2);
   system.add(rabbit, middle);
   assertEquals(true, rabbit.breed(middle, system));
 }