Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

java - Trouble with generating a complete 2D Array based on game criteria

I need to generate a 2D array grid (4 rows, 5 columns) based on the following criteria

  • 6 squares on random on the grid be set to "white"
  • the rest of the grid squares must be set to either a number from 1-6 or a colour
  • when placing a number or colour, the square to the right/left/Up/Down of the square may not be of the same colour or number

I'm having an issue in populating the Array with colours/numbers, as there are still some indexes in the 2D array that show "NULL". I'm not sure what I'm doing wrong.

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;
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...