home *** CD-ROM | disk | FTP | other *** search
- unit LifeDef;
-
- { Program copyright (c) 1995 by Charles Calvert }
- { Project Name: LIFE }
-
- {-------------------------------}
- {---} Interface {---}
- {-------------------------------}
-
- uses
- Forms,
- Messages,
- WinProcs,
- WinTypes;
-
- const
- WM_InitGame = WM_USER;
- WM_AllDone = WM_USER + 1;
- WM_StartGame = WM_USER + 2;
- ESCape = #27;
- CarriageReturn = #13;
- DownArrow = #80;
- UpArrow = #72;
- LeftArrow = #75;
- RightArrow = #77;
- F1Key = #59;
- F2Key = #60;
- F3Key = #61;
-
- MaxSize = 100;
- YBAttr = 14 + 1 * 16;
- StatusLine = 24;
-
- type
- CellState = (Empty, Occupied);
- TGameType = (Classic, ManyBirths, MuchSurvival);
- Neighbor = set of 0..8;
-
- PBoardType = ^TBoardType;
- TBoardtype = array[1..2, 1..MaxSize, 1..MaxSize] of cellstate;
-
- PSaveBoard = ^TSaveBoard;
- TSaveBoard = array[1..MaxSize, 1..MaxSize] of cellstate;
-
-
- {1..2 holds the old and new boards. The third board, saveboard, is
- used to detect alternating generations. The 1..MaxSize parts refer to
- the actual board coordinates. When checking for alternating generations
- I save the copy of the new board in the board called SaveSecond. }
-
- TState = (Dead, Stable, Growing, Double, UserTermination);
-
- TBoardInfo = record
- LifeForm: TForm;
- MicrobeSize: Byte;
- Board : PBoardtype;
- Generation : Integer;
- New: Byte;
- Old: Byte;
- SizeX: 1..MaxSize;
- SizeY: 1..MaxSize;
- State: TState;
- IsDouble: Boolean; { False the moment we know its not a double }
- SaveSecond: PSaveBoard; { Save a copy of board to check for repeats }
- SaveThird: PSaveBoard; { Save to check for third generation repeats }
- end;
-
- TGameInfo = Record
- GameType: TGameType;
- MaxGen: Integer;
- Alivecount: Integer;
- Change: Boolean;
- Birth: Neighbor;
- Death: Neighbor;
- Survival: Neighbor;
- NumNeighbors: Integer; { Number is number of neighbors, a key fact.}
- CheckForAltGen: Boolean; { Check for alternating generations }
- StopProg : boolean; {Make menu run}
- end;
-
- TBoardFile = file of TBoardType;
-
- procedure PaintFirstBoard(var BoardInfo: TBoardInfo; BoardToDraw: Byte);
- function GameOver(State: TState; Gen: Integer; MaxGen: Integer): Boolean;
- procedure ClearOldBoard(var BoardInfo: TBoardInfo);
- procedure SaveBoard(var BoardInfo: TBoardInfo);
- procedure SwapByte(var Old, New: Byte);
-
- var
- GameInfo: TGameInfo;
- BoardInfo: TBoardInfo;
-
- {-------------------------}
- {---} implementation {---}
- {-------------------------}
-
- procedure PaintFirstBoard(var BoardInfo: TBoardInfo; BoardToDraw: Byte);
- var
- i,j,x,y : integer;
- B1,B2,SaveB: HBrush;
- begin
- B1 := CreateSolidBrush(RGB(255,0,0));
- B2 := CreateSolidBrush(RGB(0,0,255));
- with BoardInfo do begin
- for i:= 1 to SizeY do begin
- for j:= 1 to sizeX do begin
- x := j * MicrobeSize;
- y := i * MicrobeSize;
- if (Board^[BoardToDraw, i, j] = occupied) then begin
- SaveB := SelectOBject(LifeForm.Canvas.Handle, B1);
- Rectangle(LifeForm.Canvas.Handle, x, y, x + MicrobeSize, y + MicrobeSize);
- SelectOBject(LifeForm.Canvas.Handle, SaveB);
- end else begin
- SaveB := SelectOBject(LifeForm.Canvas.Handle, B2);
- Rectangle(LifeForm.Canvas.Handle, x, y, x + MicrobeSize, y + MicrobeSize);
- SelectOBject(LifeForm.Canvas.Handle, SaveB);
- end;
- end;
- end
- end; { with }
- DeleteObject(B1);
- DeleteObject(B2);
- end;
-
- function GameOver(State: TState; Gen: Integer; MaxGen: Integer): Boolean;
- begin
- GameOver := False;
- if (State in [Dead,Stable,Double,UserTermination]) or (Gen >= MaxGen) then
- GameOver := True;
- end;
-
- procedure ClearOldBoard(var BoardInfo: TBoardInfo);
- var
- i,j : Integer;
- begin
- with BoardInfo do
- FillChar(Board^[BoardInfo.Old], SizeOf(Board^[BoardInfo.Old]), Empty);
- end;
-
- procedure SaveBoard(var BoardInfo: TBoardInfo);
- begin
- with BoardInfo do
- move(Board^[new], SaveSecond^, SizeOf(SaveSecond^));
- end;
-
- procedure SwapByte(var Old, New: Byte);
- var
- Temp: Byte;
- begin
- Temp := Old;
- Old:= New;
- New := Temp;
- end;
-
- end.
-