|
@@ -14,6 +14,11 @@ public class Pigeon implements Runnable{
|
14
|
14
|
private final Game game;
|
15
|
15
|
private static final int speed = 10;
|
16
|
16
|
|
|
17
|
+ /**
|
|
18
|
+ * Create a new pigeon with a random position
|
|
19
|
+ * @param name The pigeon name
|
|
20
|
+ * @param g The pigeon world
|
|
21
|
+ */
|
17
|
22
|
public Pigeon(String name, Game g) {
|
18
|
23
|
Random r = new Random();
|
19
|
24
|
x = r.nextInt(700);
|
|
@@ -22,29 +27,28 @@ public class Pigeon implements Runnable{
|
22
|
27
|
this.game = g;
|
23
|
28
|
}
|
24
|
29
|
|
|
30
|
+ /**
|
|
31
|
+ * The piegon behavior during his life
|
|
32
|
+ */
|
25
|
33
|
@Override
|
26
|
34
|
public void run() {
|
27
|
35
|
while (true) {
|
28
|
36
|
synchronized (game) {
|
|
37
|
+ // Get the food near the pigeon
|
29
|
38
|
List<Food> foods = game.getFoods();
|
30
|
39
|
Food best = new Food();
|
31
|
|
- int bestFoodX = 0, bestFoodY = 0, distance = -1, bestIndex = -1, index = 0;
|
|
40
|
+ int distance = -1;
|
32
|
41
|
for (Food food : foods) {
|
33
|
42
|
if (distance == -1) {
|
34
|
43
|
distance = computeDistance(food.getX(), food.getY());
|
35
|
|
- bestFoodX = food.getX();
|
36
|
|
- bestFoodY = food.getY();
|
37
|
|
- bestIndex = index;
|
38
|
44
|
best = food;
|
39
|
45
|
} else if (distance > computeDistance(food.getX(), food.getY())) {
|
40
|
46
|
distance = computeDistance(food.getX(), food.getY());
|
41
|
|
- bestFoodX = food.getX();
|
42
|
|
- bestFoodY = food.getY();
|
43
|
|
- bestIndex = index;
|
|
47
|
+
|
44
|
48
|
best = food;
|
45
|
49
|
}
|
46
|
|
- index++;
|
47
|
50
|
}
|
|
51
|
+ // Eat or move if there is a food
|
48
|
52
|
if (distance != -1 && distance <= Pigeon.speed) {
|
49
|
53
|
this.x = best.getX();
|
50
|
54
|
this.y = best.getY();
|
|
@@ -55,15 +59,25 @@ public class Pigeon implements Runnable{
|
55
|
59
|
else if (distance != -1){
|
56
|
60
|
this.computeNewPosition(best.getX(), best.getY());
|
57
|
61
|
}
|
|
62
|
+ // Random move
|
|
63
|
+ else {
|
|
64
|
+ Random r = new Random();
|
|
65
|
+ if (r.nextInt(100) > 90 + r.nextInt(8)) {
|
|
66
|
+ int moveX = r.nextInt(100) -50;
|
|
67
|
+ int moveY = r.nextInt(100) -50;
|
|
68
|
+ this.x += moveX;
|
|
69
|
+ this.y += moveY;
|
|
70
|
+ if (this.x > game.getWidth())
|
|
71
|
+ this.x = game.getWidth() - r.nextInt(30);
|
|
72
|
+ else if (this.x < 0)
|
|
73
|
+ this.x = r.nextInt(30);
|
|
74
|
+ if (this.y > game.getHeight())
|
|
75
|
+ this.y = game.getHeight() - r.nextInt(30);
|
|
76
|
+ else if (this.y < 0)
|
|
77
|
+ this.y = r.nextInt(30);
|
|
78
|
+ }
|
|
79
|
+ }
|
58
|
80
|
}
|
59
|
|
-
|
60
|
|
-
|
61
|
|
-// Random r = new Random();
|
62
|
|
-// int moveX = r.nextInt(10);
|
63
|
|
-// int moveY = r.nextInt(10);
|
64
|
|
-// this.x += moveX;
|
65
|
|
-// this.y += moveY;
|
66
|
|
-
|
67
|
81
|
try {
|
68
|
82
|
Thread.sleep(100);
|
69
|
83
|
} catch (InterruptedException e) {
|
|
@@ -72,12 +86,23 @@ public class Pigeon implements Runnable{
|
72
|
86
|
}
|
73
|
87
|
}
|
74
|
88
|
|
|
89
|
+ /**
|
|
90
|
+ * Compute the distance between the pigeon and the food
|
|
91
|
+ * @param x the x position of the food
|
|
92
|
+ * @param y the y position of the food
|
|
93
|
+ * @return the distance
|
|
94
|
+ */
|
75
|
95
|
private int computeDistance(int x, int y) {
|
76
|
96
|
int diffx = this.x > x ? this.x - x : x - this.x;
|
77
|
97
|
int diffy = this.y > y ? this.y - y : y - this.y;
|
78
|
98
|
return diffx + diffy;
|
79
|
99
|
}
|
80
|
100
|
|
|
101
|
+ /**
|
|
102
|
+ * Move the pigeon
|
|
103
|
+ * @param x the x position of the food
|
|
104
|
+ * @param y the y position of the food
|
|
105
|
+ */
|
81
|
106
|
private void computeNewPosition(int x, int y) {
|
82
|
107
|
int moves = Pigeon.speed;
|
83
|
108
|
while (moves > 0 ) {
|
|
@@ -100,8 +125,11 @@ public class Pigeon implements Runnable{
|
100
|
125
|
}
|
101
|
126
|
}
|
102
|
127
|
|
|
128
|
+ /**
|
|
129
|
+ * Start the thread
|
|
130
|
+ */
|
103
|
131
|
public void start () {
|
104
|
|
- System.out.println("Starting " + name );
|
|
132
|
+ System.out.println("New pigeon alive : " + name );
|
105
|
133
|
if (t == null) {
|
106
|
134
|
t = new Thread (this, name);
|
107
|
135
|
t.start ();
|