-5

Ich versuche eine Methode in einer Klasse für Java für ein Spiel namens Quoridor zu erstellen, in dem ein Pfand die andere Seite des Boards erreichen muss. Die Pawn-Klasse (eine Koordinate) durchläuft ein 9x9-2D-Array, während die Wall-Klassen (2 Koordinaten) auf einem 10x10-2D-Array platziert werden. Die Wände sind im Grunde zwischen den Bauernquadraten platziert. Bauern können keine Wände oder andere Bauern kreuzen, ich bin mir nicht sicher, wie ich das BFS mit zwei 2D-Arrays umsetzen soll. Ich bin neu im Programmieren und habe mich gefragt, ob mir jemand Schritt für Schritt erklären könnte, wie man eine solche Methode erstellt. Derzeit haben eine Pawn und Wall-Klasse mit notwendigen Methoden zum Abrufen und Festlegen. enter code hereMulti-2D Array Breite Erste Suche 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

Haben Sie überhaupt einen Code? – Joris

+0

Ich habe gerade unsere Hauptklasse hinzugefügt, in der wir unsere Methode shortestPath() haben, an der wir gerade arbeiten. Nicht sicher, wie man es anpackt –

Antwort

0

Da Sie mit der Idee einer BFS fangen, und Sie sich entschieden haben, Ihr Board mit mehrdimensionalen Arrays darzustellen, warum Sie darüber nachdenken, wie BFS-Plänen auf die Darstellung nicht starten von deinem Brett?

Zum Beispiel können Sie den Code schreiben, um alle angrenzenden Zellen einer bestimmten Zelle aufzulisten? Wenn Sie das tun können, sollte es einfacher sein zu sehen, wie Sie den Rest von BFS implementieren.

+0

Ich aktualisierte meinen Beitrag und fügte unsere "Hauptklasse" hinzu, jede Hilfe wäre erstaunlich –

Verwandte Themen