home *** CD-ROM | disk | FTP | other *** search
- unit Setboard;
-
- { Program copyright (c) 1995 by Charles Calvert }
- { Project Name: LIFE }
-
- { This is where we set up the board to begin the game. Our
- options are:
- ReadBoard: The user picks coordinates ( Not Implemented )
- FeedBoard: The machine picks random coordinates
- FileMethod: An old board is read from disk
- }
-
- interface
-
- uses
- WinTypes, WinProcs, Classes,
- Graphics, Forms, Controls,
- StdCtrls, Tabs, LifeDef,
- Buttons, ExtCtrls, SysUtils, Dialogs;
-
- type
- TSetBrd = class(TForm)
- OpenDialog1: TOpenDialog;
- private
- function OpenReadFile(var Infile: TBoardFile; FileName: String): Boolean;
- function ReadFile(Var Target: TBoardType): Boolean;
- procedure QuickWrite(FileName: String; Target: TBoardType);
- procedure FeedBoard(var boardinfo: TBoardInfo; var GameInfo: TGameInfo);
- function FileMethod(var BoardInfo: TBoardInfo; var GameInfo: TGameInfo): Boolean;
- public
- procedure StartMethod(var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo; Option: Char);
- end;
-
- var
- SetBrd: TSetBrd;
-
- implementation
-
- {$R *.DFM}
-
- function TSetBrd.OpenReadFile(var Infile: TBoardFile; FileName: String): Boolean;
- var
- S: String;
- begin
- OpenReadFile := True;
- try
- System.Assign(infile, filename);
- Reset(Infile);
- except
- S := 'Could not open file: ' + FileName;
- MessageDlg(S, mtError, [mbOk], 0);
- OpenReadFile := False;
- end;
- end;
-
-
- function CreateFile(var OutFile: TBoardFile; FileName: String): Boolean;
- var
- S: string;
- begin
- CreateFile := True;
- {$I-}
- Assign(OutFile, FileName + '.lif'); { Extension identifies files. }
- ReWrite(OutFile);
- {$I+}
- if IOResult <> 0 then begin
- S := 'Could not create file: ' + UpperCase(FileName + '.lif ') +
- #13'(Disk Full? Current directory a CD drive?)';
- MessageDlg(S, mtError, [mbOk], 0);
- CreateFile := False;
- end;
- end;
-
- procedure TSetBrd.QuickWrite(FileName: String; Target: TBoardType);
- var
- OutFile: TBoardFile;
- begin
- if CreateFile(OutFile, FileName) then begin
- Write(OutFile, Target);
- System.Close(OutFile);
- end;
- end;
-
- { This is an alternative to readboard which feeds to the box random
- numbers specifying board coordinates. The key line here is for i := 1
- to result, with result being the figure the user passes in. }
- procedure TSetBrd.FeedBoard(var Boardinfo: TBoardInfo; var GameInfo: TGameInfo);
- var
- i,j : integer;
- x,y : integer;
- FooX, FooY : integer;
- NumCoords: Integer;
- S: string;
- begin
- S := '';
- if not InputQuery('Computer Generation', 'Number of Coordinates', S) then Exit;
- NumCoords := StrToInt(S);
- FooX := BoardInfo.SizeX;
- FooY := BoardInfo.SizeY;
- with boardinfo do begin
- {initially set all elements in old board to empty}
- ClearOldBoard(BoardInfo);
- Randomize;
- x := Random(FooX);
- if x < 1 then x := 2;
- y := Random(fooY);
- if y < 1 then y := 2;
- for i := 1 to NumCoords do begin
- BoardInfo.board^[old,y,x]:= occupied;
- x := Random(fooX);
- if x < 1 then x := 5;
- y := Random(fooY);
- if y < 1 then y := 15;
- end
- end;
- end;
-
- function TSetBrd.ReadFile(Var Target: TBoardType): Boolean;
- var
- InFile: TBoardFile;
- FileName: String;
-
- begin
- ReadFile := False;
- if OpenDialog1.Execute then
- if OpenReadFile(InFile, OpenDialog1.FileName) then begin
- Read(Infile, Target);
- System.Close(Infile);
- ReadFile := True;
- end;
- end; {read_File}
-
- function TSetBrd.FileMethod(var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo): Boolean;
- begin
- FileMethod := True;
- with BoardInfo do begin
- ClearOldBoard(BoardInfo);
- if not ReadFile(Board^) then FileMethod := False;
- end;
- end;
-
- { Sort out whether computer or user chooses coordinates.
- 1. Computer chooses
- 2. User fills in coordinates
- 3. Get coordinates from file,
- if we can't get file, then exit }
- procedure TSetBrd.StartMethod(var BoardInfo: TBoardInfo;
- var GameInfo: TGameInfo; Option: Char);
- begin
- case option of
- '1': SetBrd.FeedBoard(BoardInfo, GameInfo);
- '2': MessageDlg('Not Implemented', mtInformation, [mbOk], 0);
- '3': if not SetBrd.FileMethod(BoardInfo, GameInfo) then Exit;
- end;
- SetBrd.QuickWrite('BackUp', BoardInfo.Board^);
- end;
-
- end.
-