提示出错"index was outside the bounds of the array"是什么意思? ( 积分: 23 )

  • 主题发起人 主题发起人 wdycwopt
  • 开始时间 开始时间
W

wdycwopt

Unregistered / Unconfirmed
GUEST, unregistred user!
提示出错"index was outside the bounds of the array"是什么意思?

我想设计一个打井游戏,用drawgrid,在drawgrid上落子出现了这个提示.


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, System.ComponentModel, Borland.Vcl.Grids, Borland.Vcl.StdCtrls,
Borland.Vcl.ExtCtrls;

type
TForm1 = class(TForm)
DrawGrid1: TDrawGrid;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Shape1: TShape;
Shape2: TShape;
GroupBox2: TGroupBox;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
GroupBox3: TGroupBox;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
btrestart: TButton;
btclose: TButton;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure btrestartClick(Sender: TObject);
procedure btcloseClick(Sender: TObject);
procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure DrawGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);



private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
isplayer1:boolean=true;
playing:boolean=false;
chess:array [0..190,0..190] of integer;
currenttime:tdatetime;
player1time,player2time:double;
implementation

procedure initiate;
var
i,j:integer;
begin
for i:=0 to 2 do
for j:=0 to 2 do
chess[i,j]:=0;
form1.Label5.Caption:='0:00:00';
form1.Label6.Caption:='0:00:00';
player1time:=0;
player2time:=0;
currenttime:=time;
end;


function win(x,y:integer):boolean;
var
i,j,who,countfive:integer;
begin
result:=false;
who:=chess[x,y];
if (chess[x,y+1]=who) or (chess[x,y-1]=who) then
begin
countfive:=1;
j:=y+1;
while (chess[x,j]=who) and (j<=2) do
begin
inc(countfive);
inc(j);
end;
j:=y-1;
while (chess[x,j]=who) and (j>=0) do
begin
inc(countfive);
dec(j);
end;
if countfive=3 then
result:=true;
end;
if (chess[x+1,y]=who) or (chess[x-1,y]=who) then
begin
countfive:=1;
i:=x+1;
while (chess[i,y]=who) and (i<=2) do
begin
inc(countfive);
inc(i);
end;
i:=x-1;
while (chess[i,y]=who) and (i>=0) do
begin
inc(countfive);
dec(i);
end;
if countfive=3 then
result:=true;
end;
if (chess[x+1,y+1]=who) or (chess[x-1,y-1]=who) then
begin
countfive:=1;
i:=x+1;
j:=y+1;
while (chess[i,j]=who) and (i<=2) and (j<=2) do
begin
inc(countfive);
inc(i);
inc(j);
end;
i:=x-1;
j:=y-1;
while (chess[i,j]=who) and (i>=0) and (j>=0) do
begin
inc(countfive);
dec(i);
dec(j);
end;
if countfive=3 then
result:=true;
end;
if (chess[x+1,y-1]=who) or (chess[x-1,y+1]=who) then
begin
countfive:=1;
i:=x+1;
j:=y-1;
while (chess[i,j]=who) and (i<=2) and (j>=0) do
begin
inc(countfive);
inc(i);
dec(j);
end;
i:=x-1;
j:=y+1;
while (chess[i,j]=who) and (i>=0) and (j<=2) do
begin
inc(countfive);
dec(i);
inc(j);
end;
if countfive=3 then
result:=true;
end;
end;

{$R *.nfm}

procedure TForm1.btrestartClick(Sender: TObject);
begin
initiate;
drawgrid1.Refresh;
playing:=true;
timer1.Enabled:=true;
btrestart.Enabled:=false;
end;

procedure TForm1.btcloseClick(Sender: TObject);
begin
close;
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if chess[acol,arow]=1 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clblack;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21+1,(acol+1)*21-1,(arow+1)*21-1);
end
else
if chess[acol,arow]=2 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clwhite;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21-1,(acol+1)*21-1,(arow+1)*21-1);
end;
end;

procedure TForm1.DrawGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
acol,arow:integer;
begin
drawgrid1.MouseToCell(x,y,acol,arow);
if playing then
if isplayer1 then
begin
if chess[acol,arow]=0 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clblack;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21+1,(acol+1)*21-1,(arow+1)*21-1);
chess[acol,arow]:=1;
isplayer1:=false;
if win(acol,arow) then
begin
label9.Caption:=inttostr(strtoint(label9.Caption)+1);
timer1.Enabled:=false;
playing:=false;
btrestart.Enabled:=true;
showmessage('player 1 win!');
end;
end
else showmessage('此处不能落子!');
end
else
begin
if chess[acol,arow]=0 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clwhite;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21+1,(acol+1)*21-1,(arow+1)*21-1);
chess[acol,arow]:=2;
isplayer1:=true;
if win(acol,arow) then
begin
label10.Caption:=inttostr(strtoint(label10.Caption)+1);
timer1.Enabled:=false;
playing:=false;
btrestart.Enabled:=true;
showmessage('player 2 win!');
end;
end
else showmessage('此处不能落子!');
end;
end;





procedure TForm1.Timer1Timer(Sender: TObject);
var
temptime:tdatetime;
begin
temptime:=time;
if isplayer1 then
begin
if label1.Font.Color=clblack then
label1.Font.Color:=clred
else
label1.Font.Color:=clblack;
label2.Font.Color:=clblack;
player1time:=player1time+(temptime-currenttime);
label5.Caption:=timetostr(player1time);
end
else
begin
label1.Font.Color:=clblack;
if label2.Font.Color=clblack then
label2.Font.Color:=clred
else
label2.Font.Color:=clblack;
player2time:=player2time+(temptime-currenttime);
label6.Caption:=timetostr(player2time);
end;
currenttime:=temptime;
end;

end.
 
提示出错"index was outside the bounds of the array"是什么意思?

我想设计一个打井游戏,用drawgrid,在drawgrid上落子出现了这个提示.


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, System.ComponentModel, Borland.Vcl.Grids, Borland.Vcl.StdCtrls,
Borland.Vcl.ExtCtrls;

type
TForm1 = class(TForm)
DrawGrid1: TDrawGrid;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Shape1: TShape;
Shape2: TShape;
GroupBox2: TGroupBox;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
GroupBox3: TGroupBox;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
btrestart: TButton;
btclose: TButton;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure btrestartClick(Sender: TObject);
procedure btcloseClick(Sender: TObject);
procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure DrawGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);



private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
isplayer1:boolean=true;
playing:boolean=false;
chess:array [0..190,0..190] of integer;
currenttime:tdatetime;
player1time,player2time:double;
implementation

procedure initiate;
var
i,j:integer;
begin
for i:=0 to 2 do
for j:=0 to 2 do
chess[i,j]:=0;
form1.Label5.Caption:='0:00:00';
form1.Label6.Caption:='0:00:00';
player1time:=0;
player2time:=0;
currenttime:=time;
end;


function win(x,y:integer):boolean;
var
i,j,who,countfive:integer;
begin
result:=false;
who:=chess[x,y];
if (chess[x,y+1]=who) or (chess[x,y-1]=who) then
begin
countfive:=1;
j:=y+1;
while (chess[x,j]=who) and (j<=2) do
begin
inc(countfive);
inc(j);
end;
j:=y-1;
while (chess[x,j]=who) and (j>=0) do
begin
inc(countfive);
dec(j);
end;
if countfive=3 then
result:=true;
end;
if (chess[x+1,y]=who) or (chess[x-1,y]=who) then
begin
countfive:=1;
i:=x+1;
while (chess[i,y]=who) and (i<=2) do
begin
inc(countfive);
inc(i);
end;
i:=x-1;
while (chess[i,y]=who) and (i>=0) do
begin
inc(countfive);
dec(i);
end;
if countfive=3 then
result:=true;
end;
if (chess[x+1,y+1]=who) or (chess[x-1,y-1]=who) then
begin
countfive:=1;
i:=x+1;
j:=y+1;
while (chess[i,j]=who) and (i<=2) and (j<=2) do
begin
inc(countfive);
inc(i);
inc(j);
end;
i:=x-1;
j:=y-1;
while (chess[i,j]=who) and (i>=0) and (j>=0) do
begin
inc(countfive);
dec(i);
dec(j);
end;
if countfive=3 then
result:=true;
end;
if (chess[x+1,y-1]=who) or (chess[x-1,y+1]=who) then
begin
countfive:=1;
i:=x+1;
j:=y-1;
while (chess[i,j]=who) and (i<=2) and (j>=0) do
begin
inc(countfive);
inc(i);
dec(j);
end;
i:=x-1;
j:=y+1;
while (chess[i,j]=who) and (i>=0) and (j<=2) do
begin
inc(countfive);
dec(i);
inc(j);
end;
if countfive=3 then
result:=true;
end;
end;

{$R *.nfm}

procedure TForm1.btrestartClick(Sender: TObject);
begin
initiate;
drawgrid1.Refresh;
playing:=true;
timer1.Enabled:=true;
btrestart.Enabled:=false;
end;

procedure TForm1.btcloseClick(Sender: TObject);
begin
close;
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if chess[acol,arow]=1 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clblack;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21+1,(acol+1)*21-1,(arow+1)*21-1);
end
else
if chess[acol,arow]=2 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clwhite;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21-1,(acol+1)*21-1,(arow+1)*21-1);
end;
end;

procedure TForm1.DrawGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
acol,arow:integer;
begin
drawgrid1.MouseToCell(x,y,acol,arow);
if playing then
if isplayer1 then
begin
if chess[acol,arow]=0 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clblack;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21+1,(acol+1)*21-1,(arow+1)*21-1);
chess[acol,arow]:=1;
isplayer1:=false;
if win(acol,arow) then
begin
label9.Caption:=inttostr(strtoint(label9.Caption)+1);
timer1.Enabled:=false;
playing:=false;
btrestart.Enabled:=true;
showmessage('player 1 win!');
end;
end
else showmessage('此处不能落子!');
end
else
begin
if chess[acol,arow]=0 then
begin
drawgrid1.Canvas.Pen.Color:=clblack;
drawgrid1.Canvas.Brush.Color:=clwhite;
drawgrid1.Canvas.Ellipse(acol*21+1,arow*21+1,(acol+1)*21-1,(arow+1)*21-1);
chess[acol,arow]:=2;
isplayer1:=true;
if win(acol,arow) then
begin
label10.Caption:=inttostr(strtoint(label10.Caption)+1);
timer1.Enabled:=false;
playing:=false;
btrestart.Enabled:=true;
showmessage('player 2 win!');
end;
end
else showmessage('此处不能落子!');
end;
end;





procedure TForm1.Timer1Timer(Sender: TObject);
var
temptime:tdatetime;
begin
temptime:=time;
if isplayer1 then
begin
if label1.Font.Color=clblack then
label1.Font.Color:=clred
else
label1.Font.Color:=clblack;
label2.Font.Color:=clblack;
player1time:=player1time+(temptime-currenttime);
label5.Caption:=timetostr(player1time);
end
else
begin
label1.Font.Color:=clblack;
if label2.Font.Color=clblack then
label2.Font.Color:=clred
else
label2.Font.Color:=clblack;
player2time:=player2time+(temptime-currenttime);
label6.Caption:=timetostr(player2time);
end;
currenttime:=temptime;
end;

end.
 
数组越界了。
 

Similar threads

后退
顶部