Отчёт По Лабе № 15 «игра Жизнь» По Программированию (Попов Д. И.)
.docxГосударственное образовательное учреждение высшего профессионального образования
Московский государственный университет печати имени Ивана Федорова
Отчёт лабораторной работы № 15
«Игра «Жизнь»»
по дисциплине
«Программирование на языке высокого уровня»
Выполнила: студентка ФЦСиТ группы ДЦас-2-1
Битяева Е. А.
Проверил: заведующий кафедрой Информатики и вычислительной техники
профессор, д.т.н Попов Дмитрий Иванович
Москва 2010
Условие.
Игра моделирует жизнь поколений гипотетической колонии живых клеток, которые выживают, размножаются или погибают. В соответствии со следующими правилами.
1. Клетка выживает, если она имеет двух или трех соседей из восьми возможных.
2. Клетка погибает от изоляции, если у нее нет соседей или только один сосед.
3. Клетка погибает от перенаселения, если у нее 4 и более соседей.
4. В любой пустой позиции, у которой ровно 3 соседа, в следующем поколении появляется новая клетка.
Блок-схема.
Начало
n
for j:=1 to max_y do
for i:=1 to max_x do
a[i,j]:=false
for j:=1 to max_y do begin
str
for i:=1 to min(max_x,length(str)) do
if str[i]='*'
a[i,j]:=true
a[i,j]:=false
for k:=1 to n do begin
for j:=1 to max_y do
for i:=1 to max_x do
if (a[i,j])
('*')
(' ')
for i:=1 to max_x do
for j:=1 to max_y do begin
s:=sosedi (a,i,j)
if a[i,j] and ((s=2) or (s=3))
a[i,j]:=true
Q1
К с.3
if a[i,j]and ((s=0)or (s=1))
Q1
На с.2
a[i,j]:=false
if a[i,j] and (s>=4)
a[i,j]:=false
if (not a[i,j]) and(s=3)
a[i,j]:=true
Конец
Листинг программы.
program lab15;
{$APPTYPE CONSOLE}
uses
SysUtils, Math;
const
max_x=80;
max_y=24;
type
pole=array[1..max_x, 1..max_y] of boolean;
function sosedi (a: pole ; x,y:integer):integer;
var
cnt:integer;
begin
cnt:=0;
if (x-1>0) and a[x-1,y] then cnt:=cnt+1;
if (x-1>0) and (y-1> 0) and a[x-1,y-1] then cnt:=cnt+1;
if (x-1>0) and (y+1<=max_y) and a[x-1,y+1] then cnt:=cnt+1;
if (x+1<=max_x) and a[x+1,y] then cnt:=cnt+1;
if (x+1<=max_x) and (y-1> 0) and a[x+1,y-1] then cnt:=cnt+1;
if (x+1<=max_x) and (y+1<=max_y) and a[x+1,y+1] then cnt:=cnt+1;
if (y-1> 0) and a[x,y-1] then cnt:=cnt+1;
if (y+1<=max_y) and a[x,y+1] then cnt:=cnt+1;
result:=cnt;
end;
var a:pole;
i,j,k,n,s:integer;
str:string;
begin
write('Put any quantity: ');
readln(n);
for j:=1 to max_y do
for i:=1 to max_x do
a[i,j]:=false;
for j:=1 to max_y do begin
readln (str);
for i:=1 to min(max_x,length(str)) do
if str[i]='*' then a[i,j]:=true
else a[i,j]:=false;
end;
for k:=1 to n do begin
for j:=1 to max_y do
for i:=1 to max_x do
if (a[i,j]) then write('*')
else write(' ');
for i:=1 to max_x do
for j:=1 to max_y do begin
s:=sosedi (a,i,j);
if a[i,j] and ((s=2) or (s=3)) then a[i,j]:=true;
if a[i,j] and ((s=0) or (s=1)) then a[i,j]:=false;
if a[i,j] and (s>=4) then a[i,j]:=false;
if (not a[i,j] )and (s=3) then a[i,j]:=true;
end;
readln;
end;
writeln('That is all. Please, press ENTER.');
readln;
end.