@@ -78,43 +78,39 @@ function get2048GameHTML() {
7878 box-shadow: var(--shadow, 0 2px 4px rgba(0,0,0,0.05));
7979 }
8080
81- /* Layout Manager for Board + Controls */
82- .game-layout {
83- display: flex;
84- flex-direction: row; /* Aligns items horizontally */
85- align-items: center;
86- justify-content: center;
87- gap: 30px; /* Space between board and controls */
81+ #grid-container {
82+ width: 100%;
83+ max-width: 440px;
8884 margin: auto;
8985 }
9086
9187 #grid-container {
9288 width: 340px;
9389 height: 340px;
9490 display: grid;
95- grid-template-columns: repeat(4, 70px);
96- grid-template-rows: repeat(4, 70px);
91+ grid-template-columns: repeat(4, 1fr);
9792 gap: 10px;
9893 background: var(--panel-color, #bbada0);
9994 border: 10px solid var(--panel-color, #bbada0);
10095 padding: 0;
10196 border-radius: 10px;
102- box-sizing: border-box;
103- box-shadow: inset 0 2px 8px rgba(0,0,0,0.15);
97+ touch-action: none;
10498 }
10599
106100 .tile {
107- width: 70px;
108- height: 70px;
109- display: flex;
110- justify-content: center;
111- align-items: center;
112- font-size: 26px;
113- font-weight: bold;
114- border-radius: 6px;
115- box-sizing: border-box;
116- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
117- }
101+ width: 100%;
102+ aspect-ratio: 1 / 1;
103+ height: auto;
104+ background: var(--control-color);
105+
106+ display: flex;
107+ justify-content: center;
108+ align-items: center;
109+
110+ font-size: clamp(16px, 5vw, 28px);
111+ font-weight: bold;
112+
113+ border-radius: 12px;
118114
119115 /* Right Side Controls Layout */
120116 .controls {
@@ -441,22 +437,41 @@ function init2048Game() {
441437 }
442438 }
443439
444- gridContainer . addEventListener ( "touchstart" , ( e ) => {
445- touchStartX = e . touches [ 0 ] . clientX ;
446- touchStartY = e . touches [ 0 ] . clientY ;
447- } , { passive : true } ) ;
448-
449- gridContainer . addEventListener ( "touchend" , ( e ) => {
450- if ( ! e . changedTouches . length ) return ;
451- handleSwipeEnd ( e . changedTouches [ 0 ] . clientX , e . changedTouches [ 0 ] . clientY ) ;
452- } , { passive : true } ) ;
453-
454- let isDragging = false ;
455- gridContainer . addEventListener ( "mousedown" , ( e ) => {
456- isDragging = true ;
457- touchStartX = e . clientX ;
458- touchStartY = e . clientY ;
459- } ) ;
440+ let touchStartX = 0 ;
441+ let touchStartY = 0 ;
442+
443+ gridContainer . addEventListener ( 'touchstart' , e => {
444+ e . preventDefault ( ) ;
445+ touchStartX = e . changedTouches [ 0 ] . screenX ;
446+ touchStartY = e . changedTouches [ 0 ] . screenY ;
447+ } , { passive : false } ) ;
448+
449+ gridContainer . addEventListener ( 'touchend' , e => {
450+ e . preventDefault ( ) ;
451+ let touchEndX = e . changedTouches [ 0 ] . screenX ;
452+ let touchEndY = e . changedTouches [ 0 ] . screenY ;
453+
454+ let dx = touchEndX - touchStartX ;
455+ let dy = touchEndY - touchStartY ;
456+ let moved = false ;
457+
458+ if ( Math . abs ( dx ) > Math . abs ( dy ) ) {
459+ if ( dx > 30 ) moved = moveRight ( ) ;
460+ else if ( dx < - 30 ) moved = moveLeft ( ) ;
461+ } else {
462+ if ( dy > 30 ) moved = moveDown ( ) ;
463+ else if ( dy < - 30 ) moved = moveUp ( ) ;
464+ }
465+
466+ if ( moved ) {
467+ addNewTile ( ) ;
468+ drawBoard ( ) ;
469+ }
470+ } , { passive : false } ) ;
471+
472+ document
473+ . getElementById ( "restart-btn" )
474+ . addEventListener ( "click" , ( ) => {
460475
461476 window . addEventListener ( "mouseup" , ( e ) => {
462477 if ( ! isDragging ) return ;
0 commit comments