@@ -20,15 +20,19 @@ document.addEventListener("DOMContentLoaded", function (event) {
20
20
canvas . onmousedown = mouseDown ;
21
21
mouseUp = true ;
22
22
} ) ;
23
- document . getElementById ( "startstop" ) . addEventListener ( "click" , function ( ) {
24
- if ( isDraw ) {
25
- isDraw = false ;
26
- document . getElementById ( "state" ) . innerText = "Stopped"
23
+ function StartStop ( isdraw ) {
24
+ console . log ( isdraw )
25
+ if ( isdraw ) {
26
+ isDraw = false ;
27
+ document . getElementById ( "state" ) . innerText = "Stopped" ;
27
28
} else {
28
- isDraw = true ;
29
- document . getElementById ( "state" ) . innerText = "Running" ;
30
- draw ( ) ;
29
+ isDraw = true ;
30
+ document . getElementById ( "state" ) . innerText = "Running" ;
31
+ draw ( ) ;
31
32
}
33
+ }
34
+ document . getElementById ( "startstop" ) . addEventListener ( "click" , function ( ) {
35
+ StartStop ( isDraw )
32
36
} )
33
37
document . getElementById ( "simulate" ) . addEventListener ( "change" , function ( ) {
34
38
if ( document . getElementById ( "simulate" ) . checked ) {
@@ -60,16 +64,19 @@ function mouseDown(event) {
60
64
function onMouseMove ( event ) {
61
65
x = parseInt ( event . offsetX / 4 ) ;
62
66
y = parseInt ( event . offsetY / 4 ) ;
63
- grid [ y ] [ x ] = [ 1 , hue ] ;
64
- const randomNumber = Math . floor ( Math . random ( ) * 4 ) + 1 ;
65
- const randomNumber1 = Math . floor ( Math . random ( ) * 4 ) + 1 ;
66
- if ( y != grid . length - 1 && ( randomNumber == 1 || randomNumber1 == 1 ) ) { grid [ y + 1 ] [ x ] = [ 1 , hue ] ; }
67
- if ( x != grid [ y ] . length && ( randomNumber == 2 || randomNumber1 == 2 ) ) { grid [ y ] [ x + 1 ] = [ 1 , hue ] ; }
68
- if ( x != 0 && ( randomNumber == 3 || randomNumber1 == 3 ) ) { grid [ y ] [ x - 1 ] = [ 1 , hue ] ; }
69
- if ( y != 0 && ( randomNumber == 4 || randomNumber1 == 4 ) ) { grid [ y - 1 ] [ x ] = [ 1 , hue ] ; }
70
- if ( isDraw == false ) {
71
- isDraw = true ;
72
- draw ( ) ;
67
+ if ( grid [ y ] [ x ] [ 0 ] != 1 ) {
68
+ grid [ y ] [ x ] = [ 1 , hue ] ;
69
+ const randomNumber = Math . floor ( Math . random ( ) * 4 ) + 1 ;
70
+ const randomNumber1 = Math . floor ( Math . random ( ) * 4 ) + 1 ;
71
+ if ( y != grid . length - 1 && ( randomNumber == 1 || randomNumber1 == 1 ) ) { grid [ y + 1 ] [ x ] = [ 1 , hue ] ; }
72
+ if ( x != grid [ y ] . length && ( randomNumber == 2 || randomNumber1 == 2 ) ) { grid [ y ] [ x + 1 ] = [ 1 , hue ] ; }
73
+ if ( x != 0 && ( randomNumber == 3 || randomNumber1 == 3 ) ) { grid [ y ] [ x - 1 ] = [ 1 , hue ] ; }
74
+ if ( y != 0 && ( randomNumber == 4 || randomNumber1 == 4 ) ) { grid [ y - 1 ] [ x ] = [ 1 , hue ] ; }
75
+ if ( isDraw == false ) {
76
+ isDraw = true ;
77
+ document . getElementById ( "state" ) . innerText = "Running" ;
78
+ draw ( ) ;
79
+ }
73
80
}
74
81
}
75
82
if ( mouseUp == false ) {
0 commit comments