-
Notifications
You must be signed in to change notification settings - Fork 0
/
Craig.java
79 lines (66 loc) · 2.02 KB
/
Craig.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
package assignment5;
public class Craig extends Critter {
@Override
public String toString() { return "C"; }
private static final int GENE_TOTAL = 24;
private int[] genes = new int[8];
private int dir;
public Craig() {
for (int k = 0; k < 8; k += 1) {
genes[k] = GENE_TOTAL / 8;
}
dir = Critter.getRandomInt(8);
}
public boolean fight(String not_used) { return true; }
@Override
public void doTimeStep() {
/* take one step forward */
walk(dir);
if (getEnergy() > 150) {
Craig child = new Craig();
for (int k = 0; k < 8; k += 1) {
child.genes[k] = this.genes[k];
}
int g = Critter.getRandomInt(8);
while (child.genes[g] == 0) {
g = Critter.getRandomInt(8);
}
child.genes[g] -= 1;
g = Critter.getRandomInt(8);
child.genes[g] += 1;
reproduce(child, Critter.getRandomInt(8));
}
/* pick a new direction based on our genes */
int roll = Critter.getRandomInt(GENE_TOTAL);
int turn = 0;
while (genes[turn] <= roll) {
roll = roll - genes[turn];
turn = turn + 1;
}
assert(turn < 8);
dir = (dir + turn) % 8;
}
public String runStats(java.util.List<Critter> craigs) {
int total_straight = 0;
int total_left = 0;
int total_right = 0;
int total_back = 0;
for (Object obj : craigs) {
Craig c = (Craig) obj;
total_straight += c.genes[0];
total_right += c.genes[1] + c.genes[2] + c.genes[3];
total_back += c.genes[4];
total_left += c.genes[5] + c.genes[6] + c.genes[7];
}
String s = ("" + craigs.size() + " total Craigs ");
s += ("" + total_straight / (GENE_TOTAL * 0.01 * craigs.size()) + "% straight ");
s += ("" + total_back / (GENE_TOTAL * 0.01 * craigs.size()) + "% back ");
s += ("" + total_right / (GENE_TOTAL * 0.01 * craigs.size()) + "% right ");
s += ("" + total_left / (GENE_TOTAL * 0.01 * craigs.size()) + "% left ");
return s;
}
@Override
public CritterShape viewShape() { return CritterShape.SQUARE; }
@Override
public javafx.scene.paint.Color viewOutlineColor() { return javafx.scene.paint.Color.BLUE; }
}