英文:
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 < fullMap.length; row++) {
for (int col = 0; col < fullMap[row].length; col++) {
if (((row == currentRow-1) && (col == currentCol)) && currentRow != 0) {
//store the chunk
} else if (((row == currentRow-1) && (col == currentCol+1)) && (currentRow != 0 && currentCol != 10)) {
//store the chunk
} else if (((row == currentRow-1) && (col == currentCol-1)) && (currentRow != 0 && currentCol != 0)) {
//store the chunk
} else if (((row == currentRow) && (col == currentCol+1)) && currentCol != 10) {
//store the chunk
} else if (((row == currentRow) && (col == currentCol-1)) && currentCol != 0) {
//store the chunk
} else if (((row == currentRow+1) && (col == currentCol+1)) && (currentRow != 10 && currentCol != 10)){
//store the chunk
} else if (((row == currentRow+1) && (col == currentCol)) && currentRow != 10) {
//store the chunk
} else if (((row == currentRow+1) && (col == currentCol-1)) && (currentRow != 10 && 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 <= 1; i++) {
for (int j = -1; j <= 1 ; j++) {
if (isMovePossible(currentRow + i, currentCol + j)) {
// store chunk
}
}
}
...
private static boolean isMovePossible(int row, int col) {
return row >= 0 && row <= 10 && col >= 0 && col <=10;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论