临
临火
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我写的扫雷主要代码,请各路高手评点一下!
程序把单个的地雷作为类(TBomb)来处理,hideflag=0~8,bomb;showflag是其他的标记
写这个类时,一直困扰我的是TBomb存在的理由——:给我一个理由,好不好(星仔)
不过它存在了,她够简单够明了!
程序从创建雷区,到左击翻开,右击标记红旗,双击推开一片,然后结束。代码中附有解释。
procedure TForm1.FormCreate(Sender: TObject);
var
i,j,k,ran,row,col:integer;
hideflagarray:array[0..bombrow-1,0..bombcol-1] of integer;
begin
//从资源中载入地雷的运行显示位图
showflagbitmap:=tbomb.LoadShowFlagBitmap;
hideflagbitmap:=tbomb.LoadHideFlagBitmap;
//设定雷区中每个雷的hideflag,然后创建她create,再画出她onpaint
for i:=0 to bombrow-1do
for j:=0 to bombcol-1do
hideflagarray[i,j]:=0;
k:=bombnumber;
randomize;
repeat
ran:=random(bombareasize);
row:=ran div bombcol;
col:=ran mod bombcol;
if hideflagarray[row,col]<>-1 then
begin
hideflagarray[row,col]:=-1;
dec(k);
for i:=-1 to 1do
for j:=-1 to 1do
begin
if (row+i>-1) and (row+i<bombrow) and
(col+j>-1) and (col+j<bombcol) then
if hideflagarray[row+i,col+j]<>-1 then
inc(hideflagarray[row+i,col+j]);
end;
end;
until k=0;
for i:=0 to bombrow-1do
for J:=0 to bombcol-1do
begin
bombarray[i*bombcol+j]:=tbomb.create(hfzero,i*BOMBheight,j*BOMBwidth);
end;
end;
procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
//判定按键,在按下时记录
if button=mbright then
begin
rbutton:=true;
with bombarray[(y div bombheight)*bombcol+(x div bombwidth)]do
begin
ChangeShowFlag;
draw(form1.Canvas,showflagbitmap);
end;
end else
begin
if button=mbleft then
begin
lbutton:=true;
end;
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
i,j:integer;
begin
for i:=0 to bombrow-1do
for j:=0 to bombcol-1do
bombarray[i*bombcol+j].Draw(form1.Canvas ,showflagbitmap);
end;
procedure TForm1.FormMouseUp(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
var
row,col:integer;
i,j,count:integer;
whf:whideflag;
begin
//左键翻开地雷,双击按规则处理
row:=y div bombheight;
col:=x div bombwidth;
if lbutton and rbutton then
begin
whf:=bombarray[row*bombcol+col].gethideflag;
count:=0;
for i:=-1 to 1do
for j:=-1 to 1do
begin
if (row+i>-1) and (row+i<bombrow) and
(col+j>-1) and (col+j<bombcol) then
//index:=(row+i)*bombcol+(col+j);
with bombarray[(row+i)*bombcol+(col+j)]do
begin
if isredflag then
inc(count);
end;
end;
if whideflag(count)=whf then
begin
for i:=-1 to 1do
for j:=-1 to 1do
if (row+i>-1) and (row+i<bombrow) and
(col+j>-1) and (col+j<bombcol) then
with bombarray[(row+i)*bombcol+(col+j)]do
begin
if not isredflag then
uncover(row+i,col+j);
end;
end;
lbutton:=false;
rbutton:=false;
end else
begin
if button=mbleft then
begin
lbutton:=false;
with bombarray[(y div bombheight)*bombcol+(x div bombwidth)]do
begin
uncover(row,col);
end;
end else
begin
if button=mbright then
rbutton:=false;
end;
end;
end;
procedure TForm1.Uncover(ARow, Acol: integer);
var
i,j:integer;
brow,bcol:integer;
begin
//翻开地雷,为地雷结束,为0则推开
with bombarray[arow*bombcol+acol]do
begin
Draw(form1.Canvas,hideflagbitmap);
if gethideflag=hfbomb then
showmessage('Hello!');
if gethideflag=hfzero then
begin
for i:=-1 to 1do
for j:=-1 to 1do
begin
if (arow+i>-1) and (arow+i<bombrow) and
(acol+j>-1) and (acol+j<bombcol) then
begin
brow:=arow+i;
bcol:=acol+j;
uncover(brow,bcol);
end;
end;
end;
end;
end;
如果你看到这里了,请一定留下你的高见!
程序把单个的地雷作为类(TBomb)来处理,hideflag=0~8,bomb;showflag是其他的标记
写这个类时,一直困扰我的是TBomb存在的理由——:给我一个理由,好不好(星仔)
不过它存在了,她够简单够明了!
程序从创建雷区,到左击翻开,右击标记红旗,双击推开一片,然后结束。代码中附有解释。
procedure TForm1.FormCreate(Sender: TObject);
var
i,j,k,ran,row,col:integer;
hideflagarray:array[0..bombrow-1,0..bombcol-1] of integer;
begin
//从资源中载入地雷的运行显示位图
showflagbitmap:=tbomb.LoadShowFlagBitmap;
hideflagbitmap:=tbomb.LoadHideFlagBitmap;
//设定雷区中每个雷的hideflag,然后创建她create,再画出她onpaint
for i:=0 to bombrow-1do
for j:=0 to bombcol-1do
hideflagarray[i,j]:=0;
k:=bombnumber;
randomize;
repeat
ran:=random(bombareasize);
row:=ran div bombcol;
col:=ran mod bombcol;
if hideflagarray[row,col]<>-1 then
begin
hideflagarray[row,col]:=-1;
dec(k);
for i:=-1 to 1do
for j:=-1 to 1do
begin
if (row+i>-1) and (row+i<bombrow) and
(col+j>-1) and (col+j<bombcol) then
if hideflagarray[row+i,col+j]<>-1 then
inc(hideflagarray[row+i,col+j]);
end;
end;
until k=0;
for i:=0 to bombrow-1do
for J:=0 to bombcol-1do
begin
bombarray[i*bombcol+j]:=tbomb.create(hfzero,i*BOMBheight,j*BOMBwidth);
end;
end;
procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
//判定按键,在按下时记录
if button=mbright then
begin
rbutton:=true;
with bombarray[(y div bombheight)*bombcol+(x div bombwidth)]do
begin
ChangeShowFlag;
draw(form1.Canvas,showflagbitmap);
end;
end else
begin
if button=mbleft then
begin
lbutton:=true;
end;
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
i,j:integer;
begin
for i:=0 to bombrow-1do
for j:=0 to bombcol-1do
bombarray[i*bombcol+j].Draw(form1.Canvas ,showflagbitmap);
end;
procedure TForm1.FormMouseUp(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
var
row,col:integer;
i,j,count:integer;
whf:whideflag;
begin
//左键翻开地雷,双击按规则处理
row:=y div bombheight;
col:=x div bombwidth;
if lbutton and rbutton then
begin
whf:=bombarray[row*bombcol+col].gethideflag;
count:=0;
for i:=-1 to 1do
for j:=-1 to 1do
begin
if (row+i>-1) and (row+i<bombrow) and
(col+j>-1) and (col+j<bombcol) then
//index:=(row+i)*bombcol+(col+j);
with bombarray[(row+i)*bombcol+(col+j)]do
begin
if isredflag then
inc(count);
end;
end;
if whideflag(count)=whf then
begin
for i:=-1 to 1do
for j:=-1 to 1do
if (row+i>-1) and (row+i<bombrow) and
(col+j>-1) and (col+j<bombcol) then
with bombarray[(row+i)*bombcol+(col+j)]do
begin
if not isredflag then
uncover(row+i,col+j);
end;
end;
lbutton:=false;
rbutton:=false;
end else
begin
if button=mbleft then
begin
lbutton:=false;
with bombarray[(y div bombheight)*bombcol+(x div bombwidth)]do
begin
uncover(row,col);
end;
end else
begin
if button=mbright then
rbutton:=false;
end;
end;
end;
procedure TForm1.Uncover(ARow, Acol: integer);
var
i,j:integer;
brow,bcol:integer;
begin
//翻开地雷,为地雷结束,为0则推开
with bombarray[arow*bombcol+acol]do
begin
Draw(form1.Canvas,hideflagbitmap);
if gethideflag=hfbomb then
showmessage('Hello!');
if gethideflag=hfzero then
begin
for i:=-1 to 1do
for j:=-1 to 1do
begin
if (arow+i>-1) and (arow+i<bombrow) and
(acol+j>-1) and (acol+j<bombcol) then
begin
brow:=arow+i;
bcol:=acol+j;
uncover(brow,bcol);
end;
end;
end;
end;
end;
如果你看到这里了,请一定留下你的高见!