Java 围绕单个位置搜索二维阵列
所以对于match 3类型的游戏,比如candy crush,我需要搜索2D数组,如果重复相同的数字,则创建一个匹配 例如,如果我的2d数组类似于Java 围绕单个位置搜索二维阵列,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,所以对于match 3类型的游戏,比如candy crush,我需要搜索2D数组,如果重复相同的数字,则创建一个匹配 例如,如果我的2d数组类似于 212031201 012102312 101223200 012131013 010321022 201210101 102023202 <-- 012102312 <-- 012321022 <-- 212031201 012102312 101223200 012131013 010321022 201210101 1
212031201
012102312
101223200
012131013
010321022
201210101
102023202 <--
012102312 <--
012321022 <--
212031201
012102312
101223200
012131013
010321022
201210101
102023202我认为最好保留一个由Tile
对象组成的2D数组。平铺类可以如下所示:
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;
}
}
然后,您可以询问每个磁贴的邻居位置,然后检查它们的类型。对边缘平铺进行一些检查是必要的,但我认为这应该可以很好地工作。优雅的方法是设计一个迭代器,围绕提供的点交付每个元素
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 + ",");
}
}
公共静态类实现了Iterable{
//在x中行走一个点。
私有静态final int[]xInc={-1,1,1,0,0,-1,-1,0};
//y也是如此。
私有静态final int[]yInc={-1,0,0,1,1,0,0,0,-1};
//实际数组。
最终T[]a;
//中心。
最终int cx;
最终积分;
公共区域(T[][]a,内部cx,内部cy){
//抓取我的参数-数组。
这个a=a;
//我们必须绕着中心走。
这个.cx=cx;
this.cy=cy;
}
@凌驾
公共迭代器迭代器(){
//迭代器。
返回新的迭代器(){
//从cx开始,cy
int x=cx;
int y=cy;
//沿着inc阵列的步骤。
int i=0;
@凌驾
公共布尔hasNext(){
//当我们到达序列的末尾时停止。
返回i=0&&y=0&&x
也许可以尝试查找表,以获取您正在测试的磁贴的邻居的索引。
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 + ",");
}
}