您好,我一直在尝试使用广度优先搜索来编写一种算法,该算法可以找到蓝点在游戏中退出的最短路径。我是java新手,在运行/理解该类的算法时遇到困难。我有一个名为 gameModel 的类,它存储每个点的状态。该算法旨在测试蓝点在不穿过橙色点(选定)的情况下退出棋盘的最快方式,如果没有出路,则玩家获胜。我继续运行该程序并收到编译错误,我不知道如何修复。我包含了运行短点的 Controller 类。
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)
被宣告错误。你不能有-
在方法名称中,还需要指定返回类型(只有构造函数没有要定义的返回类型)。您还需要指定参数类型。据我了解,targets 和 start 看起来像 String 类型,blocked 看起来像 List,请尝试用以下 public void breadthFirstSearch(String start, String targets, List blocked)
替换方法头不确定您想要什么返回类型,因为该方法中没有任何返回值。但在您的情况下,您可能需要路径,因此可能是列表类型,或者是 boolean 值来知道是否存在路径。
关于java - 广度优先搜索移动点游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832524/