基于游戏标准生成完整2d数组的问题

nvbavucw  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(176)

我需要根据以下条件生成一个二维数组网格(4行5列)
网格上的6个正方形随机设置为“白色”
其余的方格必须设置为1-6之间的数字或颜色
在放置数字或颜色时,正方形的右/左/上/下正方形的颜色或数字可能不同
我在用颜色/数字填充数组时遇到了一个问题,因为2d数组中仍然有一些索引显示“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;
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题