【发布时间】:2015-07-11 21:58:43
【问题描述】:
我的代码是这样的
public class AsciiGrid
{
private static int ascrow = 0;
private static int asccol = 0;
private static char[][] myAsciiGrid;
private boolean [][] GridOccupied;
private static char space = ' ';
.
.
.
.
.
public String toString()
{
String[][] mystring = new String [ascrow+2][asccol+2];
String space = " ";
for (int i=0; i < ascrow+2; i++)
{
for (int j = 0; j< asccol+2; j++)
{
if ((i==0)||(i==ascrow+1))
mystring[i][j] = "=";
else if ((j==0)||(j==asccol+1))
mystring[i][j] = "|";
else
mystring[i][j] = space;
}
}
return mystring;
}
}
这将返回一个类似网格的东西,我将在其中放置一些图像。图像只会是三角形和矩形 就像:
=========== ===========
| | | |
| * | | ** |
| ** | | ** |
=========== ===========
在我把下一张图片放在那里之前,我想通过使用布尔值来检查空间是否已经被使用。我不希望它们重叠。所以我写的代码就像:
private boolean placeable(int r,int c,int ascrow, int asccol){
}
但我不知道如何将我的处理代码更改为布尔值并测试空间是否在那里。 这是我的copyGrid:
private static char [][] copyGrid(char [][] myAsciiGrid)
{
char[][] newarray1 = new char [myAsciiGrid.length][myAsciiGrid[0].length];
for (int i = 0; i < myAsciiGrid.length; i++)
for(int j = 0; j < myAsciiGrid[0].length; j++)
newarray1[i][j] = myAsciiGrid[i][j];
return newarray1;
}
我不确定这是否正确。
有人可以帮忙吗?谢谢
【问题讨论】:
-
为什么不直接使用
array[i][j].equals(" ")来检查是否可以安全地替换而不重叠。 -
@Daniel Nugent - 大概他的形状有仍然不能重叠的“空”空间?
-
我不知道为什么 OP 正在复制网格。但是好的问题是具体的:“我看到
的行为,我想要 ”会比“这对吗”更好,更“可回答”? -
您需要更好地定义您期望这个布尔值的含义。事实上,我怀疑通过这样做,您将弄清楚如何评估现有数据以确定其真实性,或者如何定义一个结构来为您跟踪它。
-
你应该使用状态模式
标签: java arrays multidimensional-array boolean