微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法根据游戏标准生成完整的 2D 数组

如何解决无法根据游戏标准生成完整的 2D 数组

我需要根据以下条件生成二维数组网格(4 行 5 列)

  • 网格上随机的 6 个方块设置为“白色”
  • 其余的方格必须设置为 1-6 之间的数字或颜色
  • 放置数字或颜色时,正方形右侧/左侧/上/下的方块可能不是相同的颜色或数字

我在用颜色/数字填充数组时遇到问题,因为二维数组中仍有一些索引显示“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 举报,一经查实,本站将立刻删除。