@@ -134,6 +134,24 @@ function paint(cell){
134134 updateStats ( ) ;
135135}
136136
137+
138+ function getFillPercent ( ) {
139+ const input = document . getElementById ( 'fillPercent' ) ;
140+ const parsed = Number . parseFloat ( input . value . replace ( ',' , '.' ) ) ;
141+ const safe = Number . isFinite ( parsed ) ? Math . min ( 100 , Math . max ( 0 , parsed ) ) : 100 ;
142+ input . value = `${ safe } %` ;
143+ return safe ;
144+ }
145+
146+ function pickRandomCells ( cells , count ) {
147+ const pool = [ ...cells ] ;
148+ for ( let i = pool . length - 1 ; i > 0 ; i -- ) {
149+ const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
150+ [ pool [ i ] , pool [ j ] ] = [ pool [ j ] , pool [ i ] ] ;
151+ }
152+ return pool . slice ( 0 , count ) ;
153+ }
154+
137155function updateStats ( ) {
138156 const mult = + document . getElementById ( 'multiplier' ) . value ;
139157 const isSpreadEnabled = document . getElementById ( 'commitSpread' ) . value === 'plus2' ;
@@ -171,11 +189,21 @@ document.getElementById('clearBtn').addEventListener('click', ()=>{
171189 updateStats ( ) ; setStatus ( '' ) ;
172190} ) ;
173191document . getElementById ( 'fillBtn' ) . addEventListener ( 'click' , ( ) => {
174- document . querySelectorAll ( '.cell:not(.out)' ) . forEach ( c => {
192+ const fillPercent = getFillPercent ( ) ;
193+ const activeCells = [ ...document . querySelectorAll ( '.cell:not(.out)' ) ] ;
194+ const targetCount = Math . round ( activeCells . length * ( fillPercent / 100 ) ) ;
195+ const picked = new Set ( pickRandomCells ( activeCells , targetCount ) ) ;
196+
197+ activeCells . forEach ( c => {
175198 const i = + c . dataset . i ;
176- const level = resolveLevel ( ) ;
177- grid [ i ] = level ;
178- c . style . background = COLORS [ level ] ;
199+ if ( picked . has ( c ) ) {
200+ const level = resolveLevel ( ) ;
201+ grid [ i ] = level ;
202+ c . style . background = COLORS [ level ] ;
203+ return ;
204+ }
205+ grid [ i ] = 0 ;
206+ c . style . background = COLORS [ 0 ] ;
179207 } ) ;
180208 updateStats ( ) ;
181209} ) ;
0 commit comments