繁体   English   中英

广度优先搜索移动点游戏

[英]Breadth-First-Search for moving dot game

嗨,我一直在尝试使用广度优先搜索来编程算法,该算法可以找到蓝点退出游戏的最短路径。 我是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);
            }
        }
    }

您的方法public Breadth-First-Start(start, targets, blocked)被声明为错误。 您不能在方法名称中有-还需要指定返回类型(仅构造函数没有要定义的返回类型)。 另外,您需要指定参数类型。 从我了解的目标和开始看起来像字符串类型和阻塞看起来像列表的角度来看,请尝试用以下public void breadthFirstSearch(String start, String targets, List blocked)的方法来替换方法头: public void breadthFirstSearch(String start, String targets, List blocked)不确定要返回哪种类型该方法没有任何回报。 但是在您的情况下,您可能想要的路径可能是List类型,或者是一个布尔型,以知道是否存在路径。

您想要做的事与图论有关。 如果连接了两个节点,则会在它们之间创建一条边。 在这种情况下,橙色点将不会连接到任何东西,因为路径无法通过它们。 Dijkstra的算法对于做您想做的事情非常有用,尽管它是广度优先而不是深度优先。 我建议从此处开始,我确定有在Java中实现该算法的示例。

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

我看到您的阻止列表声明中包含nwq而不是new。 那可能就是您的问题。

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM