home *** CD-ROM | disk | FTP | other *** search
- unit GenLaws;
-
- { Program copyright (c) 1995 by Charles Calvert }
- { Project Name: LIFE }
-
- { This is where the basic genetic laws are actually calculated. Using
- a double loop structure, the program counts through each square in
- the matrix, comparing the state of the old and new squares.
-
- The first law is implemented by asking if the position in question
- on the old board is empty and if number is in birth.
- Number is calculated in countneighbors. The only number in birth is 3.
- If these conditions are met the position on the new board is occupied.
- Alivecount is incremented. The board has been changed.
-
- The second law asks if the position on the old board was occupied and
- if number is now in Death. The numbers for Death are 0 1 4 5 6 7 and 8.
- If these conditions are met the position on the new board is empty.
- The board has been changed.
-
- The third law asks if the old board position was occupied and if the
- number is in survival. The numbers for survival are two and three.
- If these conditions are met the position on the new board is occupied.
- Alivecount is incremented. But board does not change. }
-
- interface
-
- uses
- Dialogs,
- LifeDef;
-
- procedure GeneticLaws (var boardinfo : TBoardInfo; var GameInfo: TGameInfo);
- function CheckForDouble(i,j: Integer; var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo): Boolean;
- implementation
-
- { Look for the edges of the Board. Size is BoardInfo.Size }
- procedure CheckLocation(var Up, Down, Left, Right: Integer;
- x, y, SizeX, SizeY: Integer);
- begin
- if x > 1 then Left := -1
- else Left := 0;
-
- if x < SizeX then Right := 1
- else Right := 0;
-
- if y > 1 then Up := -1
- else Up := 0;
-
- if y < SizeY then Down := 1
- else Down := 0;
- end;
-
- { Determine the number of neighbors. }
- procedure CountNeighbor(Boardinfo: TBoardInfo;
- x, y:Integer; var Count: Integer);
- var
- Down, Left, Right, Up: Integer;
- i,j: Integer;
- begin
- Count := 0;
- with BoardInfo do begin
- CheckLocation(Up, Down, Left, Right, X, Y, BoardInfo.SizeX, BoardInfo.SizeY);
- for i := Left to Right do
- for j := Up to Down do
- if (BoardInfo.board^[BoardInfo.old, y + j, x + i] = Occupied) and
- not((i = 0) and (j = 0)) then
- count := count + 1
- end
- end;
-
- { See if they are born }
- procedure LawNumberOne(i, j: Integer; var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo);
- begin
- with BoardInfo do begin
- if (Board^[old,i,j] = Empty) and
- (GameInfo.NumNeighbors in GameInfo.Birth) then begin
- Board^[New,i,j] := Occupied;
- GameInfo.Change := true;
- Inc(GameInfo.AliveCount);
- end;
- end;
- end;
-
- { See if they've died }
- procedure LawNumberTwo(i, j: Integer; var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo);
- begin
- if (BoardInfo.Board^[BoardInfo.Old,i,j] = Occupied) and
- (GameInfo.NumNeighbors in GameInfo.Death) then
- GameInfo.Change := true
- end;
-
- { See if they Survive }
- procedure LawNumberThree(i, j: Integer; var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo);
- begin
- with BoardInfo do begin
- if (Board^[Old,i,j] = Occupied) and
- (GameInfo.NumNeighbors in GameInfo.Survival) then begin
- Board^[New,i,j] := Occupied;
- Inc(GameInfo.AliveCount);
- end;
- end;
- end;
-
- { Check for Alternating Generations }
- function CheckForDouble(i,j: Integer; var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo): Boolean;
- begin
- with BoardInfo do begin
- if BoardInfo.IsDouble then State := Double; { Double til proven otherwise}
- if Board^[New,i,j] = SaveSecond^[i,j] then { do nothing }
- else begin
- State := Growing; {Can't be a double so for now its growing}
- IsDouble := False; {Can't change back automatically}
- end;
- end;
- end;
-
- procedure GeneticLaws (var BoardInfo : TBoardInfo; var GameInfo: TGameInfo);
- var
- i,j: Integer;
- begin
- with BoardInfo do begin
- GameInfo.AliveCount := 0;
- GameInfo.Change := False;
- if GameInfo.CheckForAltGen then IsDouble := True;
- for i:= 1 to SizeY do begin
- for j:= 1 to SizeX do begin
- CountNeighbor(BoardInfo, j, i, GameInfo.NumNeighbors);
- Board^[New, i, j] := Empty;
- LawNumberOne(i, j, BoardInfo, GameInfo);
- LawNumberTwo(i, j, BoardInfo, GameInfo);
- LawNumberThree(i, j, BoardInfo, GameInfo);
- if GameInfo.CheckForAltGen then
- CheckForDouble(i, j, BoardInfo, GameInfo);
- end;
- end;
- end; {with}
- end; { GeneticLaws}
-
- end.
-