有没有一种更简单的方法来在一个11×11的矩阵中找到一个3×3的矩阵?

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

Is there a simpler way to find a 3x3 matrix in a 11x11 matrix?

问题

// 以下是翻译好的代码部分:

for (int row = 0; row < fullMap.length; row++) {
    for (int col = 0; col < fullMap[row].length; col++) {
        if (((row == currentRow - 1) && (col == currentCol)) && currentRow != 0) {
            // 存储块信息
        } else if (((row == currentRow - 1) && (col == currentCol + 1)) && (currentRow != 0 && currentCol != 10)) {
            // 存储块信息
        } else if (((row == currentRow - 1) && (col == currentCol - 1)) && (currentRow != 0 && currentCol != 0)) {
            // 存储块信息
        } else if (((row == currentRow) && (col == currentCol + 1)) && currentCol != 10) {
            // 存储块信息
        } else if (((row == currentRow) && (col == currentCol - 1)) && currentCol != 0) {
            // 存储块信息
        } else if (((row == currentRow + 1) && (col == currentCol + 1)) && (currentRow != 10 && currentCol != 10)) {
            // 存储块信息
        } else if (((row == currentRow + 1) && (col == currentCol)) && currentRow != 10) {
            // 存储块信息
        } else if (((row == currentRow + 1) && (col == currentCol - 1)) && (currentRow != 10 && currentCol != 0)) {
            // 存储块信息
        }
    }
}

如果需要其他信息,请随时提供。

英文:

A few friends and I are creating a simulation based game using java where our "players" go out on an 11x11 matrix full of a Class called Chunk. In order for the players to be able to interact with the world around it, it requires us to see the information of the Chunks in a 3x3 surrounding the player.

Example(smaller matrix size, x=Nothing There, o=Person):

xxxxxxxxxxx
xxxxxxxxxxx
xxxxxoxxxxx
xxxxxxxxxxx
xxxxxxxxxxx

We would need to be able to collect the chunks surrounding the player(o)(in this case row's [1],[2],[3] and col's[4],[5],[6]). Of course, the 3x3 matrix cannot leave the bounds of the matrix and if it does we just ignore the ones that aren't there and collect the Chunks we can collect. Currently, we have a block of code that works for the desired task but we feel like it could be made faster, cleaner, or completely remade in another way.

fullMap is an array which is initialized to be an 11x11 full of Chunk's and
currentRow, currentCol both are integers which relate to the position in fullMap the player is currently in.

for(int row = 0; row &lt; fullMap.length; row++) {
for (int col = 0; col &lt; fullMap[row].length; col++) {
if (((row == currentRow-1) &amp;&amp; (col == currentCol)) &amp;&amp; currentRow != 0) {
//store the chunk
} else if (((row == currentRow-1) &amp;&amp; (col == currentCol+1)) &amp;&amp; (currentRow != 0 &amp;&amp; currentCol != 10)) {
//store the chunk
} else if (((row == currentRow-1) &amp;&amp; (col == currentCol-1)) &amp;&amp; (currentRow != 0 &amp;&amp; currentCol != 0)) {
//store the chunk
} else if (((row == currentRow) &amp;&amp; (col == currentCol+1)) &amp;&amp; currentCol != 10) {
//store the chunk
} else if (((row == currentRow) &amp;&amp; (col == currentCol-1))  &amp;&amp; currentCol != 0) {
//store the chunk
} else if (((row == currentRow+1) &amp;&amp; (col == currentCol+1)) &amp;&amp; (currentRow != 10 &amp;&amp; currentCol != 10)){
//store the chunk
} else if (((row == currentRow+1) &amp;&amp; (col == currentCol)) &amp;&amp; currentRow != 10) {
//store the chunk
} else if (((row == currentRow+1) &amp;&amp; (col == currentCol-1)) &amp;&amp; (currentRow != 10 &amp;&amp; currentCol != 0)){
//store the chunk
}
}
}

If other information is needed I am happy to post it.

答案1

得分: 2

你不需要迭代fullMap,对吗?
你知道(currentRow, currentCol),从那里开始,在每个方向上都进行-1/+1移动。总共你将有9个字段需要检查。

    ...
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1 ; j++) {
        if (isMovePossible(currentRow + i, currentCol + j)) {
          // 存储块
        }
      }
    }
    ...


  private static boolean isMovePossible(int row, int col) {
    return row >= 0 && row <= 10 && col >= 0 && col <= 10;
  }
英文:

You do not need to iterate over the fullMap, do you?
You know the (currentRow, currentCol) and from there you go -1/+1 in each direction. In total you will have 9 fields to check.

    ...
for (int i = -1; i &lt;= 1; i++) {
for (int j = -1; j &lt;= 1 ; j++) {
if (isMovePossible(currentRow + i, currentCol + j)) {
// store chunk
}
}
}
...
private static boolean isMovePossible(int row, int col) {
return row &gt;= 0 &amp;&amp; row &lt;= 10 &amp;&amp; col &gt;= 0 &amp;&amp; col &lt;=10;
}

huangapple
  • 本文由 发表于 2020年5月4日 03:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580601.html
匿名

发表评论

匿名网友

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

确定