📄 main.~pas
字号:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus;
type
TForm1 = class(TForm)
Memo1: TMemo;
MainMenu1: TMainMenu;
Start1: TMenuItem;
N1: TMenuItem;
Replay1: TMenuItem;
Exit1: TMenuItem;
Options1: TMenuItem;
procedure FormPaint(Sender: TObject);
function GetRect(px,py:integer):Trect;
procedure GenerateRandom;
procedure FormCreate(Sender: TObject);
procedure DrawImage(px,py,nv:integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
function GetNotValue(iv:integer):integer;
function JudgeIsWin:boolean;
procedure Start1Click(Sender: TObject);
procedure Replay1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
nValue:array[0..5,0..5] of integer;
nCount:integer;
blEnd:boolean;
end;
const
WinSize=56;
nTimes=30;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormPaint(Sender: TObject);
var
i,j:integer;
begin
for i:=1 to 6 do begin
canvas.MoveTo(winsize,winsize*i);
canvas.LineTo(winsize*6,winsize*i);
canvas.MoveTo(winsize*i,winsize);
canvas.LineTo(winsize*i,winsize*6);
end;
for j:=1 to 5 do
for i:=1 to 5 do begin
drawimage(i,j,nvalue[i,j]);
end;
drawimage(7,2,0);
canvas.TextOut(7*winsize+trunc(winsize/16),3*winsize+4,'Closed');
drawimage(7,4,1);
canvas.TextOut(7*winsize+trunc(winsize/10),5*winsize+4,'Open');
end;
procedure TForm1.GenerateRandom;
var
i,j:integer;
begin
randomize;
for j:=1 to 5 do
for i:=1 to 5 do begin
nValue[i,j]:=random(2);
end;
end;
function TForm1.GetRect(px, py: integer): Trect;
begin
result:=rect(winsize*px,winsize*py,(winsize+1)*px,(winsize+1)*py);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
blEnd:=false;
generaterandom ;
ncount:=0;
end;
procedure TForm1.DrawImage(px, py, nv: integer);
var bit:Tbitmap;
begin
bit:=tbitmap.Create;
bit.LoadFromFile(inttostr(nv)+'.bmp');
canvas.Draw(px*winsize+2,py*winsize+2,bit);
bit.Free;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var px,py:integer;
begin
if winsize=0 then exit;
px:=trunc(x/winsize);
py:=trunc(y/winsize);
if not( (px>0) and (py>0) and (px<=5) and (py<=5)) then exit;
if nCount>=nTimes then exit;
if blend then exit;
nCount:=nCount+1;
nValue[px,py]:=getnotValue(nValue[px,py]);
if px>1 then nValue[px-1,py]:=getnotvalue(nValue[px-1,py]);
if px<5 then nValue[px+1,py]:=getnotvalue(nValue[px+1,py]);
if py>1 then nValue[px,py-1]:=getnotvalue(nValue[px,py-1]);
if py<5 then nValue[px,py+1]:=getnotvalue(nValue[px,py+1]);
self.Refresh;
self.Canvas.TextOut(winsize*1,trunc(winsize*6.3),'REMAIN TIMES : '+inttostr(ntimes-ncount));
if Judgeiswin then begin
showmessage('You Win!');
blend:=true;
exit;
end;
// self.Caption:='X= ' +inttostr(px) + ' Y = ' +inttostr(py);
end;
function TForm1.GetNotValue(iv:integer): integer;
begin
if iv=0 then result:=1 else result:=0 ;
end;
function TForm1.JudgeIsWin: boolean;
var i,j:integer;
begin
result:=true;
for j:=1 to 5 do
for i:=1 to 5 do
if nValue[i,j]=1 then begin
result:=false;
exit;
end;
end;
procedure TForm1.Start1Click(Sender: TObject);
begin
blEnd:=false;
nCount:=0;
generaterandom;
self.Refresh;
end;
procedure TForm1.Replay1Click(Sender: TObject);
begin
if application.MessageBox('Are you sure to replay?','System Info',MB_YESNO)=IDNO then exit;
blEnd:=false;
nCount:=0;
generaterandom;
self.Refresh;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -