-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourmi.java
154 lines (140 loc) · 3.41 KB
/
Fourmi.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.awt.Color;
import java.util.Random;
public class Fourmi {
private int l; // indice de la ligne qu'occupe la fourmi dans le monde
private int c; // indice de la colonne qu'occupe la fourmi dans le monde
private int d; // direction de la fourmi : 0 vers le haut, 1 vers la droite, 2 vers le bas, 3 vers la gauche
private int[] vel; // vel[0] rectilign velocity, vel[1] step velocity (step/gen)
private boolean coll = false;
private Color cl; // couleur fourmi
private boolean active = true;
// Constructeur complet
public Fourmi(int x, int y, int d) {
this.l = y;
this.c = x;
this.d = d;
this.vel = new int[] {1,1};
setRandomColor();
}
public Fourmi(int x, int y, int d, int[] vel, boolean coll) {
this.l = x;
this.c = y;
this.d = d;
this.coll = coll;
this.vel = vel;
setRandomColor();
}
public Fourmi(Plateau p, int d, int[] vel, boolean coll) {
this.l = p.getHeight()/2;
this.c = p.getWidth()/2;
this.d = d;
this.coll = coll;
this.vel = vel;
setRandomColor();
}
public Fourmi(Plateau p, boolean r, int[] vel, boolean coll) {
this(p,0,vel, coll);
if(r) {
this.c = (int)(p.getWidth()*Math.random());
this.l = (int)(p.getHeight()*Math.random());
this.d = (int)(4*Math.random());
}
}
// Accesseurs
public int getLigne(){
return this.l;
}
public int getColonne(){
return this.c;
}
public int getDirection(){
return this.d;
}
public void setDirection(int direction) { this.d = direction; }
public Color getColor() {
return this.cl;
}
public void setColor(Color c) {
this.cl = c;
}
public void setRandomColor() {
this.cl = new Color((int)(Math.random() * 0x1000000)).brighter();
}
public int getStepVelocity() {
return this.vel[1];
}
public int getVelocity() {
return this.vel[0];
}
public void setStepVelocity(int aVel) {
this.vel[1] = aVel;
}
public void setVelocity(int vel) {
this.vel[0] = vel;
}
public boolean doesCollide() {
return this.coll;
}
public void setCollisions(boolean coll) { this.coll = coll; }
public void avance(){
switch(this.d) {
case 0: {
this.c--;
break;
}
case 1: {
this.l++;
break;
}
case 2: {
this.c++;
break;
}
default: {
this.l--;
break;
}
}
}
/**
* Deplacement de la fourmie
* @param c Vaut true si la case ou se situe la fourmi avant le deplacement est noire (false = blanche)
*/
public void tourner(boolean c) {
this.d += (c ? 1 : 3);
this.d = this.d%4;
}
public boolean collidesWith(Fourmi f) {
if(this == null || f == null) return false;
return (this.doesCollide() && f.doesCollide() && this.c == f.getColonne() && this.l == f.getLigne());
}
public void onCollideWith(Fourmi coll) {
if(!coll.doesCollide()) return;
tourner(true);
setRandomColor();
coll.setRandomColor();
}
public boolean isOut(Plateau p) {
int w = p.getWidth();
int h = p.getHeight();
return (this.c < 0 || this.c >= w || this.l < 0 || this.l >= h);
}
public void getIn(Plateau p) {
int w = p.getWidth();
int h = p.getHeight();
if (this.l < 0)
this.l = h-1;
else if (this.c < 0)
this.c = w-1;
else if (this.l >= h)
this.l = 0;
else if (this.c >= w)
this.c = 0;
}
public String toString() {
return "Fourmi: x="+this.c+", "+this.l+", d="+this.d;
}
public boolean isActive() { return active; }
public void stop() { this.active = false; }
public void start() { this.active = true; }
}