@@ -1,255 +1,377 @@
import java.util.ArrayList;
import java.util.List;

/**
* one Field of the grid pattern of a forest
*/
public class ForestField {

private BugColony colony = null;
private ForestField left = null;
private ForestField right = null;
private ForestField up = null;
private ForestField down = null;
private BugColony colony = null;
private ForestField left = null;
private ForestField right = null;
private ForestField up = null;
private ForestField down = null;

/**
* Verifies if all eight neighboring fields of this field are free in terms
* of not being infested by bugs. If the field is at the edge of the
* forest, i.e. has less than eight neighbors, the non-existing fields are
* counted as non-free.
* Verifies if at least one of the eight neighboring fields of this field are free in terms of not being
* infested by bugs. If the field is at the edge of the forest, i.e. has less than eight neighbors, the
* non-existing fields are counted as non-free.
* <p>
* Preconditions: All field pointers (left, right, up and down) are properly intitialized, i.e. != null unless
* this field is an edge field where some neighbors might not exist.
* Postconditions: All neighboring fields checked for existence of bug colonies on them
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null
* unless this field is an edge field where some neighbors might not exist. Postconditions: All
* neighboring fields checked for existence of bug colonies until one is found that's free
*
* @return true if no forestField in an 8-neighborhood of this field is occupied (field.getColony() == null),
* false otherwise
* @return true if at least one forestField in an 8-neighborhood of this field is free (field.getColony()
* == null), false otherwise
*/
public boolean checkNeighborhoodFree() {
boolean upperNeighbors = true;
boolean middleNeighbors = true;
boolean lowerNeighbors = true;
boolean upperNeighbors = false;
boolean middleNeighbors = false;
boolean lowerNeighbors = false;

if (this.up != null) {
upperNeighbors = this.up.colony == null || (this.up.left != null && this.up.left.colony == null)
|| (this.up.right != null && this.up.right.colony == null);
}
if (this.down != null) {
lowerNeighbors = this.down.colony == null
|| (this.down.left != null && this.down.left.colony == null)
|| (this.down.right != null && this.down.right.colony == null);
}
middleNeighbors = (this.left != null && this.left.colony == null)
|| (this.right != null && this.right.colony == null);
return upperNeighbors || middleNeighbors || lowerNeighbors;
}

if(this.up != null){
if(this.up.getColony() == null){
upperNeighbors = true;
/**
* Returns a randomly selected neighbor field out of this field's 8-neighborhood that is not yet occupied by bugs
*
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null
* unless this field is an edge field where some neighbors might not exist.
* Postconditions: Randomly selected existing neighbor returned
*
* @return a randomly selected neighboring field of this field that is not null and has no bug colony set
*/
public ForestField getRandomFreeNeighbor() {
int rand = (int) Math.random() * 8;
ForestField retVal = null;
do {
switch (rand) {
case 0:
retVal = (this.up != null) ? this.up.left : null;
break;
case 1:
retVal = this.up;
break;
case 2:
retVal = (this.up != null) ? this.up.right : null;
break;
case 3:
retVal = this.right;
break;
case 4:
retVal = (this.down != null) ? this.down.right : null;
break;
case 5:
retVal = this.down;
break;
case 6:
retVal = (this.down != null) ? this.down.left : null;
break;
case 7:
retVal = this.left;
break;
default:
// should not happen
throw new IllegalStateException("Not expected random number: " + rand);
}
else{
upperNeighbors = false;
rand = (int) Math.random() * 8;
} while (retVal == null || retVal.colony != null);
return retVal;
}

/**
* Returns a randomly selected field out of this field's neighborhood
*
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null
* unless this field is an edge field where some neighbors might not exist.
* Postconditions: Randomly selected existing neighbor
*
* @return Randomly selected existing neighbor
*/
public ForestField getRandomNeighbor(){
int rand = (int) Math.random() * 8;
ForestField retVal = null;
do {
switch (rand) {
case 0:
retVal = (this.up != null) ? this.up.left : null;
break;
case 1:
retVal = this.up;
break;
case 2:
retVal = (this.up != null) ? this.up.right : null;
break;
case 3:
retVal = this.right;
break;
case 4:
retVal = (this.down != null) ? this.down.right : null;
break;
case 5:
retVal = this.down;
break;
case 6:
retVal = (this.down != null) ? this.down.left : null;
break;
case 7:
retVal = this.left;
break;
default:
// should not happen
throw new IllegalStateException("Not expected random number: " + rand);
}
if(this.up.left != null && upperNeighbors == true){
if(this.up.left.getColony() == null){
upperNeighbors = true;
rand = (int) Math.random() * 8;
} while (retVal == null);
return retVal;
}

/**
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null
* unless this field is an edge field where some neighbors might not exist.
* <p>
* Postconditions: All healthy neighbors are counted.
*
* @return number of healthy neighbors
*/
public int getNumHealthyNeighbors() {
int cntHealthyNeighbors = 0;

if (this.up != null) {
if (this.up.getColony() != null) {
if (this.up.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
else {
upperNeighbors = false;
}
if (this.up.left != null) {
if (this.up.left.getColony() != null) {
if (this.up.left.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
}
}
if(this.up.right != null && upperNeighbors == true){
if(this.up.right.getColony() == null){
upperNeighbors = true;
}else {
upperNeighbors = false;
if (this.up.right != null) {
if (this.up.right.getColony() != null) {
if (this.up.right.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
}
}
}
if(this.left != null){
if(this.left.getColony() == null){
middleNeighbors = true;
}else{
middleNeighbors = false;
}
}
if(this.right != null && middleNeighbors == true){
if(this.right.getColony() == null){
middleNeighbors = true;
}else{
middleNeighbors = false;
if (this.left != null) {
if (this.left.getColony() != null) {
if (this.left.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
}
}

if(this.down != null){
if(this.down.getColony() == null){
lowerNeighbors = true;
}
else{
lowerNeighbors = false;
if (this.down != null) {
if (this.down.getColony() != null) {
if (this.down.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
}
if(this.down.left != null && lowerNeighbors == true){
if(this.down.left.getColony() == null){
lowerNeighbors = true;
if (this.down.left != null) {
if (this.down.left.getColony() != null) {
if (this.down.left.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
}
else {
lowerNeighbors = false;
}
if (this.down.right != null) {
if (this.down.right.getColony() != null) {
if (this.down.right.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
}
}
if(this.down.right != null && lowerNeighbors == true){
if(this.down.right.getColony() == null){
lowerNeighbors = true;
}else {
lowerNeighbors = false;
}
if (this.right != null) {
if (this.right.getColony() != null) {
if (this.right.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
}
}

return upperNeighbors && middleNeighbors && lowerNeighbors;
return cntHealthyNeighbors;
}

/**
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null unless
* this field is an edge field where some neighbors might not exist.
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null
* unless this field is an edge field where some neighbors might not exist.
* <p>
* Postconditions: All healthy neighbors are counted.
*
* @return number of healthy neighbors
*/
public synchronized int getHealthyNeighbors() {
int cntHealthyNeighbors = 0;
public List<ForestField> getHealthyNeighbors() {
List<ForestField> retVal = new ArrayList<>();

if (this.up != null) {
if (this.up.getColony() != null) {
if (this.up.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.up.getColony().isHealthy()) {
retVal.add(this.up);
}
}
if (this.up.left != null) {
if (this.up.left.getColony() != null) {
if (this.up.left.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.up.left.getColony().isHealthy()) {
retVal.add(this.up.left);
}
}
}
if (this.up.right != null) {
if (this.up.right.getColony() != null) {
if (this.up.right.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.up.right.getColony().isHealthy()) {
retVal.add(this.up.right);
}
}
}
}
if (this.left != null) {
if (this.left.getColony() != null) {
if (this.left.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.left.getColony().isHealthy()) {
retVal.add(this.left);
}
}
}
if (this.down != null) {
if (this.down.getColony() != null) {
if (this.down.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.down.getColony().isHealthy()) {
retVal.add(this.down);
}
}
if (this.down.left != null) {
if (this.down.left.getColony() != null) {
if (this.down.left.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.down.left.getColony().isHealthy()) {
retVal.add(this.down.left);
}
}
}
if (this.down.right != null) {
if (this.down.right.getColony() != null) {
if (this.down.right.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.down.right.getColony().isHealthy()) {
retVal.add(this.down.right);
}
}
}
}
if (this.right != null) {
if (this.right.getColony() != null) {
if (this.right.getColony().isHealthy()) { cntHealthyNeighbors++; }
if (this.right.getColony().isHealthy()) {
retVal.add(this.right);
}
}
}

/* if (this.up != null && this.up.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
if (this.up.left != null && this.up.left.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
if (this.up.right != null && this.up.right.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
if (this.left != null && this.left.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
if (this.down != null && this.down.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
if (this.down.left != null && this.down.left.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
if (this.down.right != null && this.down.right.getColony().isHealthy()) {
cntHealthyNeighbors++;
}
if (this.right != null && this.right.getColony().isHealthy()) {
cntHealthyNeighbors++;
} */


return cntHealthyNeighbors;
return retVal;
}


/**
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null unless
* this field is an edge field where some neighbors might not exist.
* Preconditions: All field pointers (left, right, up and down) are properly initialized, i.e. != null
* unless this field is an edge field where some neighbors might not exist.
* <p>
* Postconditions: All infected neighbors are counted.
*
* @return number of infected neighbors
*/
public synchronized int getInfectedNeighbors() {
public int getNumInfectedNeighbors() {
int cntInfectedNeighbors = 0;

if (this.up != null) {
if (this.up.getColony() != null) {
if (!this.up.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.up.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
if (this.up.left != null) {
if (this.up.left.getColony() != null) {
if (!this.up.left.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.up.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
}
if (this.up.right != null) {
if (this.up.right.getColony() != null) {
if (!this.up.right.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.up.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
}
}
if (this.left != null) {
if (this.left.getColony() != null) {
if (!this.left.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
}
if (this.down != null) {
if (this.down.getColony() != null) {
if (!this.down.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.down.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
if (this.down.left != null) {
if (this.down.left.getColony() != null) {
if (!this.down.left.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.down.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
}
if (this.down.right != null) {
if (this.down.right.getColony() != null) {
if (!this.down.right.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.down.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
}
}
if (this.right != null) {
if (this.right.getColony() != null) {
if (!this.right.getColony().isHealthy()) { cntInfectedNeighbors++; }
if (!this.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
}
}

/* if (this.up != null && !this.up.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.up.left != null && !this.up.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.up.right != null && !this.up.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.left != null && !this.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.down != null && !this.down.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.down.left != null && !this.down.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.down.right != null && !this.down.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.right != null && !this.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
} */
/* if (this.up != null && !this.up.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.up.left != null && !this.up.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.up.right != null && !this.up.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.left != null && !this.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.down != null && !this.down.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.down.left != null && !this.down.left.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.down.right != null && !this.down.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
}
if (this.right != null && !this.right.getColony().isHealthy()) {
cntInfectedNeighbors++;
} */

return cntInfectedNeighbors;
}
@@ -1,34 +1,39 @@
import java.awt.*;

/**
* Arbeitsaufteilung:
* Christoph Huber: Forest, ForestField
* Michael Langowksi: BugColony
* Ines Rieder: Test und Zusicherungen
* Arbeitsaufteilung: Christoph Huber: Forest, ForestField Michael Langowksi: BugColony Ines Rieder: Test und
* Zusicherungen
*/

public class Test {

public static void main(String[] args) {
Test test = new Test();

System.out.println("testForest: " + test.testForest());
System.out.println("testForestField: " + test.testForestField());

/* Point[] points = new Point[3];
points[0] = new Point(1,0);
points[1] = new Point(2,2);
points[2] = new Point(2,0);
Forest forest = new Forest(3,3,points);
System.out.println(forest.toString()); */
// System.out.println("testForest: " + Test.testForest());
// System.out.println("testForestField: " + Test.testForestField());
// System.out.println("testBugColonyRandomNumbers: " + Test.testColonyRandomNum());
/* Point[] points = new Point[3];
points[0] = new Point(1,0);
points[1] = new Point(2,2);
points[2] = new Point(2,0);
Forest forest = new Forest(3,3,points);
System.out.println(forest.toString()); */
Test.testSim();
}

private static boolean testSim(){
Forest.init(10, 10, 3);
Forest.getInstance().startSimulation();
return true;
}

private static boolean testForest() {
Forest forest = new Forest(3,3);
Forest.init(3, 3);
Forest forest = Forest.getInstance();
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
ForestField tmp = forest.getFieldAt(new Point(x,y));
ForestField tmp = forest.getFieldAt(new Point(x, y));
if ((tmp == null) || (tmp.getColony() != null)) {
System.out.println("Forest(int width, int height) not working correctly.");
return false;
@@ -37,22 +42,24 @@ private static boolean testForest() {
}

Point[] points = new Point[3];
points[0] = new Point(0,0);
points[1] = new Point(1,1);
points[2] = new Point(2,2);
Forest forest1 = new Forest(3,3, points);
for(int i = 0; i < points.length; i++) {
if ((forest1.getFieldAt(points[i])) == null) {
points[0] = new Point(0, 0);
points[1] = new Point(1, 1);
points[2] = new Point(2, 2);
Forest.init(3, 3, points);
forest = Forest.getInstance();
for (int i = 0; i < points.length; i++) {
if ((forest.getFieldAt(points[i])) == null) {
System.out.println("Forest(int width, int height, Point... colonies) not working correctly.");
return false;
}
}

Forest forest2 = new Forest(3,3,3);
Forest.init(3, 3, 3);
forest = Forest.getInstance();
int counter = 0;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
ForestField tmp = forest2.getFieldAt(new Point(x,y));
ForestField tmp = forest.getFieldAt(new Point(x, y));
if (tmp.getColony() != null) {
counter++;
}
@@ -66,7 +73,7 @@ private static boolean testForest() {
forest.placeColony(points[0]);
forest.placeColony(points[1]);
forest.placeColony(points[2]);
for(int i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
if ((forest.getFieldAt(points[i])) == null) {
System.out.println("placeColony(Point pos) not working correctly.");
return false;
@@ -77,37 +84,52 @@ private static boolean testForest() {
}

private static boolean testForestField() {
Forest forest = new Forest(4,4);
Forest.init(4, 4);
Forest forest = Forest.getInstance();

Point tmp = new Point(0,3);
Point tmp = new Point(0, 3);
BugColony colony = new BugColony(forest.getFieldAt(tmp));
colony.setHealthy(false);
forest.placeColony(colony, tmp);
forest.placeColony(new Point(3,2));
forest.placeColony(new Point(3, 2));

if ((forest.getFieldAt(new Point(1,1)).checkNeighborhoodFree() != true) ||
(forest.getFieldAt(new Point(3,0)).checkNeighborhoodFree() != true) ||
(forest.getFieldAt(new Point(1,2)).checkNeighborhoodFree() != false) ||
(forest.getFieldAt(new Point(2,2)).checkNeighborhoodFree() != false)) {
if ((forest.getFieldAt(new Point(1, 1)).checkNeighborhoodFree() != true)
|| (forest.getFieldAt(new Point(3, 0)).checkNeighborhoodFree() != true)
|| (forest.getFieldAt(new Point(1, 2)).checkNeighborhoodFree() != false)
|| (forest.getFieldAt(new Point(2, 2)).checkNeighborhoodFree() != false)) {
return false;
}

//getHealthyNeighbours
Point tmp1 = new Point(2,1);
Point tmp1 = new Point(2, 1);
BugColony colony1 = new BugColony(forest.getFieldAt(tmp1));
colony1.setHealthy(false);
forest.placeColony(colony1, tmp1);
forest.placeColony(new Point(2,3));
forest.placeColony(new Point(0,2));

if ((forest.getFieldAt(new Point(1,2)).getInfectedNeighbors() != 2) ||
(forest.getFieldAt(new Point(1,2)).getHealthyNeighbors() != 2) ||
(forest.getFieldAt(new Point(2,2)).getInfectedNeighbors() != 1) ||
(forest.getFieldAt(new Point(2,2)).getHealthyNeighbors() != 2) ||
(forest.getFieldAt(new Point(3,3)).getHealthyNeighbors() != 2)) {
forest.placeColony(new Point(2, 3));
forest.placeColony(new Point(0, 2));

if ((forest.getFieldAt(new Point(1, 2)).getNumInfectedNeighbors() != 2)
|| (forest.getFieldAt(new Point(1, 2)).getNumHealthyNeighbors() != 2)
|| (forest.getFieldAt(new Point(2, 2)).getNumInfectedNeighbors() != 1)
|| (forest.getFieldAt(new Point(2, 2)).getNumHealthyNeighbors() != 2)
|| (forest.getFieldAt(new Point(3, 3)).getNumHealthyNeighbors() != 2)) {
return false;
}

return true;
}

private static boolean testColonyRandomNum() {
int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = BugColony.calcWaitMsecs();
}
for (int i : numbers) {
//System.out.println("randNr = " + i);
if (i < 5 || i > 50) {
return false;
}
}
return true;
}
}