-5

我想创建一个名为Quoridor的游戏,其中一个Pawn必须到达另一侧的董事会的Java类的方法。 Pawn类(一个坐标)穿过9x9二维阵列,而Wall类(2个坐标)放置在10x10二维阵列上。墙壁基本上放置在典当广场之间。典当不能穿过墙壁或其他典当,我不知道如何用两个二维阵列实现BFS。我是编程新手,想知道是否有人可以一步一步地告诉我如何创建这样的方法。目前有一个Pawn和Wall类有必要的get和set方法。 enter code here多维二维数组广度优先搜索Java

package Players.HaydenLindquist; 

import java.util.*; 

import Engine.Logger; 
import Interface.Coordinate; 
import Interface.PlayerModule; 
import Interface.PlayerMove; 

public class HaydenLindquist implements PlayerModule { 

    Coordinate newCoords; 
    Wall theWall; 
    private Logger logOut; 
    Pawn player; 
    Pawn opponent; 
    List<Wall> wallList; 
    List<Pawn> pawnList; 

    public int getID() 
    { 
     return player.getId(); 
    } 

    public Set<Coordinate> getNeighbors(Coordinate c) { 

     // Creates HashSet we will use to store neighbor tiles 
     Set<Coordinate> neighbor = new HashSet<Coordinate>(); 

     int x = c.getRow(); 
     int y = c.getCol(); 

     // Coordinates for the 4 adjacent spaces 
     Coordinate top = new Coordinate(x,y-1); 
     Coordinate bottom = new Coordinate(x,y+1); 
     Coordinate left = new Coordinate(x-1,y); 
     Coordinate right = new Coordinate(x+1,y); 

     if(x == 0) { 
      if(y == 0) { 
       if(! wallCheck(right)) 
        neighbor.add(right); 
       if(! wallCheck(bottom)) 
        neighbor.add(bottom); 
      } 
      else if(y == 8) { 
       if(! wallCheck(top)) 
        neighbor.add(top); 
       if(! wallCheck(right)) 
        neighbor.add(right); 
      } 
      else { 
       if(! wallCheck(top)) 
        neighbor.add(top); 
       if(! wallCheck(right)) 
        neighbor.add(right); 
       if(! wallCheck(bottom)) 
        neighbor.add(bottom); 
      } 
     } 

     else if(x == 8) { 
      if(y == 0) { 
       if(! wallCheck(left)) 
        neighbor.add(left); 
       if(! wallCheck(bottom)) 
        neighbor.add(bottom); 
      } 
      else if(y == 8) { 
       if(! wallCheck(top)) 
        neighbor.add(top); 
       if(! wallCheck(left)) 
        neighbor.add(left); 
      } 
      else { 
       if(! wallCheck(top)) 
        neighbor.add(top); 
       if(! wallCheck(left)) 
        neighbor.add(left); 
       if(! wallCheck(bottom)) 
        neighbor.add(bottom); 
      } 
     } 

     else if(y == 0) { 
      if(! wallCheck(right)) 
       neighbor.add(right); 
      if(! wallCheck(left)) 
       neighbor.add(left); 
      if(! wallCheck(bottom)) 
       neighbor.add(bottom); 
     } 

     else if(y == 8) { 
      if(! wallCheck(right)) 
       neighbor.add(right); 
      if(! wallCheck(left)) 
       neighbor.add(left); 
      if(! wallCheck(top)) 
       neighbor.add(top); 
     } 

     else { 
      if(! wallCheck(right)) 
       neighbor.add(right); 
      if(! wallCheck(left)) 
       neighbor.add(left); 
      if(! wallCheck(top)) 
       neighbor.add(top); 
      if(! wallCheck(bottom)) 
       neighbor.add(bottom); 
     }  
     return neighbor;   
    } 

    /** 
    * 
    */ 
    public Coordinate getPlayerLocation(int playerID) 
    {   
     if(playerID == player.getId()) 
     { 
      return(player.getLocation()); 
     } 
     else return(opponent.getLocation());   
    } 

    /** 
    * 
    */ 
    public Map<Integer, Coordinate> getPlayerLocations() { 

     // Creates HashMap of Integer, Coordinate type 
     HashMap<Integer, Coordinate> locations = new HashMap<Integer, Coordinate>(); 

     // Adds the ID and locations of the 2 players to the HashMap 
     locations.put(player.getId(), player.getLocation()); 
     locations.put(opponent.getId(), opponent.getLocation());  

     return locations; 
    } 


    /** 
    * 
    */ 
    public List<Coordinate> getShortestPath(Coordinate start, Coordinate end) 
    { 
     List<Coordinate> path = new ArrayList<Coordinate>(); 


     return null; 
    } 


    /** 
    * 
    */ 
    public int getWallsRemaining(int playerID) 
    {   
     if(playerID == player.getId()) 
     { 
      return(player.getWalls()); 
     } 
     else return(opponent.getWalls()); 
    } 


    /** 
    * 
    */ 
    public void init(Logger logger, int playerID, int numWalls, Map<Integer, Coordinate> playerHomes) 
    {   
     logOut = logger;   
     // Creates ArrayList used to store wall objects 
     wallList = new ArrayList<Wall>();   
     // Creates our two players and initializes them with data from engine 
     for (Integer i : (Set<Integer>) playerHomes.keySet()) 
     { 
      if (i == playerID) 
       player = new Pawn(playerID,numWalls,playerHomes.get(i)); 
      else 
      { 
       opponent = new Pawn(2,numWalls,playerHomes.get(i)); 
      } 
     } 
    } 

    public void lastMove(PlayerMove m) 
    {   
     // Check if m is a player move or wall placement 
     if(m.isMove()) 
     {    
      // Switch to differentiate between player 1 and 2. 
      // then updates the appropriate players location 
      switch(m.getPlayerId()) 
      {    
      case 1: 
       player.setLocation(m.getEnd()); 
       break; 

      case 2: 
       opponent.setLocation(m.getEnd()); 
       break; 
      } 
     }   
     else 
     {    
      switch(m.getPlayerId()) 
      {    
      case 1: 
       addWall(m.getStart(), m.getEnd()); 
       player.setWalls(player.getWalls() - 1); 
       break; 

      case 2: 
       addWall(m.getStart(), m.getEnd()); 
       opponent.setWalls(player.getWalls() - 1); 
       break; 
      }  
     } 
    } 


    /** 
    * 
    */ 
    public Set<PlayerMove> allPossibleMoves() 
    { 
     return null; 
    } 


    /** 
    * 
    */ 
    public PlayerMove move() 
    { 
     return null; 
    } 

    /** 
    * 
    * @param player 
    * @return 
    */ 


    /** 
    * 
    * 
    */ 
    public void playerInvalidated(int playerID) 
    { 

    } 

    /** 
    * Method that creates a new wall object and adds it to the wallList ArrayList 
    * 
    * @param start 
    * @param end 
    */ 
    public void addWall(Coordinate start, Coordinate end) 
    { 
     Wall w = new Wall(start,end); 
     wallList.add(w);     
    } 

    /** 
    * A check method to see if entered coordinate contains a section of a wall 
    * 
    * @param c 
    * @return 
    */ 
    public boolean wallCheck(Coordinate c) 
    { 
     // Iterates through wall objects in wallList 
     for(int i = 0; i < wallList.size(); i++) 
     {    
      // Check if any adjacent squares contain a section of a wall 
      if(wallList.get(i).isWall(c)) 
      { 
       return true; 
      } 
     }   
     return false;   
    } 
} 
+1

到目前为止,您是否有任何代码? – Joris

+0

我刚刚添加了我们的主类,在那里我们有我们正在使用的shortestPath()方法。不知道如何解决它 –

回答

0

既然你已经从一个BFS,和的想法您决定表示与多维数组,你的董事会为什么不通过思考BFS如何映射到表示启动你的董事会?

例如,你能写出代码来列出给定单元格的所有相邻单元吗?如果你能做到这一点,应该更容易看到如何实现BFS的其余部分。

+0

我更新了我的帖子,并添加了我们的“主类”任何帮助将是惊人的 –