-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEspace.java
More file actions
193 lines (175 loc) · 4.19 KB
/
Copy pathEspace.java
File metadata and controls
193 lines (175 loc) · 4.19 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* @author Conor Evans
* @version 1.0
* @date 2018-11-28
*
* Represents a grid of Cases (co-ordinates) for the Chenille (caterpillar)
* to move around in.
*
*/
import java.util.Arrays;
import java.util.Random;
public class Espace {
/** width */
private int largeur;
/** height */
private int hauteur;
/** array of caterpillars to be placed on our grid */
private Chenille[] chenilles;
/** number of caterpillars on our grid */
private int nbChenilles;
/**
* Default constructor which makes a 10x10 square grid with 5 potential
* caterpillars, none as of yet instanciated.
*/
public Espace() {
this.largeur = 10;
this.hauteur = 10;
this.chenilles = new Chenille[5];
this.nbChenilles = 0;
}
/**
* Constructor which makes a i x i square grid with 5 potential
* caterpillars, none as of yet instantiated.
*
* @param {int}
* i - height/width
*/
public Espace(int i) {
this.largeur = i;
this.hauteur = i;
this.chenilles = new Chenille[5];
this.nbChenilles = 0;
}
/**
* Constructor which makes a i x j square grid with 5 potential
* caterpillars, none as of yet instantiated.
*
* @param {int}
* i - width
* @param {int}
* j - height
*/
public Espace(int i, int j) {
this.largeur = i;
this.hauteur = j;
this.chenilles = new Chenille[5];
this.nbChenilles = 0;
}
/**
* Adds caterpillar to the grid
*
* @param {Chenille}
* c
*/
public void addChenille(Chenille c) {
// if our initial array of caterpillars is full and we wish to add
// another caterpillar
// we augment the array.
if (this.chenilles.length == this.nbChenilles) {
this.chenilles = Arrays.copyOf(this.chenilles, this.chenilles.length + 5);
this.chenilles[this.chenilles.length] = c;
this.nbChenilles++;
} else {
this.chenilles[this.nbChenilles] = c;
this.nbChenilles++;
}
}
/**
* Verifies if c is a valid co-ordinate in this grid
*
* @param {Case}
* c
* @return boolean
*/
public boolean contient(Case c) {
return (c.getAbscisse() >= 0 && c.getAbscisse() < this.largeur && c.getOrdonnee() >= 0
&& c.getOrdonnee() < this.hauteur);
}
/**
* Verifies if current co-ordinate is already occupied
*
* @param {Case}
* c
* @return boolean
*/
public boolean caseOccupee(Case c) {
for (int i = 0; i < this.nbChenilles; i++) {
if (this.chenilles[i].estSur(c)) {
return true;
}
}
return false;
}
/**
* Returns a random coordinate in the grid
*
* @return {Case}
*/
public Case caseAuHasard() {
Random r = new Random();
return new Case(r.nextInt(largeur), r.nextInt(hauteur));
}
/**
* Returns a random unoccupied coordinate in the grid
*
* @return {Case}
*/
public Case caseLibreAuHasard() {
Case has = caseAuHasard();
if (!caseOccupee(has)) {
return has;
}
return caseLibreAuHasard();
}
/**
* Represents the grid as a String return {String}
*/
public String toString() {
String s = "";
for (int i = 0; i < this.hauteur; i++) {
for (int j = 0; j < this.largeur; j++) {
Case c = new Case(j, i);
// set base grid
if (!caseOccupee(c)) {
s += ". ";
} else {
for (int k = 0; k < this.nbChenilles; k++) {
if (this.chenilles[k].estSur(c)) {
Case[] chenille = this.chenilles[k].getCases();
// capital to represent head of the caterpillar
if (c.equals(chenille[0])) {
s += (char) (k + 'A') + " ";
} else {
s += (char) (k + 'a') + " ";
}
}
}
}
}
s += "\n";
}
return s;
}
public int getLargeur() {
return largeur;
}
public int getHauteur() {
return hauteur;
}
public Chenille[] getChenilles() {
return chenilles;
}
public int getNbChenilles() {
return nbChenilles;
}
public void main(String[] args) {
Espace e = new Espace(10);
Chenille ch1 = new Chenille(e, new Case(0, 0), 5);
e.addChenille(ch1);
for (int i = 0; i < ch1.getCases().length; i++) {
System.out.println(e.toString());
ch1.avance();
}
}
}