2016-03-06 57 views
2

您好我一直在尝试使用广度优先搜索编程算法,找到蓝点退出游戏的最短路径。我是java新手,无法运行/理解该类的算法。我有一个名为gameModel的类,它存储每个点的状态。该计划旨在测试蓝点可以在没有通过橙色点(SELECTED)的情况下离开板的最快方式,并且如果没有出路胜过玩家获胜。我一直在运行程序,并得到编译错误,我不知道如何解决。我将控制器类包含在短点运行的地方。广度优先搜索移动网点游戏

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); 
      } 
     } 
    } 
+0

什么是编译错误?请将它们添加到帖子中。 – hotzst

+0

Sthepanie,这与问题无关,但你必须学会​​接受一些答案*如果*他们解决了你的问题(或只是留下评论,表明答案不起作用),因为这个网站的工作原理你不会甚至不必付钱给他们接受他们的答案(但你的问题主要是“调试”,他们很难,不容易解决),这样做会引发人们在未来再次帮助你。 –

+2

所有的渥太华学生都会让其他人为他们做功课吗? – Oliver

回答

3

您的方法public Breadth-First-Start(start, targets, blocked)被声明为错误。你不能在方法名中有-,你也需要指定返回类型(只有构造函数没有要定义的返回类型)。你也需要指定参数类型。从我理解的目标开始,看起来像String类型,并阻止看起来像一个列表,请尝试替换方法头由以下public void breadthFirstSearch(String start, String targets, List blocked)不知道你想要什么样的返回类型,因为你没有任何方法返回。但在你的情况下,你可能需要这样的路径,所以可能是List类型,或布尔值来知道是否有路径。

+0

谢谢修复问题 – Stephanie

+0

请接受答案,谢谢。 –

1

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

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

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

希望这会有帮助

+0

谢谢生病看看我可能更好地理解它的其他算法 – Stephanie