vendredi 26 novembre 2021

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(" ─────────────");


    }
}



Aucun commentaire:

Enregistrer un commentaire