我正在尝试在一个名为Quoridor的游戏中为Java创建一个方法,其中Pawn必须到达棋盘的另一侧。 Pawn类(一个坐标)遍历9x9 2D阵列,而Wall类(2个坐标)放置在10x10 2D阵列上。墙壁基本上位于Pawn广场之间。 Pawns不能穿过Walls或其他Pawns,我不知道如何用两个2D阵列实现BFS。我是编程的新手,想知道是否有人可以一步一步地给我如何创建这样的方法。目前有一个带有必要的get和set方法的Pawn和Wall类。enter code here
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;
}
}
答案 0 :(得分:0)
既然您已经开始考虑使用BFS,和,那么您决定用多维数组代表您的主板,为什么不首先考虑一下BFS如何映射到您的电路板的表示?
例如,您是否可以编写代码来列出给定单元格的所有相邻单元格?如果你能做到这一点,那么应该更容易看到如何实现BFS的其余部分。