Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 移动点游戏的广度优先搜索_Java_Breadth First Search - Fatal编程技术网

Java 移动点游戏的广度优先搜索

Java 移动点游戏的广度优先搜索,java,breadth-first-search,Java,Breadth First Search,嗨,我一直在尝试编程一个算法,使用广度优先搜索,找到蓝点在游戏中退出的最短路径。我是java新手,在运行/理解该类的算法时遇到困难。我有一个名为gameModel的类,它存储每个点的状态。该算法旨在测试蓝点不通过橙色点(选中)退出棋盘的最快方式,如果没有出路,则玩家获胜。我一直在运行程序,并得到编译错误,我不知道如何修复。我包括运行短点的控制器类 import java.util.Random; import java.awt.event.ActionEvent; import java.aw

嗨,我一直在尝试编程一个算法,使用广度优先搜索,找到蓝点在游戏中退出的最短路径。我是java新手,在运行/理解该类的算法时遇到困难。我有一个名为gameModel的类,它存储每个点的状态。该算法旨在测试蓝点不通过橙色点(选中)退出棋盘的最快方式,如果没有出路,则玩家获胜。我一直在运行程序,并得到编译错误,我不知道如何修复。我包括运行短点的控制器类

import java.util.Random;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.*;

/**
 * The class <b>GameController</b> is the controller of the game. It implements 
 * the interface ActionListener to be called back when the player makes a move. It computes
 * the next step of the game, and then updates model and view.

*/


public class GameController implements ActionListener {

private int size;
private GameModel gameModel;
private GameView gameView;
private boolean click;

 /**
 * Constructor used for initializing the controller. It creates the game's view 
 * and the game's model instances
 * 
 * @param size
 *            the size of the board on which the game will be played
 */
public GameController(int size) {
    this.size = size;
    this.gameModel = new GameModel(size);
    this.gameView = new GameView (gameModel, this);
    click =  false;
}


/**
 * Starts the game
 */
public void start(){
    if (click){
        List start = new List {gameModel.getCurrentDot().getX(), gameModel.getCurrentDot().getY()};
        List<int> targets = new ArrayList<>();
        List<int> blocked = nwq ArrayList<>();
        for (int i = 0; i < size; i++){
            targets.add(i, 0);
            targets.add(i, size);
            targets.add(1, size);
            targets.add(1, 0);
        }
        for (int i = 0; i < size; i++){
            for (int j = 0; j < size; j++)
                if(gameModel.getstatus(i, j) == SELECTED){
                blocked.add(i, j);
            }
        String path = Breadth-First-Start(start, targets, blocked);
        gameView = new GameView(gameModel, this);
        gameView.getBoardView().update();
    }
}

public Breadth-First-Start(start, targets, blocked){ // Need help with
    Queue queue = new LinkedList();
    queue.add(start + "");

    while(!queue.isEmpty()){
        String p = queue.remove();
        if (p != blocked){       //If p is not in blocked paths
            if (p == targets){   //If p is in targets
                return "q + {p}";
            } else {
                queue.add("q + {p}");
                blocked.add(p);
            }
        }
    }
import java.util.Random;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.*;
/**
*GameController类是游戏的控制器。它实现
*玩家移动时要回调的接口ActionListener。它计算
*游戏的下一步,然后更新模型和视图。
*/
公共类GameController实现ActionListener{
私有整数大小;
私人博弈模型;
私人游戏视图游戏视图;
私有布尔点击;
/**
*用于初始化控制器的构造函数。它创建游戏的视图
*以及游戏的模型实例
* 
*@param大小
*游戏将在其上进行的棋盘的大小
*/
公共游戏控制器(整数大小){
这个。大小=大小;
this.gameModel=新游戏模型(大小);
this.gameView=新的gameView(gameModel,this);
单击=false;
}
/**
*开始比赛
*/
公开作废开始(){
如果(单击){
列表开始=新列表{gameModel.getCurrentDot().getX(),gameModel.getCurrentDot().getY()};
列表目标=新的ArrayList();
List blocked=nwq ArrayList();
对于(int i=0;i
您的方法
公共宽度优先开始(开始、目标、阻止)
声明错误。方法名称中不能有
-
,还需要指定返回类型(只有构造函数没有要定义的返回类型)。您还需要指定参数类型。据我所知,字符串类型的目标和开始看起来像一个列表,而阻止类型看起来像一个列表,请尝试用以下
public void breadthFirstSearch(字符串开始,字符串目标,列表阻止)替换方法头
由于方法中没有任何返回,因此不确定您想要什么返回类型。但在您的情况下,您可能需要路径,因此可能是类型列表,或者使用布尔值来知道是否有路径。

您想要做的事与图论有关。如果两个节点连接,则会在它们之间创建一条边。在这种情况下,橙色点不会连接到任何东西,因为路径不可能通过它们存在。Dijkstra的算法对于做你想做的事情非常有用,尽管它是广度优先而不是深度优先。我建议从这里开始,我相信有一些用java实现算法的例子

图的边具有权重,这些权重将进行比较,以便找到两个节点之间的最短路径

我看到你的阻止列表声明中有nwq而不是新的。这可能是你的问题


希望这有帮助

编译错误是什么?请将它们添加到帖子中。窗格,这与问题无关,但如果它们解决了你的问题,你必须学会接受一些答案(或者只是留下评论,表明答案不起作用),因为这就是网站的运作方式,你甚至不必付钱给他们,只要接受他们的答案就行了(然而你的问题大多是调试,很难解决),通过这样做,它将触发人们在将来再次帮助你。所有uOttawa学生都让其他人为他们做作业吗?谢谢你解决了一个问题谢谢我来看看其他算法我可能会更好地理解它