-
Notifications
You must be signed in to change notification settings - Fork 0
/
AntWorld.java
126 lines (103 loc) · 3.72 KB
/
AntWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* The world where ants live.
*
* @author Michael Kölling
* @version 1.1
*/
public class AntWorld extends World
{
public static final int SIZE = 4000;
protected Counter AntsLivingCounter;
protected Counter AntsDeadCounter;
private Counter TimeCounter;
private Counter Date;
/**
* Create a new world. It will be initialised with a few ant hills
* and food sources
*/
public AntWorld()
{
super(SIZE, SIZE, 1);
setPaintOrder(Ant.class, Pheromone.class, AntHill.class, Food.class);
setup4();
}
/**
* Create world contents: one ant hill and food.
*/
private void setup1()
{
removeObjects(getObjects(null)); // remove all existing objects
addObject(new AntHill(70), SIZE / 2, SIZE / 2);
addObject(new Food(), SIZE / 2, SIZE / 2 - 260);
addObject(new Food(), SIZE / 2 + 215, SIZE / 2 - 100);
addObject(new Food(), SIZE / 2 + 215, SIZE / 2 + 100);
addObject(new Food(), SIZE / 2, SIZE / 2 + 260);
addObject(new Food(), SIZE / 2 - 215, SIZE / 2 + 100);
addObject(new Food(), SIZE / 2 - 215, SIZE / 2 - 100);
}
/**
* Create world contents: two ant hills and food.
*/
private void setup2()
{
removeObjects(getObjects(null)); // remove all existing objects
addObject(new AntHill(40), 506, 356);
addObject(new AntHill(40), 95, 267);
addObject(new Food(), 80, 71);
addObject(new Food(), 291, 56);
addObject(new Food(), 516, 212);
addObject(new Food(), 311, 269);
addObject(new Food(), 318, 299);
addObject(new Food(), 315, 331);
addObject(new Food(), 141, 425);
addObject(new Food(), 378, 547);
addObject(new Food(), 566, 529);
}
/**
* Create world contents: two ant hills and food.
*/
private void setup3()
{
removeObjects(getObjects(null)); // remove all existing objects
addObject(new AntHill(40), 576, 134);
addObject(new AntHill(40), 59, 512);
addObject(new Food(), 182, 84);
addObject(new Food(), 39, 308);
addObject(new Food(), 249, 251);
addObject(new Food(), 270, 272);
addObject(new Food(), 291, 253);
addObject(new Food(), 339, 342);
addObject(new Food(), 593, 340);
addObject(new Food(), 487, 565);
}
private void setup4() {
removeObjects(getObjects(null));
addObject(new AntHill(40), (SIZE / 3), (SIZE / 3));
for (int i = 0; i < (Greenfoot.getRandomNumber(SIZE / 20) + SIZE / 100); i++) {
addObject(new Food(), Greenfoot.getRandomNumber(SIZE), Greenfoot.getRandomNumber(SIZE));
}
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm:s");
Date = new Counter("Date: " + sdf.format(new Date()));
addObject(Date, SIZE - 105, 30);
TimeCounter = new CounterTime("Time:");
addObject(TimeCounter, SIZE - 40, 50);
AntsLivingCounter = new Counter("Living Ants: ");
addObject(AntsLivingCounter, SIZE - 140, 70);
AntsDeadCounter = new Counter("Dead Ants: ");
addObject(AntsDeadCounter, SIZE - 150, 90);
}
public void act(){
spawnFood();
this.TimeCounter.increment();
}
private void spawnFood(){
//TODO EVALUATE following expression
// Greenfoot.getRandomNumber((int) Math.pow(SIZE, 2)) < (int)(SIZE / 100)
if ((Greenfoot.getRandomNumber(1000) < 20)) {
addObject(new Food(), Greenfoot.getRandomNumber(SIZE) + 1 , Greenfoot.getRandomNumber(SIZE) + 1);
}
}
}