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

  1. unit Gamebrd;
  2.  
  3. { Program copyright (c) 1995 by Charles Calvert }
  4. { Project Name: LIFE }
  5.  
  6. interface
  7.  
  8. uses
  9.   SysUtils, WinTypes, WinProcs,
  10.   Messages, Classes, Graphics,
  11.   Controls, Forms, Dialogs,
  12.   LifeDef, LifeMain;
  13.  
  14. type
  15.   TBoard = class(TForm)
  16.   private
  17.     procedure StartGame(var Msg: TMessage);
  18.       message wm_StartGame;
  19.   protected
  20.     procedure CreateParams(var Params: TCreateParams); override;
  21.   public
  22.     procedure CleanUp(var BoardInfo: TBoardInfo);
  23.     procedure Initialize(var BoardInfo: TBoardInfo;
  24.                          var GameInfo: TGameInfo;
  25.                          FormInit: TForm);
  26.   end;
  27.  
  28. var
  29.   Board: TBoard;
  30.  
  31. implementation
  32.  
  33. uses
  34.   ChsDlg,
  35.   GenLaws,
  36.   SetBoard,
  37.   Setting;
  38.  
  39. {$R *.DFM}
  40.  
  41. procedure PaintIt(var BoardInfo: TBoardInfo; GameInfo: TGameInfo; BoardToDraw: Byte);
  42. var
  43.   i,j,x,y : integer;
  44.   B1,B2,SaveB: HBrush;
  45. procedure DoDraw;
  46. begin
  47.   with BoardInfo do begin
  48.     if (Board^[BoardToDraw, i, j] = occupied) then begin
  49.       SaveB := SelectOBject(GameBrd.Board.Canvas.Handle, B1);
  50.       Rectangle(GameBrd.Board.Canvas.Handle, x, y, x + MicrobeSize, y + MicrobeSize);
  51.       SelectOBject(GameBrd.Board.Canvas.Handle, SaveB);
  52.     end else begin
  53.       SaveB := SelectOBject(GameBrd.Board.Canvas.Handle, B2);
  54.       Rectangle(GameBrd.Board.Canvas.Handle, x, y, x + MicrobeSize, y + MicrobeSize);
  55.       SelectOBject(GameBrd.Board.Canvas.Handle, SaveB);
  56.     end;
  57.   end;
  58. end;
  59.  
  60. begin
  61.   B1 := CreateSolidBrush(RGB(255,0,0));
  62.   B2 := CreateSolidBrush(RGB(0,0,255));
  63.   with BoardInfo do begin
  64.     for i:= 1 to SizeY do begin
  65.       for j:= 1 to SizeX do begin
  66.         x := j * MicrobeSize;
  67.         y := i * MicrobeSize;
  68.         if GameInfo.CheckForAltGen then CheckForDouble(i, j, BoardInfo, GameInfo);
  69.         if BoardInfo.Board^[New, i, j] <> BoardInfo.Board^[Old, i, j] then
  70.           DoDraw
  71.       end;
  72.     end
  73.   end; { with }
  74.   DeleteObject(B1);
  75.   DeleteObject(B2);
  76. end;
  77.  
  78. procedure Writeboard(var BoardInfo: TBoardInfo;
  79.                      GameInfo: TGameInfo; BoardToDraw: Byte);
  80. var
  81.   S: String;
  82. begin
  83.   S := ' Generation = ' + IntToStr(boardinfo.generation) + ' ';
  84.   PaintIt(BoardInfo, GameInfo, BoardToDraw);
  85. end; {Procedure WriteBoard}
  86.  
  87. procedure RunGame(var BoardInfo: TBoardInfo; var GameInfo: TGameInfo);
  88. begin
  89.   GameInfo.Change := True;
  90.   PaintFirstBoard(BoardInfo, BoardInfo.Old);
  91.   with boardinfo do begin
  92.     Generation := 1;
  93.     repeat
  94.       GeneticLaws(BoardInfo, GameInfo);  { Find out who lives and who dies }
  95.       WriteBoard(BoardInfo, GameInfo, New); { Draw to the screen           }
  96.       Inc(Generation);
  97.       SwapByte(Old, New);
  98.       { Save copy of board, alternate generations testing in GeniticLaws}
  99.       if GameInfo.Change = True then SaveBoard(BoardInfo);
  100.       if (State <> Double) and (State <> UserTermination) then State:= Growing;
  101.       if GameInfo.Alivecount = 0 then State := Dead;
  102.       if not GameInfo.Change then State:= Stable;
  103.       Application.ProcessMessages;
  104.       LifeMainForm.NumGens := Generation;
  105.       LifeMainForm.GameState := State;
  106.     until GameOver(State, Generation, GameInfo.MaxGen);
  107.   end; {with}
  108. end;
  109.  
  110. procedure MakeRules(var GameInfo: TGameInfo);
  111. begin
  112.   case GameInfo.GameType of
  113.     Classic: begin
  114.       GameInfo.Survival := [2, 3];
  115.       GameInfo.Death := [0, 1, 4, 5, 6, 7, 8];
  116.       GameInfo.Birth := [3];
  117.     end;
  118.  
  119.     ManyBirths: begin
  120.       GameInfo.Survival := [2, 3, 4];
  121.       GameInfo.Death := [0, 1, 5, 6,7, 8];
  122.       GameInfo.Birth := [3, 4];
  123.     end;
  124.  
  125.     MuchSurvival: begin
  126.       GameInfo.Survival := [2, 3, 4];
  127.       GameInfo.Death := [0, 1, 5, 6, 7, 8];
  128.       GameInfo.Birth := [3];
  129.     end;
  130.   end;
  131. end;
  132.  
  133. procedure TBoard.StartGame(var Msg: TMessage);
  134. var
  135.   Choice: Char;
  136. begin
  137.   if not ChooseDlg.ShowYourself(Choice) then Exit;
  138.   Initialize(BoardInfo, GameInfo, Self);
  139.   try
  140.     SetBrd.StartMethod(BoardInfo, GameInfo, Choice);
  141.   except
  142.     BoardInfo.State := Dead;     { Tell the program it didn't work }
  143.     raise;                       { ReRaise the error for default handling }
  144.   end;
  145.   RunGame(BoardInfo, GameInfo);
  146.   CleanUp(BoardInfo);
  147. end;
  148.  
  149. procedure TBoard.Initialize(var BoardInfo: TBoardInfo; var GameInfo: TGameInfo; FormInit: TForm);
  150. begin
  151.   BoardInfo.LifeForm := FormInit;
  152.   New(BoardInfo.Board);
  153.   New(BoardInfo.SaveSecond);
  154.   New(BoardInfo.SaveThird);
  155.   MakeRules(GameInfo);
  156.   GameInfo.StopProg := True;
  157.   BoardInfo.IsDouble := False;      { Generations not alternating.}
  158.   Boardinfo.State := Growing;
  159.   Boardinfo.Generation := 1;
  160.   BoardInfo.Old := 1;
  161.   BoardInfo.New := 2;
  162. end;
  163.  
  164. procedure TBoard.CleanUp(var BoardInfo: TBoardInfo);
  165. begin
  166.   Dispose(BoardInfo.Board);
  167.   Dispose(BoardInfo.SaveSecond);
  168.   Dispose(BoardInfo.SaveThird);
  169. end;
  170.  
  171. procedure TBoard.CreateParams(var Params: TCreateParams);
  172. begin
  173.   inherited CreateParams(Params);
  174.   Params.Style := Params.Style and not ws_Caption;
  175.   Params.Style := Params.Style and not ws_ThickFrame;
  176.   Params.Style := Params.Style or ws_Border;
  177. end;
  178.  
  179. end.
  180.