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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
--
-- Originally written by Cyril ADRIAN and Antony LE LABOUSSE
--
class SPREAD_ILLNESS
-- To compile type command : compile SPREAD_ILLNESS make
--
-- Also try Optimised version adding options "-boost -O2"
--
creation make
feature {NONE}
world1, world2: ARRAY2[INTEGER];
empty : INTEGER is 1;
healthy : INTEGER is 0;
di: ARRAY[INTEGER] is
once
Result := <<-1,-1,-1,0,1,1,1,0 >>
end;
dj: ARRAY[INTEGER] is
once
Result := <<-1,0,1,1,1,0,-1,-1 >>
end;
error_msg : STRING is "Error : do it again !%N"
yes_or_no(question: STRING; default_answer: BOOLEAN): BOOLEAN is
do
io.put_string(question);
io.put_string("(y/n)? ");
if default_answer then
io.put_string("[y]");
else
io.put_string("[n]");
end;
io.flush;
io.read_line;
io.last_string.to_lower;
if io.last_string.is_empty then
Result := default_answer;
elseif io.last_string.first = 'y' then
Result := true;
elseif io.last_string.first = 'n' then
else
Result := yes_or_no(question,default_answer);
end;
end;
feature
make is
-- Try to spreads an illness through a set of people.
local
day: INTEGER;
fed_up: BOOLEAN;
do
from
day := 1;
first_day;
until
fed_up
loop
io.put_string("Day #");
io.put_integer(day);
io.put_new_line;
display;
fed_up := yes_or_no("Exit spread_illness",false);
day := day + 1;
next_day
end;
end;
first_day is
-- Create the world in the 1st day's state.
local
i, j, ill_state: INTEGER;
ok: BOOLEAN;
do
if yes_or_no("Default First World",true) then
!!world1.from_model(
<<<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>>>);
else
io.put_string("World size: ");
io.flush;
io.read_integer;
!!world1.make(1,io.last_integer,1,io.last_integer);
io.put_string("%Ne)mpty, i)ll with number of days, h)ealthy%N%N");
from
i := world1.lower1
until
i > world1.upper1
loop
from
j := world1.lower2
until
j > world1.upper2
loop
from
ok := false;
until
ok
loop
io.put_string("State of [");
io.put_integer(i);
io.put_character(',');
io.put_integer(j);
io.put_string("] : ");
io.flush;
io.read_word;
inspect
io.last_string.first
when 'E','e' then
if io.last_string.count /= 1 then
io.put_string(error_msg)
else
world1.put(empty,i,j);
ok := true
end
when 'H','h' then
if io.last_string.count /= 1 then
io.put_string(error_msg)
else
world1.put(healthy,i,j);
ok := true
end
when 'I','i' then
ok := true;
io.flush;
io.read_integer;
ill_state := io.last_integer;
ill_state := ill_state.max(1);
ill_state := ill_state.min(4);
world1.put(- ill_state,i,j);
else
io.put_string(error_msg)
end;
end;
j := j + 1;
end;
i := i + 1;
end;
end;
world2 := clone(world1)
end;
display is
-- Displays current `world2'.
local
i,j : INTEGER;
do
io.put_character('+');
io.put_character('-');
from
i := world1.lower1
until
i > world1.upper1
loop
io.put_character('-');
io.put_character('-');
i := i+1
end;
io.put_character('+');
io.put_new_line;
from
i := world1.lower1
until
i > world1.upper1
loop
io.put_character('|');
io.put_character(' ');
from
j := world1.lower2
until
j > world1.upper2
loop
inspect
world2.item(i,j)
when empty then
io.put_character(' ')
when healthy then
io.put_character('O')
else
io.put_character((- world2.item(i,j)).digit)
end;
io.put_character(' ');
j := j+1
end;
io.put_character('|');
io.put_new_line;
i := i+1
end;
io.put_character('+');
io.put_character('-');
from
i := world1.lower1
until
i > world1.upper1
loop
io.put_character('-');
io.put_character('-');
i := i+1
end;
io.put_character('+');
io.put_new_line;
end;
next_day is
-- `world1' and `world2' are swapped.
local
i,j: INTEGER;
do
world1.copy(world2);
from
i := world1.lower1;
until
i > world1.upper1
loop
from
j := world1.lower2;
until
j > world1.upper2
loop
inspect
world1.item(i,j)
when empty then
when healthy then
spread(i,j);
else
cure_or_die(i,j);
end;
j := j + 1;
end;
i := i + 1;
end;
end;
feature {NONE}
cure_or_die(i,j : INTEGER) is
-- When ill, inspects if must be death or more ill or cured.
do
inspect
- world1.item(i,j)
when 1 then
world2.put(-2,i,j)
when 2 then
die(i,j)
when 3 then
world2.put(-4,i,j)
when 4 then
world2.put(healthy,i,j)
end
end;
die(i,j: INTEGER) is
-- When two days ill, dies or more ill ?
local
d,k:INTEGER;
do
from
d := 1;
variant
9 - d
until
d > 8
loop
if v(i+di.item(d),j+dj.item(d)) <= -2 then
k := k+1;
end;
d := d+1;
end;
if k >= 4 then
world2.put(empty,i,j);
else
world2.put(-3,i,j);
end
end;
spread(i,j: INTEGER) is
-- spread the illness ?
local
d: INTEGER;
do
from
d := 1;
variant
10 - d
until
d = 9
loop
if ill(i + di @ d , j + dj @ d) then
world2.put(-1,i,j);
d := 9;
else
d := d+1;
end
end
end;
ill(i,j: INTEGER): BOOLEAN is
-- Is there someone is ill at `i', `j'.
do
Result := (v(i,j) < 0)
end;
v(i,j: INTEGER): INTEGER is
-- Gives `empty' when out of range or the
-- value in `world1'
do
if (world1.lower1 <= i and then i <= world1.upper1)
and (world1.lower2 <= j and then j <= world1.upper2) then
Result := world1.item(i,j)
else
Result := empty
end;
end;
end -- SPREAD_ILLNESS
|