program Life; const MAX = 100; type FieldRange = 1..MAX; Field = array[FieldRange, FieldRange] of boolean; var i, steps: integer; command: string; HEIGHT, WIDTH: FieldRange; map: Field; procedure printField(map: Field); var i, j: integer; begin for i := 1 to HEIGHT do begin for j := 1 to WIDTH do if map[i, j] then write('#') else write('_'); writeln end; writeln; writeln end; procedure copyField(var source, destination: Field); var i, j: integer; begin for i := 1 to HEIGHT do for j := 1 to WIDTH do destination[i, j] := source[i, j] end; procedure inputField(var map: Field); var i, j: integer; c: char; begin writeln('Input field:'); for i := 1 to HEIGHT do begin for j := 1 to WIDTH do begin read(c); if c = '#' then map[i, j] := true else map[i, j] := false end; readln end; writeln('Field has been loaded.') end; procedure saveField(map: Field; filename: string); var savefile: text; i, j: integer; begin assign(savefile, filename); rewrite(savefile); writeln(savefile, HEIGHT); writeln(savefile, WIDTH); for i := 1 to HEIGHT do begin for j := 1 to WIDTH do if map[i,j] then write(savefile, '#') else write(savefile, '_'); writeln(savefile) end; close(savefile) end; procedure loadField(var map: Field; filename: string); var loadfile: text; i, j: integer; c: char; begin assign(loadfile, filename); reset(loadfile); readln(loadfile, HEIGHT); readln(loadfile, WIDTH); for i := 1 to HEIGHT do begin for j := 1 to WIDTH do begin read(loadfile, c); if c = '#' then map[i, j] := true else map[i, j] := false end; readln(loadfile) end; close(loadfile) end; function check(map: Field; y, x: integer): boolean; begin if y < 1 then y := HEIGHT else if y > HEIGHT then y := 1; if x < 1 then x := WIDTH else if x > WIDTH then x := 1; check := map[y, x] end; procedure next(var map: Field); var i, j, count: integer; map_copy: Field; begin copyField(map, map_copy); for i := 1 to HEIGHT do for j := 1 to WIDTH do begin { Neighbours count calculation } count := 0; if check(map_copy, succ(i), succ(j)) then count := succ(count); if check(map_copy, i, succ(j)) then count := succ(count); if check(map_copy, pred(i), succ(j)) then count := succ(count); if check(map_copy, succ(i), j) then count := succ(count); if check(map_copy, pred(i), j) then count := succ(count); if check(map_copy, succ(i), pred(j)) then count := succ(count); if check(map_copy, i, pred(j)) then count := succ(count); if check(map_copy, pred(i), pred(j)) then count := succ(count); { Neighbours count analysis } if (count = 3) or (map_copy[i,j] and (count = 2)) then map[i, j] := true else map[i, j] := false end end; begin writeln('Welcome in Life game realization by AKVeresov!'); writeln('Load savefile? (yes - "y" / no - anything else)'); readln(command); if command = 'y' then begin loadField(map, 'save'); writeln('Savefile has been loaded.'); printField(map) end else begin writeln('Write height and width of field:'); read(HEIGHT, WIDTH); inputField(map) end; writeln('Write steps count:'); read(steps); for i := 1 to steps do begin next(map); printField(map) end; readln; writeln('Save field? (yes - "y" / no - anything else)'); readln(command); if command = 'y' then begin saveField(map, 'save'); writeln('Field has been saved.') end; writeln('Goodbye!') end.