home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programming Unleashed / Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso / misc / life / lifemain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-03-21  |  5.3 KB  |  213 lines

  1. unit Lifemain;
  2.  
  3. { Program copyright (c) 1995 by Charles Calvert }
  4. { Project Name: LIFE }
  5.  
  6. { The Game of Life
  7.   Programmer: Charles Calvert
  8.   Version 1.0 : February 1990.
  9.   Version 2.0 : December 1992
  10.   Version 3.0 : March 1995 ( Delphi )
  11.  
  12.   When you select the GO button, you can chooes to have the
  13.   computer select coordinates for you, or you may choose
  14.   coordinates yourself ( not implemented ), or you may load
  15.   coordinates from a file. In the first case the user is asked
  16.   how many random coordinates the computer should supply.
  17.  
  18.   At the beginning of every game, the coordinates are written to a
  19.   file called BACKUP.LIF. After the game, the user may use the
  20.   FileManager, etc, to rename this file, if he or she wants to
  21.   preserve the run. I'm currently not attempting to preserve
  22.   the size of the board.
  23.  
  24.   After the colony dies or reaches a stable state, the run ends.
  25.  
  26.   The program has an adjustable board size and adjustable grid size.
  27.   For instance, you can maximize the program and work with a larger
  28.   grid.
  29.  
  30.   If you press Alt S you can change the size of the grid, and
  31.   turn on and off the alternating generations detection. }
  32.  
  33. interface
  34.  
  35.  
  36. uses
  37.   WinTypes, WinProcs, Classes,
  38.   Messages, Controls, Graphics,
  39.   Printers, LifeDef, Tabs,
  40.   Forms, Buttons, StdCtrls,
  41.   ExtCtrls;
  42.  
  43.  
  44. type
  45.   TLifeMainForm = class(TForm)
  46.     TabControl1: TTabSet;
  47.     Panel1: TPanel;
  48.     Label2: TLabel;
  49.     Label3: TLabel;
  50.     SPStop: TBitBtn;
  51.     SPGo: TBitBtn;
  52.     SPExit: TBitBtn;
  53.     Label4: TPanel;
  54.     Label5: TPanel;
  55.     procedure GetSubWinRect(var R: TRect);
  56.     procedure TabControl1Click(Sender: TObject);
  57.     procedure FormResize(Sender: TObject);
  58.     procedure SPGoClick(Sender: TObject);
  59.     procedure SPStopClick(Sender: TObject);
  60.     procedure SPExitClick(Sender: TObject);
  61.     procedure FormCreate(Sender: TObject);
  62.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  63.     procedure TabControl1Change(Sender: TObject; NewTab: Integer;
  64.       var AllowChange: Boolean);
  65.   private
  66.     procedure SetGameState(GameState: TState);
  67.     procedure SetNumGens(i: Integer);
  68.     procedure InitGame(var Msg: TMessage);
  69.       Message wm_InitGame;
  70.   public
  71.   published
  72.     property GameState: TState write SetGameState;
  73.     property NumGens: Integer write SetNumGens;
  74.   end;
  75.  
  76. var
  77.   LifeMainForm: TLifeMainForm;
  78.  
  79. implementation
  80.  
  81. uses
  82.   Boarder,
  83.   GameBrd,
  84.   Setting,
  85.   SysUtils;
  86.  
  87. {$R *.DFM}
  88.  
  89. procedure TLifeMainForm.GetSubWinRect(var R: TRect);
  90. var
  91.   Border: Integer;
  92.   Bounds: TRect;
  93. begin
  94.   Border := GetSystemMetrics(SM_CXFRAME);
  95.   Bounds := GetClientRect;
  96.   TabControl1.Left := Bounds.Left + 10;
  97.   TabControl1.Width := Bounds.Right - 20;
  98.   TabControl1.Top := Bounds.Bottom - 40;
  99.   TabControl1.Height := 30;
  100.   R.Left := TabControl1.Left;
  101.   R.Top := Panel1.Height + 10;
  102.   R.Right := TabControl1.Width + (2 * Border) + 2;
  103.   R.Bottom := TabControl1.Top;
  104. end;
  105.  
  106. procedure TLifeMainForm.TabControl1Click(Sender: TObject);
  107. begin
  108. {  if TabControl1.Tabs[TabControl1.TabIndex] = 'Settings Page' then begin
  109.     Settings.BringToFront;
  110.   end;
  111.   if TabControl1.Tabs[TabControl1.TabIndex] = 'Game Board' then
  112.     Board.BringToFront;                  }
  113. end;
  114.  
  115. procedure TLifeMainForm.InitGame(var Msg: TMessage);
  116. var
  117.   R: TRect;
  118. begin
  119.   GetSubWinRect(R);
  120.   Settings.Show;
  121.   Settings.BoundsRect := R;
  122.   Settings.TabsOn(False);
  123.   Board.Show;
  124.   Board.BoundsRect := R;
  125. end;
  126.  
  127. procedure TLifeMainForm.FormResize(Sender: TObject);
  128. var
  129.   R: TRect;
  130. begin
  131.   GetSubWinRect(R);
  132.   if Settings <> nil then
  133.     Settings.BoundsRect := R;
  134.   if Board <> nil then
  135.     Board.BoundsRect := R;
  136.   Settings.FindBestSize;
  137. end;
  138.  
  139. procedure TLifeMainForm.SPGoClick(Sender: TObject);
  140. begin
  141.   if BoardInfo.State in [Dead, Stable, Double, UserTermination] then begin
  142.     PostMessage(Board.Handle, wm_StartGame, 0, 0);
  143.     Board.Show;
  144.     TabControl1.TabIndex := 0;
  145.   end;
  146. end;
  147.  
  148. procedure TLifeMainForm.SPStopClick(Sender: TObject);
  149. begin
  150.   BoardInfo.State := UserTermination;
  151. end;
  152.  
  153. procedure TLifeMainForm.SPExitClick(Sender: TObject);
  154. begin
  155.   BoardInfo.State := UserTermination;
  156.   Close;
  157. end;
  158.  
  159. procedure TLifeMainForm.FormCreate(Sender: TObject);
  160. var
  161.   BoardPlayer: TBoardPlay;
  162. begin
  163.   Label4.Caption := '';
  164.   Label5.Caption := '';
  165.   Board := TBoard.Create(Self);
  166.   Board.Parent := Self;
  167.   Settings := TSettings.Create(Self);
  168.   Settings.Parent := Self;
  169.  
  170.   PostMessage(Handle, wm_InitGame, 0, 0);
  171.   Settings.SetStartState;
  172. end;
  173.  
  174. procedure TLifeMainForm.FormClose(Sender: TObject;
  175.   var Action: TCloseAction);
  176. begin
  177.   Settings.Free;
  178.   Board.Free;
  179. end;
  180.  
  181. procedure TLifeMainForm.SetGameState(GameState: TState);
  182. var
  183.   S: string;
  184. begin
  185. {  Label5.Caption := GetEnumName(GameState,i); } { Where's TYPEINFO? }
  186.   case GameState of
  187.     Dead: S := 'Dead';
  188.     Stable: S := 'Stable';
  189.     Growing: S := 'Growing';
  190.     Double: S := 'Double';
  191.     UserTermination: S := 'UserTermination';
  192.   end;
  193.   Label5.Caption := S;
  194. end;
  195.  
  196. procedure TLifeMainForm.SetNumGens(i: Integer);
  197. begin
  198.   Label4.Caption := IntToStr(i);
  199. end;
  200.  
  201. procedure TLifeMainForm.TabControl1Change(Sender: TObject; NewTab: Integer;
  202.   var AllowChange: Boolean);
  203. begin
  204.   if TabControl1.TabIndex = 0 then
  205.     Settings.BringToFront
  206.   else
  207.     Board.BringToFront;
  208.  
  209.   Settings.TabsOn(not Boolean(TabControl1.TabIndex));
  210. end;
  211.  
  212. end.
  213.