问题描述
我需要根据以下条件生成二维数组网格(4 行 5 列)
我在用颜色/数字填充数组时遇到问题,因为二维数组中仍有一些索引显示“NULL”。我不确定我做错了什么。
public class WindowGenerator {
private String WindowNum;
public String getwindowNum() {
return WindowNum;
}
public void setwindowNum(String windowNum) {
WindowNum = windowNum;
}
private String windowGrid[][] = new String[4][5];
public String[][] generate()
{
//set all blocks in the grid to null
//null denotes empty block
for(int a = 0; a<4; a++)
{
for(int b = 0; b<5; b++)
{
windowGrid[a][b] = null;
}
}
ArrayList<Integer> row = new ArrayList<Integer>();
ArrayList<Integer> col = new ArrayList<Integer>();
Random random = new Random();
//populate row List
for(int i=0;i<4;i++)
{
row.add(i);
}
//populate column List
for(int x= 0; x<5;x++)
{
col.add(x);
}
//populate grid with blank tiles first == WHITE
for(int y=0; y<7; y++)
{
int r = row.get(random.nextInt(row.size()));
int c = col.get(random.nextInt(col.size()));
windowGrid[r][c] = "White";
}
solve(0,0);
return windowGrid;
}
private void solve (int row,int col)
{
Random gen = new Random();
boolean VFound = false;
//list of possibilities
ArrayList<String> options = new ArrayList<String>();
//populate Options List
options.add("one");
options.add("two");
options.add("three");
options.add("four");
options.add("five");
options.add("six");
options.add("purple");
options.add("red");
options.add("yellow");
options.add("green");
while (options.isEmpty()==false||VFound == false) {
//pick random value from Options List
String val = options.get(gen.nextInt(options.size()));
if(windowGrid[row][col] != "white") {
if (checkSection(row,col,val) == true)
{
windowGrid[row][col] = val;
VFound = true;
break;
}
else {
//remove value from list of available options
Integer index = options.indexOf(val);
options.remove(index);
if(options.isEmpty())
break;
}
}
}
//if out of numbers go back 1 block
if(options.isEmpty() == true)
{
back(row,col);
}
//if a value was added and there are still more empty blocks in the window grid
//go forward 1 block
else if(VFound == true && (emptyCheck() == true))
{
next(row,col);
}
}
//move to the next block
private void next(int row,int col){
if(col==4)
solve(row +1,0);
else
solve(row,col+1);
}
//move to the prevIoUs block
private void back(int row,int col)
{
if(row == 0)
solve(row -1,4);
else
solve(row,col-1);
}
//check each element of the section for the value (LEFT/RIGHT/UP/DOWN to the index),if value is found return false
private boolean checkSection(int xPos,int yPos,String val)
{
String[][] section = new String[3][3];
section = getSection(xPos,yPos);
for(int i = 0; i<3;i++)
{
for(int j =0 ;j<3;j++)
{
if(section[i][j] == val)
{
return false;
}
}
}
return true;
}
private String[][] getSection(int xPos,int yPos)
{
String[][] section = new String[3][3];
int xIndex = 3*(xPos / 3);
int yIndex = 3*(yPos / 3);
for(int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
section[i][j] = windowGrid[xIndex+i][yIndex+j];
}
}
return section;
}
//searches grid for empty squares
//o denotes empty
private boolean emptyCheck(){
for(int i = 0; i < 4;i++)
{
for(int j = 0; j<5; j++)
{
if(windowGrid[i][j] == null)
{
return false;
}
}
}
return true;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)