home *** CD-ROM | disk | FTP | other *** search
- unit Lifemain;
-
- { Program copyright (c) 1995 by Charles Calvert }
- { Project Name: LIFE }
-
- { The Game of Life
- Programmer: Charles Calvert
- Version 1.0 : February 1990.
- Version 2.0 : December 1992
- Version 3.0 : March 1995 ( Delphi )
-
- When you select the GO button, you can chooes to have the
- computer select coordinates for you, or you may choose
- coordinates yourself ( not implemented ), or you may load
- coordinates from a file. In the first case the user is asked
- how many random coordinates the computer should supply.
-
- At the beginning of every game, the coordinates are written to a
- file called BACKUP.LIF. After the game, the user may use the
- FileManager, etc, to rename this file, if he or she wants to
- preserve the run. I'm currently not attempting to preserve
- the size of the board.
-
- After the colony dies or reaches a stable state, the run ends.
-
- The program has an adjustable board size and adjustable grid size.
- For instance, you can maximize the program and work with a larger
- grid.
-
- If you press Alt S you can change the size of the grid, and
- turn on and off the alternating generations detection. }
-
- interface
-
-
- uses
- WinTypes, WinProcs, Classes,
- Messages, Controls, Graphics,
- Printers, LifeDef, Tabs,
- Forms, Buttons, StdCtrls,
- ExtCtrls;
-
-
- type
- TLifeMainForm = class(TForm)
- TabControl1: TTabSet;
- Panel1: TPanel;
- Label2: TLabel;
- Label3: TLabel;
- SPStop: TBitBtn;
- SPGo: TBitBtn;
- SPExit: TBitBtn;
- Label4: TPanel;
- Label5: TPanel;
- procedure GetSubWinRect(var R: TRect);
- procedure TabControl1Click(Sender: TObject);
- procedure FormResize(Sender: TObject);
- procedure SPGoClick(Sender: TObject);
- procedure SPStopClick(Sender: TObject);
- procedure SPExitClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure TabControl1Change(Sender: TObject; NewTab: Integer;
- var AllowChange: Boolean);
- private
- procedure SetGameState(GameState: TState);
- procedure SetNumGens(i: Integer);
- procedure InitGame(var Msg: TMessage);
- Message wm_InitGame;
- public
- published
- property GameState: TState write SetGameState;
- property NumGens: Integer write SetNumGens;
- end;
-
- var
- LifeMainForm: TLifeMainForm;
-
- implementation
-
- uses
- Boarder,
- GameBrd,
- Setting,
- SysUtils;
-
- {$R *.DFM}
-
- procedure TLifeMainForm.GetSubWinRect(var R: TRect);
- var
- Border: Integer;
- Bounds: TRect;
- begin
- Border := GetSystemMetrics(SM_CXFRAME);
- Bounds := GetClientRect;
- TabControl1.Left := Bounds.Left + 10;
- TabControl1.Width := Bounds.Right - 20;
- TabControl1.Top := Bounds.Bottom - 40;
- TabControl1.Height := 30;
- R.Left := TabControl1.Left;
- R.Top := Panel1.Height + 10;
- R.Right := TabControl1.Width + (2 * Border) + 2;
- R.Bottom := TabControl1.Top;
- end;
-
- procedure TLifeMainForm.TabControl1Click(Sender: TObject);
- begin
- { if TabControl1.Tabs[TabControl1.TabIndex] = 'Settings Page' then begin
- Settings.BringToFront;
- end;
- if TabControl1.Tabs[TabControl1.TabIndex] = 'Game Board' then
- Board.BringToFront; }
- end;
-
- procedure TLifeMainForm.InitGame(var Msg: TMessage);
- var
- R: TRect;
- begin
- GetSubWinRect(R);
- Settings.Show;
- Settings.BoundsRect := R;
- Settings.TabsOn(False);
- Board.Show;
- Board.BoundsRect := R;
- end;
-
- procedure TLifeMainForm.FormResize(Sender: TObject);
- var
- R: TRect;
- begin
- GetSubWinRect(R);
- if Settings <> nil then
- Settings.BoundsRect := R;
- if Board <> nil then
- Board.BoundsRect := R;
- Settings.FindBestSize;
- end;
-
- procedure TLifeMainForm.SPGoClick(Sender: TObject);
- begin
- if BoardInfo.State in [Dead, Stable, Double, UserTermination] then begin
- PostMessage(Board.Handle, wm_StartGame, 0, 0);
- Board.Show;
- TabControl1.TabIndex := 0;
- end;
- end;
-
- procedure TLifeMainForm.SPStopClick(Sender: TObject);
- begin
- BoardInfo.State := UserTermination;
- end;
-
- procedure TLifeMainForm.SPExitClick(Sender: TObject);
- begin
- BoardInfo.State := UserTermination;
- Close;
- end;
-
- procedure TLifeMainForm.FormCreate(Sender: TObject);
- var
- BoardPlayer: TBoardPlay;
- begin
- Label4.Caption := '';
- Label5.Caption := '';
- Board := TBoard.Create(Self);
- Board.Parent := Self;
- Settings := TSettings.Create(Self);
- Settings.Parent := Self;
-
- PostMessage(Handle, wm_InitGame, 0, 0);
- Settings.SetStartState;
- end;
-
- procedure TLifeMainForm.FormClose(Sender: TObject;
- var Action: TCloseAction);
- begin
- Settings.Free;
- Board.Free;
- end;
-
- procedure TLifeMainForm.SetGameState(GameState: TState);
- var
- S: string;
- begin
- { Label5.Caption := GetEnumName(GameState,i); } { Where's TYPEINFO? }
- case GameState of
- Dead: S := 'Dead';
- Stable: S := 'Stable';
- Growing: S := 'Growing';
- Double: S := 'Double';
- UserTermination: S := 'UserTermination';
- end;
- Label5.Caption := S;
- end;
-
- procedure TLifeMainForm.SetNumGens(i: Integer);
- begin
- Label4.Caption := IntToStr(i);
- end;
-
- procedure TLifeMainForm.TabControl1Change(Sender: TObject; NewTab: Integer;
- var AllowChange: Boolean);
- begin
- if TabControl1.TabIndex = 0 then
- Settings.BringToFront
- else
- Board.BringToFront;
-
- Settings.TabsOn(not Boolean(TabControl1.TabIndex));
- end;
-
- end.
-