lundi 4 décembre 2017

Randomly generate coordinates for a 2D array

I'm trying to randomly generate the letter 'P' on a 2D array (the game board) and it cannot be placed on certain characters. I've got it to work for 90% of the board but for some reason 'P' will not be placed on some of the coordinates. After some testing i'm pretty sure it's due to how I'm generating the random coordinates. Here's the code:

protected String spawnPlayer(){
    Random randomGenerator = new Random(); 
    for(int i=0; i<Board.length; i++){
        for(int j=0; j<Board[i].length; j++){
            if(Board[i][j]=='G' || Board[i][j]=='E' || Board[i][j]=='#'){
                String numx = Integer.toString(i);
                String numy = Integer.toString(j);
                String num = numx+numy;
                int excepResult = Integer.parseInt(num);
                exceptions.add(excepResult);
            }
        }
    }
    randomIndexX = randomGenerator.nextInt(Board.length);  //random coordinates
    randomIndexY = randomGenerator.nextInt(Board[0].length);  //random coordinates
    System.out.println(randomIndexX);
    System.out.println(randomIndexY);
    String ranIndexX = Integer.toString(randomIndexX);
    String ranIndexY = Integer.toString(randomIndexY);
    String ran= ranIndexX+ranIndexY;
    int ranResult = Integer.parseInt(ran);
    if(!exceptions.contains(ranResult)){  
        for(int row=0; row<Board.length; row++){
            for(int column=0; column<(Board[0].length); column++){
                Board[randomIndexX][randomIndexY] = 'P';
                yPos = randomIndexX;
                xPos = randomIndexY;
                System.out.print(Board[row][column]);
            }
            System.out.println();
        }
    }else{
        exceptions.clear();
        spawnPlayer();
    } 
    return null;
}

Here is the 2d array:

map = new char[][]  {
    {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
    {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
    {'#','.','.','.','.','.','.','G','.','.','.','.','.','.','.','.','.','E','.','#'},
    {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
    {'#','.','.','E','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
    {'#','.','.','.','.','.','.','.','.','.','.','.','G','.','.','.','.','.','.','#'},
    {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
    {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
    {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
    };

The output when I loop this code: (this shows what coordinates aren't being picked up)

####################
#.........PPPPPPPPP#
#PPPPPPGPPPPPPPPPEP#
#PPPPPPPPPPPPPPPPPP#
#PPEPPPPPPPPPPPPPPP#
#PPPPPPPPPPPGPPPPPP#
#PPPPPPPPPPPPPPPPPP#
#PPPPPPPPPPPPPPPPPP#
####################

I know my naming of the index X and index Y is wrong but pls ignore that :) Any help would be appreciated, thank you!




Aucun commentaire:

Enregistrer un commentaire