Открыть спойлер
Закрыть спойлер
- Program gameOfLife;
- Uses crt;
- Var
- cell,nextGen: array[1..39,1..39] Of Boolean;
- x,y,i,j,alertFlag: Integer;
- Key: char;
- buffer: boolean;
- Function scan(x,y:integer): boolean;
- Var n: 1..8;
- number: integer;
- Begin
- number := 0;
- For n:=1 To 8 Do
- Begin
- Case n Of
- 1: If ((x-1)>0)And((y-1)>0) Then
- Begin
- If cell[x-1][y-1] Then inc(number);
- End;
- 2: If y-1>0 Then
- Begin
- If cell[x][y-1] Then inc(number);
- End;
- 3: If ((x+1)<40)And((y-1)>0) Then
- Begin
- If cell[x+1][y-1] Then inc(number);
- End;
- 4: If x+1<40 Then
- Begin
- If cell[x+1][y] Then inc(number);
- End;
- 5: If ((x+1)<40) And ((y+1)<40) Then
- Begin
- If cell[x+1][y+1] Then inc(number);
- End;
- 6: If y+1<40 Then
- Begin
- If cell[x][y+1] Then inc(number);
- End;
- 7: If ((x-1)>1) And ((y+1)<40) Then
- Begin
- If cell[x-1][y+1] Then inc(number);
- End;
- 8: If x-1>0 Then
- Begin
- If cell[x-1][y] Then inc(number);
- End;
- End;
- If number=3 Then scan := true
- Else If number=2 Then
- Begin
- If cell[x][y] Then scan := true
- Else scan := false;
- End;
- End;
- If ((number<>2)And(number<>3)) Then scan := false;
- End;
- Begin
- For i:=1 To 39 Do
- Begin
- For j:=1 To 39 Do
- Begin
- cell[i][j] := false;
- End;
- End;
- x := 1;
- y := 1;
- TextMode(Lo(lastMode));
- While Key<>'k' Do
- Begin
- gotoXY(x,y);
- Key := readKey;
- If Key='w' Then y := y-1;
- If Key='s' Then y := y+1;
- If Key='a' Then x := x-1;
- If Key='d' Then x := x+1;
- If x>39 Then x := 1;
- If x<1 Then x := 39;
- If y>39 Then y := 1;
- If y<1 Then y := 39;
- If Key='x' Then
- Begin
- If cell[x][y] = true Then
- Begin
- cell[x][y] := false;
- write(' ');
- End
- Else
- Begin
- cell[x][y] := true;
- write('#');
- End;
- End;
- End;
- While true Do
- Begin
- For i:=1 To 39 Do
- Begin
- For j:=1 To 39 Do
- Begin
- buffer := scan(i,j);
- nextGen[i][j] := buffer;
- If buffer Then
- Begin
- gotoXY(i,j);
- write('#');
- gotoXY(1,1);
- End;
- If buffer<>true Then
- Begin
- gotoXY(i,j);
- write('.');
- gotoXY(1,1);
- End;
- End;
- End;
- delay(1000);
- For i:=1 To 39 Do
- Begin
- For j:=1 To 39 Do
- Begin
- cell[i][j] := nextGen[i][j];
- End;
- End;
- End;
- End.