-
Notifications
You must be signed in to change notification settings - Fork 0
/
Population.jack
140 lines (117 loc) · 4.04 KB
/
Population.jack
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
class Population{
field Array people;
field int size, num_infected, num_vaxed, num_dead;
constructor Population new(int Asize, int Anum_infected, int Anum_vaxed){
let num_infected = Anum_infected;
let num_vaxed = Anum_vaxed;
let num_dead = 0;
let size = Asize;
let people = Array.new(size);
do createPeople();
return this;
}
method void createPeople(){
var int i;
var int randX, randY, randV_x, randV_y;
let i=0;
//Creates the num_infected people
while (i<num_infected){
let randX = RandGen.rand_in_range(GameManager.get_screen_left()+10, GameManager.get_screen_right()-10);
let randY = RandGen.rand_in_range(GameManager.get_screen_top()+10, GameManager.get_screen_bottom()-10);
let randV_x = RandGen.rand_in_range(-4,4);
let randV_y = RandGen.rand_in_range(-4,4);
let people[i] = Person.new(randX, randY, randV_x, randV_y, true, false);
let i = i+1;
}
//Creates the vaxed people
while (i<(num_infected + num_vaxed)){
let randX = RandGen.rand_in_range(GameManager.get_screen_left()+10, GameManager.get_screen_right()-10);
let randY = RandGen.rand_in_range(GameManager.get_screen_top()+10, GameManager.get_screen_bottom()-10);
let randV_x = RandGen.rand_in_range(-4,4);
let randV_y = RandGen.rand_in_range(-4,4);
let people[i] = Person.new(randX, randY, randV_x, randV_y, false, true);
let i = i+1;
}
//Creates the healthy people
while (i<size){
let randX = RandGen.rand_in_range(GameManager.get_screen_left()+10, GameManager.get_screen_right()-10);
let randY = RandGen.rand_in_range(GameManager.get_screen_top()+10, GameManager.get_screen_bottom()-10);
let randV_x = RandGen.rand_in_range(-4,4);
let randV_y = RandGen.rand_in_range(-4,4);
let people[i] = Person.new(randX, randY, randV_x, randV_y, false, false);
let i = i+1;
}
return;
}
//Moves all people according to their speeds
method void movePeople(){
var int i, res, wait_time;
var Person cur_p;
let i=0;
while (i<size){
let cur_p = people[i];
let res = cur_p.move();
if (res=1){
let num_infected = num_infected -1;
}
if (res=2){
let num_infected = num_infected -1;
let num_dead = num_dead +1;
}
if ((cur_p.get_is_infected())&(~(cur_p.get_is_dead()))){
let num_infected = spread(cur_p.getX(), cur_p.getY()) + num_infected;
}
let i = i+1;
}
do Sys.wait(calculate_wait_time());
return;
}
method int spread(int x, int y){
var int i, sum;
var Person cur_p;
let i =0;
let sum = 0;
while (i<size){
let cur_p = people[i];
if(cur_p.check_and_expose(x, y)){
let sum = sum+1;
}
let i = i+1;
}
return sum;
}
method int get_num_dead(){
return num_dead;
}
method int get_num_infected(){
return num_infected;
}
method int calculate_wait_time(){
if (num_infected = 0){
return 50;
}
if (num_infected =1){
return 25;
}
if (num_infected = 2){
return 12;
}
if (num_infected = 3){
return 6;
}
return 0;
}
method void dispose(){
var int i;
var Person p;
let i=0;
while(i<size){
let p = people[i];
do p.dispose();
let i = i+1;
}
do people.dispose();
do Memory.deAlloc(this);
return;
}
}