如何随机化Java“;如果;是执行语句顺序还是执行顺序代码?
我正在编写Pacman游戏中幽灵实体的深度优先搜索算法。鬼魂必须寻找一条通往吃豆人的路。但即使有一条通往Pacman的路径,深度优先搜索算法 不会总是找到它,因为if语句是按顺序排列的如何随机化Java“;如果;是执行语句顺序还是执行顺序代码?,java,algorithm,depth-first-search,Java,Algorithm,Depth First Search,我正在编写Pacman游戏中幽灵实体的深度优先搜索算法。鬼魂必须寻找一条通往吃豆人的路。但即使有一条通往Pacman的路径,深度优先搜索算法 不会总是找到它,因为if语句是按顺序排列的 private boolean searchPath( int[][] maze, int x, int y, List<Point> path ) { System.out.println( "Inky.searchPath: " + "(" + x
private boolean searchPath(
int[][] maze, int x, int y, List<Point> path )
{
System.out.println( "Inky.searchPath: " + "(" + x + "," + y + ")" );
if ( !grid.isValid( new Location( x, y ) ) )
{
System.out.println( "Inky.searchPath: Location is not valid" );
return false;
}
//Target
if ( maze[x][y] == 9 )
{
path.add( new Point( x, y ) );
return true;
}
//Changing point to point visited
if ( maze[x][y] == 0 )
{
maze[x][y] = 2;
//Need something that executes these if statements at random
//Searching one block down
if ( searchPath( maze, x - 1, y, path ) )
{
path.add( new Point( x, y ) );
return true;
}
//Searching one block up
if ( searchPath( maze, x + 1, y, path ) )
{
path.add( new Point( x, y ) );
return true;
}
//Searching one block left
if ( searchPath( maze, x, y - 1, path ) )
{
path.add( new Point( x, y ) );
return true;
}
//Searching one block right
if ( searchPath( maze, x, y + 1, path ) )
{
path.add( new Point( x, y ) );
return true;
}
}
System.out.println("Inky.searchPath: Path not found");
return false;
}
专用布尔搜索路径(
int[][]迷宫,int x,int y,列表路径)
{
System.out.println(“Inky.searchPath:“+”(“+x+”,“+y+”));
如果(!grid.isValid(新位置(x,y)))
{
System.out.println(“Inky.searchPath:位置无效”);
返回false;
}
//目标
if(迷宫[x][y]==9)
{
添加(新点(x,y));
返回true;
}
//更改点到点访问
if(迷宫[x][y]==0)
{
迷宫[x][y]=2;
//需要随机执行这些if语句的东西吗
//搜索一个街区
if(搜索路径(迷宫,x-1,y,路径))
{
添加(新点(x,y));
返回true;
}
//搜索一个街区
if(搜索路径(迷宫,x+1,y,路径))
{
添加(新点(x,y));
返回true;
}
//搜索左一个街区
if(搜索路径(迷宫,x,y-1,路径))
{
添加(新点(x,y));
返回true;
}
//在右边一个街区搜索
if(搜索路径(迷宫,x,y+1,路径))
{
添加(新点(x,y));
返回true;
}
}
System.out.println(“Inky.searchPath:未找到路径”);
返回false;
}
正如您所看到的,if语句的顺序是
1.向下搜索一个街区
2.搜索一个街区
3.向左搜索一个街区
4.向右搜索一个街区
因此,如果重影进入左侧有一个打开的块的情况,它将一直移动到左侧没有打开的块,然后第二步将开始,重影将再次返回到它开始的位置,因为第二步搜索X-1
所以,我的问题是,有没有一种方法可以随机执行if语句的顺序?要随机方向,您可以:
//introduce a collection of directions
private List<int[]> directions = Arrays.asList (new int[][] {{1,0},{-1,0}, {0,1}, {0,-1}}); //right, left, down, up
//use it
Collections.shuffle(directions);
for(int[] dir : directions){
boolean result = searchPath( maze, x + dir[0], y + dir[1], path ) ;
//todo handle result
}
//介绍一组方向
私有列表方向=Arrays.asList(新int[][{{1,0},{-1,0},{0,1},{0,-1}})//右,左,下,上
//使用它
收藏。洗牌(方向);
for(int[]目录:方向){
布尔结果=搜索路径(迷宫,x+dir[0],y+dir[1],路径);
//todo处理结果
}
将if语句放入switch生成一个随机数并使用switch
决定是否执行
将if语句放在不同的方法中,根据数据按顺序调用它们。但是你为什么要这么做呢?如果你的搜索是彻底的,那么,为什么采取的步骤顺序会影响最终结果?@Stultuske,正如你所见,这是一种递归方法,因此我认为这不起作用。提示:要对某人发表评论,请添加他的用户名,如下所示:@Stultuske@TimBiegeleisen因为有时候幽灵会被卡住,我会出现“路径未找到”错误,即使有路径,我认为这是因为搜索算法陷入循环,因为if语句的顺序。
//Need something that executes these if statements at random
int random=ThreadLocalRandom.current().nextInt(1,5);
switch(random)
{
case 1:
//Searching one block down
if ( searchPath( maze, x - 1, y, path ) )
{
path.add( new Point( x, y ) );
return true;
}
break;
case 2:
//Searching one block up
if ( searchPath( maze, x + 1, y, path ) )
{
path.add( new Point( x, y ) );
return true;
}
break;
case 3:
//Searching one block left
if ( searchPath( maze, x, y - 1, path ) )
{
path.add( new Point( x, y ) );
return true;
}
break;
case 4:
//Searching one block right
if ( searchPath( maze, x, y + 1, path ) )
{
path.add( new Point( x, y ) );
return true;
}
break;
}