无法更改二维数组中某一点周围元素的值

huangapple go评论96阅读模式
英文:

Unable to change values of elements around a point in a 2d Array

问题

我目前正在编写一个用Java运行扫雷游戏的程序,但是这个特定的部分一直在给我带来问题。我正在尝试编辑一个类似这样的小的二维数组:{ { 0, -1 }, { 0, 0 } };并且它应该被编辑成一个看起来像这样的场地:{ { 1, -1 }, { 1, 1 } };但是据我观察,它目前没有改变任何值。基本上,我只是想在与炸弹(值为-1)相邻的八个单元格中添加1,除非该单元格本身就是炸弹(-1),但是迄今为止我没有任何运气。我非常感谢您能提供的任何帮助!

  1. public static void setHint(int[][] field)
  2. {
  3. for (int x=0; x < field.length - 1; x++) {
  4. for (int y=0; y < field[0].length - 1; y++) {
  5. if (field[x][y] == -1) {
  6. if (field[x-1][y-1] != -1) {
  7. field[x-1][y-1] += 1;
  8. }
  9. if (field[x-1][y+1] != -1) {
  10. field[x-1][y+1] += 1;
  11. }
  12. if (field[x+1][y-1] != -1) {
  13. field[x+1][y-1] += 1;
  14. }
  15. if (field[x+1][y+1] != -1) {
  16. field[x+1][y+1] += 1;
  17. }
  18. if (field[x-1][y] != -1) {
  19. field[x-1][y] += 1;
  20. }
  21. if (field[x+1][y] != -1) {
  22. field[x+1][y] += 1;
  23. }
  24. if (field[x][y-1] != -1) {
  25. field[x][y-1] += 1;
  26. }
  27. if (field[x][y+1] != -1) {
  28. field[x][y+1] += 1;
  29. }
  30. }
  31. }
  32. }
  33. }
英文:

I am currently coding a program that will run minesweeper in java, but this certain section has been giving me issues. I am trying to edit a small 2d array, like: { { 0, -1 }, { 0, 0 } }; and it should edit it into a field that looks like this: { { 1, -1 }, { 1, 1 } }; but is currently not changing any of the values as far as I can tell. Basically, I am just trying to add 1 to the eight cells surrounding a bomb (value of -1), unless that cell is itself a bomb (-1), but have not had any luck so far. I would greatly appreciate any help you could offer!

  1. public static void setHint(int[][] field)
  2. {
  3. for (int x=0; x &lt; field.length - 1; x++) {
  4. for (int y=0; y &lt; field[0].length - 1; y++) {
  5. if (field[x][y] == -1) {
  6. if (field[x-1][y-1] != -1) {
  7. field[x-1][y-1] += 1;
  8. }
  9. if (field[x-1][y+1] != -1) {
  10. field[x-1][y+1] += 1;
  11. }
  12. if (field[x+1][y-1] != -1) {
  13. field[x+1][y-1] += 1;
  14. }
  15. if (field[x+1][y+1] != -1) {
  16. field[x+1][y+1] += 1;
  17. }
  18. if (field[x-1][y] != -1) {
  19. field[x-1][y] += 1;
  20. }
  21. if (field[x+1][y] != -1) {
  22. field[x+1][y] += 1;
  23. }
  24. if (field[x][y-1] != -1) {
  25. field[x][y-1] += 1;
  26. }
  27. if (field[x-1][y-1] != -1) {
  28. field[x-1][y-1] += 1;
  29. }
  30. }
  31. }
  32. }
  33. }

答案1

得分: 0

field.length 是2。因此条件 x &lt; field.length - 1 仅对 x=0 成立。y 的情况类似。因此,你只会评估场地 (0,0)。由于 field[0][0] 的值为0,所以 if 条件为假,因此不会执行任何操作。

因此,你可以首先通过更改循环的结束条件来进行修改:

  1. for (int x=0; x &lt; field.length; x++) {
  2. for (int y=0; y &lt; field[0].length; y++) {

你还需要验证是否没有在矩阵外进行测试。当 x=0 时,在 field[x-1][y] 上进行检查将导致 IndexOutOfBoundException,而在边缘位置(其中 x=0x=length-1y=0y=length-1)的所有字段都将出现这种情况。

英文:

field.length is 2. So condition x &lt; field.length -1 is only true for x=0. The same for y. So you only evaluate the field (0,0). Since the value of field[0][0] is 0, the if condition is false so nothing is executed.

So you can start by changing the end condition of the loops:

  1. for (int x=0; x &lt; field.length; x++) {
  2. for (int y=0; y &lt; field[0].length; y++) {

You also have to verify if you are not testing outside the matrix. When x=0, checking on field[x-1][y] will result in an IndexOutOfBoundException and this will be the case for all fields at the sides (where x=0, x=length-1, y=0,y=length-1)

huangapple
  • 本文由 发表于 2020年10月9日 21:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64281576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定