gpt4 book ai didi

java - 围绕单个位置搜索二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:13 26 4
gpt4 key购买 nike

因此,对于三消类型的游戏,例如《糖果粉碎传奇》,我需要搜索 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
}
}
}
}

}

最佳答案

我认为保留 Tile 对象的二维数组可能会更好。 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;
}
}

然后你可以询问每个图 block 其邻居的位置,然后检查它们的类型。对边缘瓷砖进行一些检查是必要的,但我认为这应该工作得相当好。

关于java - 围绕单个位置搜索二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27386421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com