Java中是否有用于选择2d数组特定部分的函数?

Java中是否有用于选择2d数组特定部分的函数?,java,arrays,random,2d,Java,Arrays,Random,2d,所以,我有一个学校项目,有2名玩家,使用武器、陷阱和食物课程,并随机将它们放置在阵列的特定部分。我构建了一个2d数组,每个数组都有相应的类类型,每个数组分别称为WeaponAreaLimits,FoodAreaLimits和TrapAreaLimits public class Board{ int[][] weaponAreaLimits = { {-2,2}, {2, 2}, {-2, -2}, {2, -2} }; int[][] foodAreaLimits = { {-3, 3},

所以,我有一个学校项目,有2名玩家,使用武器、陷阱和食物课程,并随机将它们放置在阵列的特定部分。我构建了一个2d数组,每个数组都有相应的类类型,每个数组分别称为
WeaponAreaLimits
FoodAreaLimits
TrapAreaLimits

public class Board{

int[][] weaponAreaLimits = { {-2,2}, {2, 2}, {-2, -2}, {2, -2} };
int[][] foodAreaLimits = { {-3, 3}, {3, 3}, {-3, -3}, {3, -3} };
int[][] trapAreaLimits = { {-4, 4}, {4, 4}, {-4, -4}, {4, -4} }; //These are the specific coordinates that limit the placement of said weapons etc.
....
问题是,武器可以放在由这些点制成的盒子里,但食物必须放在武器周围的点上,而不是放在由-
3,3,3-3,-3和
3,-3
制成的盒子里。 我构造了一个名为
createRandomFood()
的方法,该方法随机为
Food
对象提供属性。我一直关注的是x和y点,以及如何只使用周围的点,而不将它们放在不该放的地方。看看我写的:

....
public void createRandomFood(){

    for(int i = 1; i < food.length; i++){
        food[i].setId(i + 1);
        food[i].setX; 
        food[i].setY; 
        food[i].setPoints(rand.nextInt(10) + 1); //Ignore the other setters
    }
}
。。。。
公共食品{
对于(int i=1;i

我的问题是,有没有消除这些点的方法或途径?

在您的示例中,您必须在外部区域(
foodAreaLimits
)生成一个点,该点不属于内部区域(
武器区域限制
)。正确的方法可能取决于区域形状,例如,如果内部区域是圆形,而外部区域是三角形,该怎么办

您可以使用此算法解决此问题:

  • 在其内部生成一个随机点
  • 如果它在
    武器区域限制之外
    就足够了
  • 否则随机选择方向。您处理
    int
    只是为了简单:西、东、北、南
  • 更改新点的方向坐标,直到移动到
    武器区域限制之外。你处理的是
    int
    ,例如west意味着移动{-1,0},所以执行
    食物[i].x--

    您可能需要考虑区域有多大以及项目分布是什么。如果内部和外部区域的大小相近,这可能会很棘手。

    好吧,假设我现在明白了,首先

    您的限制数据可以更简单,只要它们表示矩形限制。你只需要minX,maxX,minY,maxY。我将创建一个对象来存储以下内容:

    public class Limits {
        int minX;
        int minY;
        int maxX;
        int maxY;
        public Limits(int x1, y1, x2, y2) {
           minX = x1;
           minY = y1;
           maxX = x2;
           maxY = y2;
        }
    }
    
    然后你的对象就变成了

    Limits weaponAreaLimits = new Limits(-2, -2, 2, 2);
    Limits foodAreaLimits = new Limits(-3, -3, 3, 3);
    Limits trapAreaLimits = new Limits(-4, -4, 4, 4);
    
    这是因为矩形可以由两个对角定义。在本例中,我们使用左下角和右上角。你不需要每个顶点

    现在,您需要这样一种方法:

    public boolean setPoints(Item item, Limits includeRange, Limits excludeRange) {
        // These two variables hold the width & height of the box where things can be
        int width = includeRange.maxX - includeRange.minX + 1;
        int height = includeRange.maxY - includeRange.minY + 1;
        int x;
        int y;
    
        do {
            x = includeRange.minX + (rand() % width);
            y = includeRange.minY + (rand() % height);
            // At this point, x and y are inside the includeRange.
            // We need to make sure they aren't in the disallowed area
        } while ( (excludeRange == null) ||
          (x >= excludeRange.minX && x <= excludeRange.maxX &&
           y >= excludeRange.minY && y <= excludeRange.maxY) );
    
        // When the above loop exits, you have an x and y that works.
        item.x = x;
        item.y = y;
    
        return true;
    }
    
    public boolean设定点(项目、限制包括范围、限制不包括范围){
    //这两个变量控制着盒子的宽度和高度
    int width=includeRange.maxX-includeRange.minX+1;
    int height=includeRange.maxY-includeRange.minY+1;
    int x;
    int-y;
    做{
    x=includeRange.minX+(rand()%宽度);
    y=includeRange.minY+(rand()%高度);
    //此时,x和y位于includeRange内。
    //我们需要确保他们不在不允许的区域
    }while((excludeRange==null)||
    
    (x>=excludeRange.minX&&x=excludeRange.minY&&y我忘了提到,food是一个1d数组,类型为food(另一个类)你的问题还不太清楚。你有一个代表你地图的数组?我不明白你的极限数组是什么。这是否意味着武器必须在任何边缘至少有2个插槽,食物至少有3个,陷阱至少有4个,还有一个额外的要求,食物必须在武器的一个插槽内?说明书要求我使用手推车ESIA系统用于我的坐标,但我必须将坐标转换为数组块(例如,[0][0]是我的左上边框,但{0,0}是表的中心,如果我有一个4x4板,左上角是(-2,2)).武器必须在棋盘的内部,食物在周围的棋盘上,陷阱在食物周围的棋盘上。因此,你需要的是一种方法,可以让你根据上述约束随机设置对象的x,y位置,而不是在已经有东西的单元格上。你只处理
    int
    坐标还是ar您是否允许将点设置为
    ,例如
    {-3.2,2.7}