因此,对于我正在做的这个 pset(对于 Candy Crush 克隆),我必须编写一个方法来检查所选点所在的棋盘行中是否有任何可能的匹配项。找到的方法将检查所选点的右侧,并返回所选点的匹配点数组。在给定的代码中,getRowBools
函数应该生成一个带有 true
或 false
的 boolean 数组,具体取决于点是否与所选点相同。然后,该方法将找到所选点之后 true
的长度,然后该长度应确定新点数组的长度。接下来,该方法应该将所有相关点放置到新数组中。但是,我不太明白如何以有效的方式修改点值。
public static Point[] buildPossibleMatchRow(Point p, Board b) {
int x = p.x;
int y = p.y;
boolean[] matches = b.getRowBools(new Point(x,y));
int amtcorrectx = 0;
for (int i=0; i < matches.length; i++)
{
if (matches[i]) {
amtcorrectx++;
}
}
Point[] returnVals = new Point[amtcorrectx];
returnVals[0] = p;
for (int i=0; i < returnVals.length; i++)
{
x++;
}
return returnVals;
}
这是我到目前为止的代码,但我确信最后的 for
循环有问题。我有什么遗漏的吗?
最佳答案
我不太明白你的问题,但是看看你的代码,也许你想完成这样的事情?
public static Point[] buildPossibleMatchRow(Point p, Board b) {
int x = p.x;
int y = p.y;
boolean[] matches = b.getRowBools(new Point(x,y));
int amountCorrect = 0;
for (int i=0; i < matches.length; i++) {
if (matches[i])
amountCorrect++;
}
Point[] result = new Point[amountCorrect];
for (int i = 0; i < result.length; i++)
result[i] = new Point(x + i, y);
return returnVals;
}
关于java - 如何修改数组中的后续点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307505/