有什么办法可以根据具体情况select具体的case语句吗?
Is there any way to select specific case statements based on specific situations?
Direction := TDirection(Random(Succ(Ord(High(TDirection)))));
case Direction of
up:
begin
CurrentCell := maze[i, j - 1];
CurrentCell.Wall := false;
end;
down:
begin
CurrentCell := maze[i, j + 1];
CurrentCell.Wall := false;
end;
left:
begin
CurrentCell := maze[i - 1, j];
CurrentCell.Wall := false;
end;
right:
begin
CurrentCell := maze[i + 1, j];
CurrentCell.Wall := false;
end;
我基本上有一个称为迷宫 ([0..19, 0.19]
) 的二维数组,其中从 maze[0,0]
中随机选择一个方向。如果指针 CurrentCell
位于 maze
边缘的任何单元格中,即第 0 列、第 0 行、第 19 行和第 19 列,则某些方向无法拾取。我的问题是,如果 CurrentCell
指针在上面列出的任何行和列中,我有什么方法可以告诉程序从哪个方向随机化?
路线是用枚举创建的
TDirection = (up, down, left, right);
var
Direction : TDirection;
根据当前坐标,您可以确定哪些方向可用:累积可用方向,然后随机选择一个。例如:
var
..
DirList: TList<TDirection>;
begin
..
DirList := TList<TDirection>.Create;
try
if i > 0 then
DirList.Add(mdUp);
if i < 19 then
DirList.Add(mdDown);
if j > 0 then
DirList.Add(mdRight);
if j < 19 then
DirList.Add(mdLeft);
case DirList[Random(DirList.Count)] of
mdUp: CurrentCell := maze[i, j - 1];
mdDown: CurrentCell := maze[i, j + 1];
mdLeft: CurrentCell := maze[i - 1, j];
mdRight: CurrentCell := maze[i + 1, j];
end;
CurrentCell.Wall := False;
...
没有通用列表,它看起来像这样:
var
..
DirList: TList;
begin
..
DirList := TList.Create;
try
if i > 0 then
DirList.Add(Pointer(mdUp));
if i < 19 then
DirList.Add(Pointer(mdDown));
if j < 19 then
DirList.Add(Pointer(mdLeft));
if j > 0 then
DirList.Add(Pointer(mdRight));
Direction := TDirection(DirList[Random(DirList.Count)]);
...
我真的很想用一组来做这个,这会更适合上下文,但它需要助手从中随机化。
回答标题问题需要阅读原文post。答案是否定的,在 case
被编译为二进制文件后,无法在 运行 时影响单个分支的存在。您也不能影响分支值,它们必须是在编译时解析的常量。幸运的是,这根本不是必需的,毕竟您在 运行 时间决定了选择器将保留什么值,因此将选择哪个分支。
将每个可能的方向添加到 TDirection
的数组中,然后从数组中随机选择一个元素。
这是一个函数,输入是迷宫边界和实际位置:
Type
TDirection = (up, down, left, right);
TMazeRect = record lowX,lowY,highX,highY : integer; end;
const
MyMazeRect : TMazeRect = (lowX:0;lowY:0;highX:19;highY:19);
function RandomRestrictedDirection( const area : TMazeRect; posX,posY : Integer) : TDirection;
var
dirArray : array[0..3] of TDirection;
count : Integer;
begin
count := 0;
if (posY < area.highY) then begin
dirArray[count] := up;
Inc(count);
end;
if (posY > area.lowY) then begin
dirArray[count] := down;
Inc(count);
end;
if (posX > area.lowX) then begin
dirArray[count] := left;
Inc(count);
end;
if (posX < area.highX) then begin
dirArray[count] := right;
Inc(count);
end;
Result := dirArray[Random(count)];
end;
这样称呼它:
NewDirection := RandomRestrictedDirection(MyMazeRect,i,j);
Direction := TDirection(Random(Succ(Ord(High(TDirection)))));
case Direction of
up:
begin
CurrentCell := maze[i, j - 1];
CurrentCell.Wall := false;
end;
down:
begin
CurrentCell := maze[i, j + 1];
CurrentCell.Wall := false;
end;
left:
begin
CurrentCell := maze[i - 1, j];
CurrentCell.Wall := false;
end;
right:
begin
CurrentCell := maze[i + 1, j];
CurrentCell.Wall := false;
end;
我基本上有一个称为迷宫 ([0..19, 0.19]
) 的二维数组,其中从 maze[0,0]
中随机选择一个方向。如果指针 CurrentCell
位于 maze
边缘的任何单元格中,即第 0 列、第 0 行、第 19 行和第 19 列,则某些方向无法拾取。我的问题是,如果 CurrentCell
指针在上面列出的任何行和列中,我有什么方法可以告诉程序从哪个方向随机化?
路线是用枚举创建的
TDirection = (up, down, left, right);
var
Direction : TDirection;
根据当前坐标,您可以确定哪些方向可用:累积可用方向,然后随机选择一个。例如:
var
..
DirList: TList<TDirection>;
begin
..
DirList := TList<TDirection>.Create;
try
if i > 0 then
DirList.Add(mdUp);
if i < 19 then
DirList.Add(mdDown);
if j > 0 then
DirList.Add(mdRight);
if j < 19 then
DirList.Add(mdLeft);
case DirList[Random(DirList.Count)] of
mdUp: CurrentCell := maze[i, j - 1];
mdDown: CurrentCell := maze[i, j + 1];
mdLeft: CurrentCell := maze[i - 1, j];
mdRight: CurrentCell := maze[i + 1, j];
end;
CurrentCell.Wall := False;
...
没有通用列表,它看起来像这样:
var
..
DirList: TList;
begin
..
DirList := TList.Create;
try
if i > 0 then
DirList.Add(Pointer(mdUp));
if i < 19 then
DirList.Add(Pointer(mdDown));
if j < 19 then
DirList.Add(Pointer(mdLeft));
if j > 0 then
DirList.Add(Pointer(mdRight));
Direction := TDirection(DirList[Random(DirList.Count)]);
...
我真的很想用一组来做这个,这会更适合上下文,但它需要助手从中随机化。
回答标题问题需要阅读原文post。答案是否定的,在 case
被编译为二进制文件后,无法在 运行 时影响单个分支的存在。您也不能影响分支值,它们必须是在编译时解析的常量。幸运的是,这根本不是必需的,毕竟您在 运行 时间决定了选择器将保留什么值,因此将选择哪个分支。
将每个可能的方向添加到 TDirection
的数组中,然后从数组中随机选择一个元素。
这是一个函数,输入是迷宫边界和实际位置:
Type
TDirection = (up, down, left, right);
TMazeRect = record lowX,lowY,highX,highY : integer; end;
const
MyMazeRect : TMazeRect = (lowX:0;lowY:0;highX:19;highY:19);
function RandomRestrictedDirection( const area : TMazeRect; posX,posY : Integer) : TDirection;
var
dirArray : array[0..3] of TDirection;
count : Integer;
begin
count := 0;
if (posY < area.highY) then begin
dirArray[count] := up;
Inc(count);
end;
if (posY > area.lowY) then begin
dirArray[count] := down;
Inc(count);
end;
if (posX > area.lowX) then begin
dirArray[count] := left;
Inc(count);
end;
if (posX < area.highX) then begin
dirArray[count] := right;
Inc(count);
end;
Result := dirArray[Random(count)];
end;
这样称呼它:
NewDirection := RandomRestrictedDirection(MyMazeRect,i,j);