@@ -15,8 +15,9 @@ function createGrid() {
1515 const cell = document . createElement ( 'div' ) ;
1616 cell . classList . add ( 'grid-cell' ) ;
1717 if ( grid [ i ] [ j ] !== 0 ) {
18- cell . textContent = grid [ i ] [ j ] ;
1918 cell . setAttribute ( 'data-value' , grid [ i ] [ j ] ) ;
19+ } else {
20+ cell . removeAttribute ( 'data-value' ) ;
2021 }
2122 gridContainer . appendChild ( cell ) ;
2223 }
@@ -132,11 +133,15 @@ let touchStartX = 0;
132133let touchStartY = 0 ;
133134
134135function handleTouchStart ( e ) {
136+ e . preventDefault ( ) ; // Prevent default browser handling
135137 touchStartX = e . touches [ 0 ] . clientX ;
136138 touchStartY = e . touches [ 0 ] . clientY ;
137139}
138140
139141function handleTouchEnd ( e ) {
142+ e . preventDefault ( ) ; // Prevent default browser handling
143+ if ( ! touchStartX || ! touchStartY ) return ; // Exit if we didn't get a valid start position
144+
140145 const touchEndX = e . changedTouches [ 0 ] . clientX ;
141146 const touchEndY = e . changedTouches [ 0 ] . clientY ;
142147
@@ -145,7 +150,7 @@ function handleTouchEnd(e) {
145150 const absDx = Math . abs ( dx ) ;
146151 const absDy = Math . abs ( dy ) ;
147152
148- if ( Math . max ( absDx , absDy ) > 20 ) { // Swipe threshold
153+ if ( Math . max ( absDx , absDy ) > 10 ) { // Lower swipe threshold for better responsiveness
149154 if ( absDx > absDy ) {
150155 if ( dx > 0 ) {
151156 handleMove ( 39 ) ; // Swipe right
@@ -160,10 +165,27 @@ function handleTouchEnd(e) {
160165 }
161166 }
162167 }
168+
169+ // Reset touch coordinates
170+ touchStartX = 0 ;
171+ touchStartY = 0 ;
163172}
164173
165- document . addEventListener ( 'touchstart' , handleTouchStart , false ) ;
166- document . addEventListener ( 'touchend' , handleTouchEnd , false ) ;
174+ // Use touchmove to prevent scrolling while swiping
175+ function handleTouchMove ( e ) {
176+ if ( touchStartX && touchStartY ) {
177+ e . preventDefault ( ) ; // Prevent scrolling
178+ }
179+ }
180+
181+ // Remove previous touch listeners if they exist
182+ document . removeEventListener ( 'touchstart' , handleTouchStart ) ;
183+ document . removeEventListener ( 'touchend' , handleTouchEnd ) ;
184+
185+ // Add improved touch event listeners
186+ document . addEventListener ( 'touchstart' , handleTouchStart , { passive : false } ) ;
187+ document . addEventListener ( 'touchend' , handleTouchEnd , { passive : false } ) ;
188+ document . addEventListener ( 'touchmove' , handleTouchMove , { passive : false } ) ;
167189
168190function initGame ( ) {
169191 generateRandom ( ) ;
0 commit comments