Click here to Skip to main content
15,888,337 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi there,

Say I have a 2D array, of which we are using as a gameboard for instance.

For example, a 5x5 2D array of the type char, and initialized with dots and a 'S'.

Java
char[][] array = new char[5][5]


.....
.....
..S..
.....
.....

How can I find the indexes of the 2D array that are closest to the letter S (aka wrap around it), in terms of proximity on the board, and then turn this into a new array?

So in this example, the 8 dots that surround S can now be initialized into a new array.


What I have tried:

I have tried finding the Euclidean distance between a given point and the array elements that surround it, and then attaching the objects of that array to a length by means of a hashmap. Note that the above question is more for simplicity sakes, and my actual project works with custom-type 2D arrays, evident in the code below. Nevertheless, the concept is still applicable.


Java
public static void getIndexes(Grid_23722002 grid, Piece_23722002 piece){

        int row = piece.getRow();
        int col = piece.getCol();

        Piece_23722002[][] nhood = new Piece_23722002[8][8];
        Map<Piece_23722002, Integer> unsortedLengthMap = new LinkedHashMap<Piece_23722002, Integer>();

        for (int i =0; i < grid.getArrayForm().length; i++){

            for (int j =0; j < grid.getArrayForm()[i].length; j++){

                unsortedLengthMap.put(piece, grid.getArrayForm()[i][j].getLengthToPoint(grid, i, j, piece));

            }

        }

        Map<Piece_23722002, Integer> sortedLengthMap = new LinkedHashMap<Piece_23722002, Integer>();
        unsortedLengthMap.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .forEachOrdered(x -> sortedLengthMap.put(x.getKey(), x.getValue()));


    }

    public int getLengthToPoint(Grid_23722002 grid, int i, int j, Piece_23722002 piece){

        int piecesRow = piece.getRow();
        int piecesCol = piece.getCol();

        int length = (int) Math.sqrt(Math.pow(grid.getArrayForm()[i][j].getRow() - piecesRow, 2) + Math.pow(grid.getArrayForm()[i][j].getCol() - piecesCol, 2));

        return length;

    }
Posted
Updated 14-Sep-20 3:53am

1 solution

If you have a "point" (x,y) in a 2D array, then there can be "up to" 8 points surrounding the first point (less if the first point is on an "edge" of the array).

You determine "neighbors" for x,y as follows:

n = x, y+1
s = x, y-1
e = x+1, y
w = x-1, y
ne = x+1, y+1
nw = x-1, y+1
etc.

If any calculated x or y is less than 0 or greater than an upper array bound, the calculated point is "off the board".

I use a "class" for the points in my game; and made the "neighbors" children of each point so I only do those particular calcs once.

(For "border points" I still do the calcs but use a "dummy" point to represent the point outside the array ... just makes things easier. So, if your requirement is 5x5, use 7x7 and reserve the outside points for "out of bounds")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900