-
Notifications
You must be signed in to change notification settings - Fork 0
/
Food.java
96 lines (71 loc) · 2.02 KB
/
Food.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
package sim.app.pvp;
import sim.engine.*;
import sim.field.grid.SparseGrid2D;
import sim.util.Int2D;
public class Food implements Steppable {
private double amount = 1.0;
private double diseasePr = .00005;
private int diseaseRandNum = 10000;
private double repPr = .001;
private int repRandNum = 10000;
private boolean diseased = false;
public SparseGrid2D grid;
@Override
public void step(SimState state) {
// TODO Auto-generated method stub
PVP pvp = (PVP)state;
grid = pvp.world;
//Chance of disease
this.diseaseChance(pvp);
//Chance of spread
this.reproductionChance(pvp);
}
public void diseaseChance(PVP pvp){
double d = pvp.random.nextInt(diseaseRandNum);
double disease = d/diseaseRandNum;
assert (disease > 0);
if(disease < diseasePr)
diseased = true;
}
public void reproductionChance(PVP pvp){
// reproduction rate
double rep = pvp.random.nextInt(repRandNum);
//System.out.println("rep: " + rep);
double repro = rep/repRandNum;
//System.out.println("Repro: " + repro);
assert (repro > 0);
if(repro < repPr)
this.spread(grid, pvp);
}
public boolean isDiseased(){
return diseased;
}
public void spread(SparseGrid2D grid, SimState state){
Int2D cord = grid.getObjectLocation(this);
if(cord != null){
Food p = new Food();
int direction = state.random.nextInt(7);
if (direction == 0){
grid.setObjectLocation(p, cord.x, cord.y + 1);
}
else if (direction == 1){
grid.setObjectLocation(p, cord.x, cord.y - 1);
}
else if (direction == 2){
grid.setObjectLocation(p, cord.x + 1, cord.y);
}
else if (direction == 3){
grid.setObjectLocation(p, cord.x + 1, cord.y + 1);
}
else if (direction == 4){
grid.setObjectLocation(p, cord.x + 1, cord.y - 1);
}
else if (direction == 5){
grid.setObjectLocation(p, cord.x - 1, cord.y + 1);
}
else if (direction == 6){
grid.setObjectLocation(p, cord.x - 1, cord.y - 1);
}
}// end of if
}// end of spread
}// end of class