多二维数组宽度优先搜索Java
我试图在Java类中为一个名为Quoridor的游戏创建一个方法,在这个游戏中,棋子必须到达棋盘的另一边。Pawn类(一个坐标)遍历9x9二维阵列,而Wall类(2个坐标)放置在10x10二维阵列上。这些墙基本上被放置在当兵广场之间。典当不能跨越墙壁或其他典当,我不知道如何实现两个二维阵列的BFS。我是编程新手,想知道是否有人能告诉我如何创建这样一个方法。当前有一个Pawn and Wall类,该类具有必要的get和set方法。多二维数组宽度优先搜索Java,java,dictionary,multidimensional-array,coordinates,breadth-first-search,Java,Dictionary,Multidimensional Array,Coordinates,Breadth First Search,我试图在Java类中为一个名为Quoridor的游戏创建一个方法,在这个游戏中,棋子必须到达棋盘的另一边。Pawn类(一个坐标)遍历9x9二维阵列,而Wall类(2个坐标)放置在10x10二维阵列上。这些墙基本上被放置在当兵广场之间。典当不能跨越墙壁或其他典当,我不知道如何实现两个二维阵列的BFS。我是编程新手,想知道是否有人能告诉我如何创建这样一个方法。当前有一个Pawn and Wall类,该类具有必要的get和set方法。在此处输入code package Players.HaydenLi
在此处输入code
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;
}
}
package Players.HaydenLindquist;
导入java.util.*;
进口发动机.记录器;
导入接口.坐标;
导入Interface.PlayerModule;
导入Interface.PlayerMove;
公共类HaydenLindquist实现PlayerModule{
协调新的合作关系;
墙对墙;
私人记录器注销;
棋手;
当兵对手;
名单;
上市典当行;
公共int getID()
{
返回player.getId();
}
公共集GetNeights(坐标c){
//创建用于存储相邻平铺的哈希集
Set neighbor=new HashSet();
int x=c.getRow();
int y=c.getCol();
//4个相邻空间的坐标
坐标顶部=新坐标(x,y-1);
底部坐标=新坐标(x,y+1);
左坐标=新坐标(x-1,y);
右坐标=新坐标(x+1,y);
如果(x==0){
如果(y==0){
如果(!wallCheck(右))
添加(右);
如果(!墙检查(底部))
添加(底部);
}
如果(y==8),则为else{
如果(!墙检查(顶部))
添加(顶部);
如果(!wallCheck(右))
添加(右);
}
否则{
如果(!墙检查(顶部))
添加(顶部);
如果(!wallCheck(右))
添加(右);
如果(!墙检查(底部))
添加(底部);
}
}
else如果(x==8){
如果(y==0){
如果(!wallCheck(左))
neighbor.add(左);
如果(!墙检查(底部))
添加(底部);
}
如果(y==8),则为else{
如果(!墙检查(顶部))
添加(顶部);
如果(!wallCheck(左))
neighbor.add(左);
}
否则{
如果(!墙检查(顶部))
添加(顶部);
如果(!wallCheck(左))
neighbor.add(左);
如果(!墙检查(底部))
添加(底部);
}
}
如果(y==0),则为else{
如果(!wallCheck(右))
添加(右);
如果(!wallCheck(左))
neighbor.add(左);
如果(!墙检查(底部))
添加(底部);
}
如果(y==8),则为else{
如果(!wallCheck(右))
添加(右);
如果(!wallCheck(左))
neighbor.add(左);
如果(!墙检查(顶部))
添加(顶部);
}
否则{
如果(!wallCheck(右))
添加(右);
如果(!wallCheck(左))
neighbor.add(左);
如果(!墙检查(顶部))
添加(顶部);
如果(!墙检查(底部))
添加(底部);
}
返回邻居;
}
/**
*
*/
公共坐标getPlayerLocation(int playerID)
{
如果(playerID==player.getId())
{
返回(player.getLocation());
}
else返回(对手.getLocation());
}
/**
*
*/
公共地图getPlayerLocations(){
//创建整数、坐标类型的哈希映射
HashMap位置=新的HashMap();
//将两名玩家的ID和位置添加到HashMap
locations.put(player.getId(),player.getLocation());
positions.put(对手.getId(),对手.getLocation());
返回地点;
}
/**
*
*/
公共列表getShortestPath(坐标起点、坐标终点)
{
列表路径=新的ArrayList();
返回null;
}
/**
*
*/
公共int getWallsRemaining(int playerID)
{
如果(playerID==player.getId())
{
return(player.getWalls());
}
else返回(对手.getWalls());
}
/**
*
*/
public void init(记录器记录器、int playerID、int numWalls、Map playerHomes)
{
注销=记录器;
//创建用于存储墙对象的ArrayList
wallList=新的ArrayList();
//创建我们的两个播放器,并使用来自引擎的数据初始化它们
对于(整数i:(Set)playerHomes.keySet())
{
如果(i==playerID)
玩家=新棋子(playerID、numWalls、playerHomes.get(i));
其他的
{
对手=新的棋子(2个,numWalls,playerHomes.get(i));
}
}
}
公共空间最后移动(PlayerMove m)
{
//检查m是否是玩家移动或墙壁放置
if(m.isMove())
{
//切换以区分播放器1和播放器2。
//然后更新相应的玩家位置
开关(m.getPlayerId())
{
案例1:
player.setLocation(m.getEnd());
打破
案例2:
设定位置(