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.

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement