这个问题都不能解决那DELPHI就太差了 (100分)

  • 主题发起人 主题发起人 savetime
  • 开始时间 开始时间
S

savetime

Unregistered / Unconfirmed
GUEST, unregistred user!
我用C曾定义如下数据(俄罗斯方块数据),现想用PASCAL写出相应代码
/* SQUARE Structions */
struct
{
int Data [4][4][4] ;
/* [direction][y][x] */
COLORREF Color ;
}
gShapes [7] =
{
0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0,
0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0,
RGB(255, 0, 0), /* "|" */
0,1,1,0, 0,0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,1, 0,1,0,0, 0,0,0,0,
0,1,0,0, 0,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 0,0,1,0, 1,1,1,0, 0,0,0,0,
RGB(255, 0, 255), /* "L" */
0,1,1,0, 0,1,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,1,1, 0,0,0,0,
0,0,1,0, 0,0,1,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 1,1,1,0, 0,0,1,0, 0,0,0,0,
RGB(255, 128, 64), /* "J" */
0,0,1,0, 0,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 1,1,1,0, 0,1,0,0, 0,0,0,0,
0,1,0,0, 0,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 1,1,1,0, 0,0,0,0,
RGB(255, 255, 0), /* "JL" */
0,0,1,0, 0,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 0,0,1,1, 0,0,0,0,
0,0,1,0, 0,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 0,0,1,1, 0,0,0,0,
RGB( 0, 255, 255), /* "z" */
0,1,0,0, 0,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 1,1,0,0, 0,0,0,0,
0,1,0,0, 0,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 1,1,0,0, 0,0,0,0,
RGB( 0, 255, 0), /* "z" (R) */
0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0,
0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0,
RGB( 0, 128, 255) /* "88" */
} ;
用PASCAL定义如下记录:
type
TSquareData = record
Data: array[1..4, 1..4, 1..4] of Integer;
Color: TColor;
end;

想把数据初始化为全局常量,我实在不想用
const
SquareData: array[1..7] of TSquareData;
Square[1].Data[1,1,1] = 1;
Square[1].Data[1,1,2] = 0;
Square[1].Data[1,1,3] = 1;
.....
DELPHI有没有简单的Method(sorry, my computer's IME unexpected error, i can't input chinese now).
另外,为了精简内存,谁能将程序中的Integer0,1转换为Boolean方式表达而又使代码清晰?
深夜求救,明天还要上班,就看各位大侠的了。
 
好象有个 C 2 Pascal 的程序,好象在Delphi6th.com
 
用google search一下
 
Data:array[0..1] of TSquareData=(
((1,1,0,1),(1,0,0,1),(1,0,1,0)),
((0,1,1,1),(1,1,1,1),(0,0,0,0))
)
希望对你有所启发!
至于Bool转换,更简单:Boolean(1),boolean(0)即可,也可以integer(True),integer(False).
看不惯你的题目~~~~~~~~~~~~,拜托,自己不会的,不过说Delphi太差~~~~~~~~~~~~~~~
[:(!][:)]
 
to:Kingron
等了一天无人问,一改题目马上回!
抱歉!回去试一下,是这样就分分。
[:)]
 
呵呵,变个法儿使唤便宜人嘛
 

此兄乃学习心切也,呵呵……[:D]
 
delphi不是差!比起QBASIC来怎么样?比起VC++来当然不行了!可是你如果学精delphi也一样能出好程序!
 
[:(]还是不行啊老兄,初学DELPHI,请扶一把。
能不能讲一下DELPHI在初始化数组或结构时的代码规则,我没这方面的资料。
type
TSquareData = record
Color: TColor;
Data: array[1..4, 1..4, 1..4] of Integer;
end;

const
SquareData: array[1..7] of TSquareData =
((clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))));

Compilor ErrorInfo:
[Error] TetrisCls.pas(25): Undeclared identifier: 'clRed'
[Error] TetrisCls.pas(26): ')' expected but ',' found
[Error] TetrisCls.pas(30): '=' expected but ',' found
[Error] TetrisCls.pas(31): ')' expected but ',' found
[Error] TetrisCls.pas(35): '=' expected but ',' found
[Error] TetrisCls.pas(36): ')' expected but ',' found
[Error] TetrisCls.pas(35): Identifier redeclared: 'clRed'
[Error] TetrisCls.pas(36): ';' expected but ',' found
[Error] TetrisCls.pas(40): '=' expected but ',' found
[Error] TetrisCls.pas(41): ')' expected but ',' found
[Error] TetrisCls.pas(40): Identifier redeclared: 'clRed'
[Error] TetrisCls.pas(41): ';' expected but ',' found
[Error] TetrisCls.pas(45): '=' expected but ',' found
[Error] TetrisCls.pas(46): ')' expected but ',' found
[Error] TetrisCls.pas(45): Identifier redeclared: 'clRed'
[Error] TetrisCls.pas(46): ';' expected but ',' found
[Error] TetrisCls.pas(50): '=' expected but ',' found
[Error] TetrisCls.pas(51): ')' expected but ',' found
[Error] TetrisCls.pas(50): Identifier redeclared: 'clRed'
[Error] TetrisCls.pas(51): ';' expected but ',' found
[Error] TetrisCls.pas(55): '=' expected but ',' found
[Error] TetrisCls.pas(56): ')' expected but ',' found
[Error] TetrisCls.pas(55): Identifier redeclared: 'clRed'
[Error] TetrisCls.pas(56): ';' expected but ',' found
[Fatal Error] MainFrm.pas(7): Could not compile used unit 'TetrisCls.pas'
 
你在前面uses里加上Graphics吗?
 
加了,下面是单元代码,还没完成。[?]
unit TetrisCls;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;
type
TSquareData = record
Color: TColor;
Data: array[1..4, 1..4, 1..4] of Integer;
end;

TPlayer = class(TObject)
public
FGround: TImage;
constructor Create(AForm: TForm;
APlayer: Integer);
end;

const
GSquareSize = 20;
var
SquareData: array[1..7] of TSquareData =
((TColor(RGB(255,255,255)),
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))),
(clRed,
((0,0,1,0),(0,0,1,0),(0,0,1,0),(0,0,1,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0))));
implementation
constructor TPlayer.Create(AForm: TForm;
APlayer: Integer);
begin
FGround := TImage.Create(AForm);
with FGrounddo
begin
Parent := AForm;
Top := 10;
Height := GSquareSize * 20;
Width := GSquareSize * 10;
case APlayer of
1: Left := 10;
2: Left := AForm.Width - Width - 10;
else
ShowMessage('Error player define, the player number must be 1 or 2!');
end;
Canvas.Brush.Color := clBlack;
Canvas.Rectangle(0, 0, 200, 400);
end;
end;

initialization
end.
 
给个例子, 1..4 太麻烦了,见谅 :)
type
TSquareData = record
Color: TColor;
Data: array [1..2, 1..2] of Integer;
end;

const
SquareData: array [1..2] of TSquareData =
((Color:clRed;
Data:((1, 2), (1, 2))),
(Color:clRed;
Data:((1, 2), (1, 2))));
 
[8D]我觉得Boolean操作起来比较方便,但定义很麻烦,只好将数据定义为字符串,再写个函数将它初始化为Boolean.
还有几个问题,请大侠顺便解决一下。
(1)AnsiString变量的第一个字符为什么不是string[0],而是string[1]?
(2)我想将string[ ? ]中的字符1或0转换为Boolean有没有更好的办法?下面的方法太笨。
我用StrToInt(string[ ? ])会出错。为什么?
GSquareData.Fill[d, y, x] :=
Boolean(Integer(SquareDataString[s*64 + d*16 + y*4 + x + 1]) - 48);

//以下为原代码
const
GSquareCount = 7;
type
TGSquareData = record
Color: TColor;
Fill: array[0..3, 0..3, 0..3] of Boolean;
//Direction, y, x
end;

var
GSquareData: array[0..GSquareCount-1] of TGSquareData;
procedure InitGSquareData;
var
SquareDataString: string;
s, d, y, x: Integer;
begin
SquareDataString :=
'0010001000100010000011110000000000100010001000100000111100000000' +
'0110001000100000000001110100000001000100011000000000001011100000' +
'0110010001000000000001000111000000100010011000000000111000100000' +
'0010011000100000000011100100000001000110010000000000010011100000' +
'0010011001000000000001100011000000100110010000000000011000110000' +
'0100011000100000000001101100000001000110001000000000011011000000' +
'0000011001100000000001100110000000000110011000000000011001100000';
GSquareData[0].Color := clRed;
GSquareData[1].Color := clBlue;
GSquareData[2].Color := clGreen;
GSquareData[3].Color := clGreen;
GSquareData[4].Color := clYellow;
GSquareData[5].Color := clRed;
GSquareData[6].Color := clRed;
for s := 0 to GSquareCount - 1do
for d := 0 to 3do
for y := 0 to 3do
for x := 0 to 3do
GSquareData.Fill[d, y, x] :=
Boolean(Integer(SquareDataString[s*64 + d*16 + y*4 + x + 1]) - 48);
end;

initialization
InitGSquareData;
 
这里有个俄罗斯方块delphi代码
http://www.csdn.net/dev/Delphi/Samples/index.htm
我曾经将它汉化, 并将它的Timer改为Thdtimer控件来用, 挺不错的
Thdtimer控件在:
http://www.csdn.net/dev/Delphi/Vcl/datetime/index.htm
 
(1)String这个类型在Delphi中有特殊处理,String[-1]存放了字符串的引用计数,String[0]
存放字符串的长度。
(2)GSquareData.Fill[d, y, x] :=
(SquareDataString[s*64 + d*16 + y*4 + x + 1])='1';
 
to OpuBF:
GSquareData.Fill[d, y, x] :=
(SquareDataString[s*64 + d*16 + y*4 + x + 1])='1';
好!
分先给了,最后一个问题,有空就回一下。
String[0]存放字符串的长度,那么最长就是255,可ANSISTRING长度不限?
 
对不起,写错了 :)
应该是这样的,取长度 Integer(Pointer(Integer(Pointer(Str)-4))^),即使用了四个字节
来存储长度。改成-8就可以取到引用计数了。
Delphi中声明String并未指定具体的类型,其实在编译时有相应的转换。若有{+H}编译指令则
String对应着AnsiString,长度并非不限,由使用四个字节存储长度可知其大约可以达到2GB。
若使用{-H}则String对应了ShortString,这时就真正只有255个字符长了。剩下的就是WideString
了,从各方面来看WideString都与AnsiString相似,只不过WideString使用了Unicode编码。
希望对你有帮助 :)
 
后退
顶部