jeudi 31 octobre 2019

Is there a function for selecting a specific part of a 2d array in Java?

So, I have a school project that has 2 players and uses classes for weapons, traps and food and randomly places them on a specific part of the array. I have constructed a 2d array with the corresponding class type for each one of them, each one respectively called WeaponAreaLimits, FoodAreaLimits and 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.
....

Catch is, weapons can be inside the box made from those points, but, food must be on the points surrounding weapons, not inside the box made from -3,3 3,3 -3,-3 and 3,-3. I constructed a method called createRandomFood() that randomly gives properties to Food objects. The ones I'm stuck on are the x and y ones, and how to only use those surrounding points and don't put them in a place they shouldn't be. Take a look on what I've wrote:

....
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
    }
}

My question is, is there a method or way to eliminate those points?




Aucun commentaire:

Enregistrer un commentaire