GameLogic,x连续游戏

我正在制作一个游戏,我需要制作一个方法来检查指定的单元是否是包含相同字符的水平连续单元序列的一部分。 细胞序列需要长度为l。 如果细胞是长度至少为1的水平序列的一部分,则为真,否则为假。

到目前为止,我已经知道它检测到在指定字符的行中是否至少有5个连续的单元格具有相同的字符。 有人可以帮忙吗?

您可以使用两个循环(每侧一个)简单地搜索两侧,并检查连续单元格的总和是否确实为l 。 有点像:

 public static boolean checkPositionRow(char[][] a, int row, int col, int l) { int counter = 1; //starting from 1, because for a[row][col] itself char charAtPosition = a[row][col]; //expand to the right as much as possible for (int i = col+1; i < a[row].length && a[row][i] == charAtPosition; i++) counter++; //expand to the left as much as possible for (int i = col-1; i >= 0 && a[row][i] == charAtPosition; i--) counter++; return counter >= l; } 

假设您要包含的列是 – 5 。 序列长度为3

现在,以下是该行中序列出现的可能性: –

  • Col = (5 - 2) to 5
  • Col = 5 to (5 + 2)
  • 或者,范围[ (5 - 2), (5 + 2) ]之间的任何地方

所以,你感兴趣的范围是: – [col - length + 1, col + length - 1] 。 在此范围内,每个长度为3序列将包含第5列。

因此,只需在这些范围之间运行循环。 现在,问题是当这些范围超出范围时。 所以,您之前可以进行检查。

更好的方法是使用Math.max(0, col-length+1)Math.min(col + length-1, arraylength)

所以,你可以使用这个for-loop : –

 public static boolean checkPositionRow(char[][] a, int row, int col, int l) { int counter = 0; int left = col - l + 1; int right = col + l - 1; char charAtPosition = a[row][col]; for (int i = Math.max(0, left); i < Math.min(right, a[row].length); i++) { if (a[row][i] == charAtPosition) { counter++; if (counter >= l) return true; } else { counter = 0; } } }