在单个位置周围搜索2D阵列

时间:2014-12-09 18:42:04

标签: java arrays multidimensional-array

所以对于一场比赛3类型的游戏,就像糖果粉碎一样,我需要搜索一个2D数组,如果重复相同的数字则创建一个匹配。

例如,如果我的2d数组类似于

212031201
012102312
101223200
012131013
010321022
201210101
102023202  <--
012102312  <--
012321022  <--

您注意到右下方的一行中有三个二十(指向箭头)。如何搜索数组以返回值并创建匹配项。这就是我的想法,但我不确定我的逻辑是否正确:

public class matchAI
   {
     Game game = new Game();

      public void search() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (game.board[i][j] == game.Emerald) //game.Board is the Array which is 9x9 filled  with numbers 0,1,2. game.Emerald =0
                {
                    //Store value
                    //Check Around current position for similar numbers


                   //Add to Score, indicating match, and remove current matched numbers and shift the rest down, like gravity sucks others down.
                   //Fill top row which would be empty from others shifting
                }
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

我认为保留Tile个对象的2D数组可能会更好。 Tile类看起来像这样:

public class Tile {

private int x, y;
private int type;
private int[][] neighbors;

public Tile(int x, int y, int type){
    this.x = x;
    this.y = y;
    this.type = type;

    findNeighbors();
}

private void findNeighbors(){
    int[] top = new int[] {x, y+1};
    int[] right = new int[] {x+1, y};
    int[] bottom = new int[] {x, y-1};
    int[] left = new int[] {x-1, y};

    neighbors = new int[][] {top, right, bottom, left};
}

public int getType() {
    return type;
}

public int[] getNeigbor(int side) {
    if(side == 0) {
        return neighbors[0];
    }
    //etc
    return null;
}
}

然后你可以向每个瓷砖询问其邻居的位置,然后检查它们的类型。边缘瓷砖需要进行一些检查,但我认为这应该可以很好地工作。

答案 1 :(得分:0)

优雅的方法是设计一个迭代器,在提供的点周围传递每个元素。

public static class Around<T> implements Iterable<T> {

    // Steps in x to walk around a point.
    private static final int[] xInc = {-1, 1, 1, 0, 0, -1, -1, 0};
    // Ditto in y.
    private static final int[] yInc = {-1, 0, 0, 1, 1, 0, 0, -1};
    // The actual array.
    final T[][] a;
    // The center.
    final int cx;
    final int cy;

    public Around(T[][] a, int cx, int cy) {
        // Grab my parameters - the array.
        this.a = a;
        // And the center we must walk around.
        this.cx = cx;
        this.cy = cy;
    }

    @Override
    public Iterator<T> iterator() {
        // The Iterator.
        return new Iterator<T>() {
            // Starts at cx,cy
            int x = cx;
            int y = cy;
            // Steps along the inc arrays.
            int i = 0;

            @Override
            public boolean hasNext() {
                // Stop when we reach the end of the sequence.
                return i < xInc.length;
            }

            @Override
            public T next() {
                // Which is next.
                T it = null;
                do {
                    // Take the step.
                    x += xInc[i];
                    y += yInc[i];
                    i += 1;
                    // Is it a good spot? - Not too far down.
                    if (y >= 0 && y < a.length) {
                        // There is a row here
                        if (a[y] != null) {
                            // Not too far across.
                            if (x >= 0 && x < a[y].length) {
                                // Yes! Use that one.
                                it = a[y][x];
                            }
                        }
                    }
                    // Keep lookng 'till we find a good one.
                } while (hasNext() && it == null);
                return it;
            }

        };
    }

}

public void test() {
    Integer[][] a = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
    for (Integer i : new Around<>(a, 1, 1)) {
        System.out.print(i + ",");
    }
    System.out.println();
    for (Integer i : new Around<>(a, 2, 2)) {
        System.out.print(i + ",");
    }
}