azaghal |
19-05-2006 04:48:47 |
Problema con una TStringGrid
Bueno, realmente no se q pase, lo he checado mil veces y no se q anda mal, alguien me puede ayudar. Les dejo todo el codigo y tambien les dejo todos los botones q tengo. Espero alguien me ayude. Gracias.
El Programa simula el trafico de autos.
Código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Edit1: TEdit;
StringGrid2: TStringGrid;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Columnas, veces, Renglones : Integer;
begin
Hint := 'Da valores iniciales a la primer cadena';
Randomize;
for Renglones := 0 to StringGrid1.RowCount - 1 do
for Columnas := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[Columnas,Renglones] := IntToStr(0);
for Columnas := 1 to StringGrid1.ColCount - 1 do
Begin
veces := Random(9) + 1;
If (veces = 1) and (StrToInt(StringGrid1.Cells[Columnas-1,0]) = 0)then
StringGrid1.Cells[Columnas,0] := IntToStr(Random(5) + 1);
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var
Velocidad : string;
Renglon : integer;
begin
with StringGrid1 do
begin
for renglon := 0 to RowCount - 1 do
begin
Velocidad := Cells[ACol,ARow];
if (Arow=Renglon) and (Velocidad='0') then
begin
Canvas.Brush.Color:=ClMaroon;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='1') then
begin
Canvas.Brush.Color:=Clred;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='2') then
begin
Canvas.Brush.Color:=Clblue;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='3') then
begin
Canvas.Brush.Color:=Clgreen;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='4') then
begin
Canvas.Brush.Color:=Clyellow;
Canvas.FillRect(rect);
end;
if (Arow=Renglon) and (Velocidad='5') then
begin
Canvas.Brush.Color:=Clgray;
Canvas.FillRect(rect);
end;
end;
end;
end;
procedure TForm1.StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var
Velocidad : string;
begin
with StringGrid2 do
begin
Velocidad := Cells[ACol,ARow];
if (ACol=1) and (Velocidad='1') then
begin
Canvas.Brush.Color:=Clred;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='2') then
begin
Canvas.Brush.Color:=Clblue;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='3') then
begin
Canvas.Brush.Color:=Clgreen;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='4') then
begin
Canvas.Brush.Color:=Clyellow;
Canvas.FillRect(rect);
end;
if (ACol=1) and (Velocidad='5') then
begin
Canvas.Brush.Color:=Clgray;
Canvas.FillRect(rect);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
Velocidad, Renglon : Integer;
begin
Velocidad := 0;
for Renglon := 0 to StringGrid2.RowCount - 1 do
Begin
Velocidad := Velocidad + 1;
StringGrid2.Cells[0,Renglon]:= IntToStr(Velocidad);
StringGrid2.Cells[1,Renglon]:= IntToStr(Velocidad);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button4Click(Sender: TObject);
VAR
Columnas2, Espacio, i, automoviles : Integer;
VelocidadV : ARRAY[0..100] OF Integer;
Automoviles2, J, Probabilidad, VelMax : Integer;
Columnas, NewVel, Spacio, Renglones : Integer;
BEGIN
Randomize;
FOR Renglones := 0 TO StringGrid1.RowCount - 1 DO
BEGIN
FOR Columnas2 := 0 TO StringGrid1.ColCount - 1 DO
BEGIN
IF StringGrid1.Cells[Columnas2,Renglones] = '1' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '2' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '3' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '4' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '5' THEN
BEGIN
VelocidadV[Columnas2] := StrToInt(StringGrid1.Cells[Columnas2,Renglones]);
Memo2.Lines.Add(IntToStr(Columnas2));
END
ELSE IF StringGrid1.Cells[Columnas2,Renglones] = '0' THEN
Espacio := Espacio + 1;
IF StringGrid1.Cells[Columnas2,Renglones] <> '0' THEN
BEGIN
Memo3.Lines.Add(StringGrid1.Cells[Columnas2,Renglones]);
Automoviles := Automoviles + 1;
END;
Edit1.Text := IntToStr( automoviles );
END;
FOR i:= 0 TO StringGrid1.Colcount - 1 DO
Memo1.Lines.Add(IntToStr(VelocidadV[i]));
VelMax := 5 ;
Automoviles2 := StrToInt(Edit1.Text) - 1;
FOR J := 0 TO Automoviles2 DO
BEGIN
IF J = Automoviles2 THEN
BEGIN
NewVel := StrToInt(Memo3.Lines[j]);
Spacio := 79 - StrToInt(Memo2.Lines[J]);
IF Spacio > VelMax THEN
Spacio := VelMax;
IF Spacio <= NewVel THEN
NewVel := Spacio;
Probabilidad := Random(5) + 1;
IF (Probabilidad = 1) AND (NewVel > 1) THEN
BEGIN
NewVel := NewVel - 1;
ShowMessage('La velocidad ha sido disminuida en 1');
END;
Columnas := StrToInt(Memo2.Lines[J]) + NewVel;
IF Columnas < 79 THEN //Aqui
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel)
ELSE
BEGIN
Columnas := NewVel;
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel);
END; //Termina
END
ELSE
BEGIN
NewVel := StrToInt(Memo3.Lines[j]);
Spacio := StrToInt(Memo2.Lines[j+1]) - StrToInt(Memo2.Lines[J]) - 1;
IF Spacio > 5 THEN
Spacio := 5;
IF Spacio <= NewVel THEN
NewVel := Spacio;
Probabilidad := Random(5) + 1;
IF (Probabilidad = 1) AND (NewVel > 1) THEN
BEGIN
NewVel := NewVel - 1;
ShowMessage('La velocidad ha sido disminuida en 1');
END;
Columnas := StrToInt(Memo2.Lines[J]) + NewVel;
IF Columnas < 79 THEN //Aqui
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel)
ELSE
BEGIN
Columnas := NewVel;
StringGrid1.Cells[Columnas,Renglones + 1] := IntToStr(NewVel);
END; //Termina
END;
END;
Memo1.Clear;
Memo2.Clear;
Memo3.Clear;
END;
END;
END.

|