Skip to content
Advertisement

how to make the Value “B” & “K” fill only certain number of cells Randomly

I have board made from class in Java with values (K, L) that will randomly fill the board. I wonder how to make the value “K” fill only certain number of cells (8 cells) randomly and the rest “L” would fill the board. My aim is to get a board where “K” appears 8 times and the rest would be “L” all randomly.

public class SimpsonsBoard {
    public static void main(String[] args) {
        String[][] board = new String[6][6];

        for(int i=0; i<board.length; i++) {
            for(int j=0; j<board.length; j++) {
                double random = Math.random();
                if (random < .8 ) {
                    board[i][j]= String.valueOf('K');
                }else {
                    board[i][j]= String.valueOf('L');
                }
                System.out.print(board[i][j]);
            }
        }


        System.out.println(" ─────────────");
        System.out.println("│" + board[0][0] + "│" + board[0][1] + "│" + board[0][2] + "│" + board[0][3] + "│" + board[0][4] + "│" + board[0][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[1][0] + "│" + board[1][1] + "│" + board[1][2] + "│" + board[1][3] + "│" + board[1][4] + "│" + board[1][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[2][0] + "│" + board[2][1] + "│" + board[2][2] + "│" + board[2][3] + "│" + board[2][4] + "│" + board[2][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[3][0] + "│" + board[3][1] + "│" + board[3][2] + "│" + board[3][3] + "│" + board[3][4] + "│" + board[3][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[4][0] + "│" + board[4][1] + "│" + board[4][2] + "│" + board[4][3] + "│" + board[4][4] + "│" + board[4][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[5][0] + "│" + board[5][1] + "│" + board[5][2] + "│" + board[5][3] + "│" + board[5][4] + "│" + board[5][5] + "│");
        System.out.println(" ─────────────");


    }
}

Advertisement

Answer

The simplest way is to fill your board array with K and then randomly replace 8 of them with L as suggested by @dan1st. The following should work:

public class SimpsonsBoard {
    public static void main(String[] args) {
        String[][] board = new String[6][6];
        for (String[] row : board) {
            Arrays.fill(row, "K");
        }

        Random random = new Random();
        for(int i = 1; i <= 8; i++) {
            int x = random.nextInt(6);
            int y = random.nextInt(6);
            board[x][y]= "L";
        }

        System.out.println(" ─────────────");
        System.out.println("│" + board[0][0] + "│" + board[0][1] + "│" + board[0][2] + "│" + board[0][3] + "│" + board[0][4] + "│" + board[0][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[1][0] + "│" + board[1][1] + "│" + board[1][2] + "│" + board[1][3] + "│" + board[1][4] + "│" + board[1][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[2][0] + "│" + board[2][1] + "│" + board[2][2] + "│" + board[2][3] + "│" + board[2][4] + "│" + board[2][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[3][0] + "│" + board[3][1] + "│" + board[3][2] + "│" + board[3][3] + "│" + board[3][4] + "│" + board[3][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[4][0] + "│" + board[4][1] + "│" + board[4][2] + "│" + board[4][3] + "│" + board[4][4] + "│" + board[4][5] + "│");
        System.out.println(" ─────────────");
        System.out.println("│" + board[5][0] + "│" + board[5][1] + "│" + board[5][2] + "│" + board[5][3] + "│" + board[5][4] + "│" + board[5][5] + "│");
        System.out.println(" ─────────────");
    }
}
8 People found this is helpful
Advertisement