[英]How to check neighbours of 2D array board game
So I have to create a 2-dimensional board game.所以我必须创建一个二维棋盘游戏。 I've managed to do the board and I'm able to set the 'X' row and col coordinated show.我已经设法完成了董事会,并且能够设置“X”行和 col 协调显示。 The last this what I need to do, is check the surroundings of an 'X' whether given coordinates land on this neighbourhood area or not.我需要做的最后一个是检查“X”的周围环境,无论给定的坐标是否落在这个附近区域。 This should cover isIsolated()
method.这应该涵盖isIsolated()
方法。
I have two cases:我有两种情况:
return false
, if somewhere else as not in the neighbourhood, then return true
如果坐标落在(粗体)0区域,我的方法应该return false
,如果其他地方不在附近,则return true
(3, 3)
, checking if point (2, 1)
isIsolated()
and it should return true
, because it isn't close to or in the area of X
, for point (2, 3)
, it returns false
, because it lands on the area例如,将我的点设置在(3, 3)
上,检查点(2, 1)
isIsolated()
并且它应该返回true
,因为它不靠近或不在X
的区域内,对于点(2, 3)
,它返回false
,因为它降落在该区域上0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 X 0 0 0 0 0 X 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
return false
, otherwise return true
同样的事情,如果给定的坐标落在(粗体)0区域,我的方法应该return false
,否则return true
(1, 1)
and (3, 4)
, so if I want to check if point (2, 2)
isIsolated()
, I should get false
, if fg point (1, 3)
, it should return true
例如,我将我的点设置在(1, 1)
和(3, 4)
上,所以如果我想检查点(2, 2)
isIsolated()
,我应该得到false
,如果 fg 点(1, 3)
, 它应该返回true
X 0 0 0 X 0 0 0
0 0 0 0 0 0 0 0
0 0 0 X 0 0 0 X
My code looks like this so far:到目前为止,我的代码如下所示:
public class Board {
public char[][] field;
public Board(int rows, int columns) {
field = new char[rows][columns];
for(int row = 0; row < rows; row++){
for (int col = 0; col < columns; col++){
field[row][col] = '0';
}
}
}
public void set(int row, int column) {
try{
for (int r = 0; r < field[0].length ; r++){
for(int c = 0; c < field[1].length ; c++){
if(r == row - 1 && c == column - 1 && field[r][c] != 'X'){
field[r][c] = 'X';
}
}
}
}catch (IllegalArgumentException e){
if(row > field[0].length || column > field[1].length){
System.out.println("Board isn't big enough!");
}
}
System.out.println(Arrays.deepToString(field));
}
public boolean isSet(int row, int column) {
for(int r = 0; r < field[0].length; r++){
for(int c = 0; c < field[1].length; c++){
if(r == row - 1 && c == column - 1){
if(field[r][c] == 'X'){
return true;
}
}
}
}
return false;
}
public boolean isIsolated(int row, int column) {
return true;
}
}
And tests for it:并对其进行测试:
@Test
public void boardKnowsIfSquareIsIsolated() {
Board board = new Board(5, 6);
assertThat(board.isIsolated(3, 3), is(true));
board.set(3, 3);
assertThat(board.isIsolated(2, 2), is(false));
assertThat(board.isIsolated(2, 3), is(false));
assertThat(board.isIsolated(2, 4), is(false));
assertThat(board.isIsolated(3, 2), is(false));
assertThat(board.isIsolated(3, 3), is(false));
assertThat(board.isIsolated(3, 4), is(false));
assertThat(board.isIsolated(4, 2), is(false));
assertThat(board.isIsolated(4, 3), is(false));
assertThat(board.isIsolated(4, 4), is(false));
assertThat(board.isIsolated(4, 5), is(true));
}
@Test
public void canHandleEdges() {
Board board = new Board(3, 4);
board.set(1, 1);
board.set(3, 4);
assertThat(board.isIsolated(1, 1), is(false));
assertThat(board.isIsolated(1, 2), is(false));
assertThat(board.isIsolated(3, 3), is(false));
assertThat(board.isIsolated(3, 4), is(false));
assertThat(board.isIsolated(1, 4), is(true));
}
If you have a 0-based r,c
coordinate and want to check if it is an 'X'
or is near an 'X'
, you really want to check the 3-by-3 square around the r,c
coordinate, ie the square with upper-left at r-1,c-1
and lower-right at r+1,c+1
. If you have a 0-based r,c
coordinate and want to check if it is an 'X'
or is near an 'X'
, you really want to check the 3-by-3 square around the r,c
coordinate, ie左上角在r-1,c-1
和右下角在r+1,c+1
的正方形。 You can use loops for that.您可以为此使用循环。
private static boolean isNearX(int r, int c) {
for (y = r - 1; y <= r + 1; y++)
for (x = c - 1; x <= c + 1; x++)
if (field[y][x] == 'X')
return true;
return false;
}
Of course, that 3-by-3 square may fall partially outside the board, so we need to check the upper and lower limits of the loops.当然,那个 3×3 的方块可能会部分落在棋盘之外,所以我们需要检查循环的上限和下限。 This more efficient than checking the coordinates for every cell in the 3-by-3 square.这比检查 3×3 正方形中每个单元格的坐标更有效。
private static boolean isNearX(int r, int c) {
int minY = Math.max(r - 1, 0), maxY = Math.min(r + 1, field.length - 1);
int minX = Math.max(c - 1, 0), maxX = Math.min(c + 1, field[0].length - 1);
for (y = minY; y <= maxY; y++)
for (x = minX; x <= maxX; x++)
if (field[y][x] == 'X')
return true;
return false;
}
Now, if you want to call it with the 1-based row,column
coordinate instead, that's easy to adjust.现在,如果你想用基于 1 的row,column
坐标来调用它,那很容易调整。 We can also flip the true/false value to make it match the isIsolated
point of view instead.我们还可以翻转真/假值,使其与isIsolated
的观点相匹配。
public static boolean isIsolated(int row, int column) {
int minY = Math.max(row - 2, 0), maxY = Math.min(row, field.length - 1);
int minX = Math.max(column - 2, 0), maxX = Math.min(column, field[0].length - 1);
for (y = minY; y <= maxY; y++)
for (x = minX; x <= maxX; x++)
if (field[y][x] == 'X')
return false;
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.