<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> puzzle </title> <Style type = "text/css"> # Pt_main { Margin: 0 auto; Border: 2px solid #000 ;} # Menu { Text-align: center ;} # Pt_main div { Padding: 0px; Margin: 0px; Float: left; Line-height: 200px; Font-size: 100px; Text-align: center ;} # Pt_main div: hover { Cursor: pointer ;} . Hui { Background: # CCC ;} </Style> <Script src = "http://code.jquery.com/jquery-latest.js"> </script> <Script type = "text/javascript"> // Div Border Width Var border_w = 2; // Div size Var size = 50; // Pass Var st = false; // The address of the background image (not provided yet) Var imgurl = ''; // Position of the gray block Var hui = 0; // Game width and height Var width = 3; Var height = 3; $ (Function (){ Init (); }); // Determine whether two numbers can be exchanged Function ut_jh (a, B ){ If (Math. abs (a-B) = width ){ Return true; } If (Math. abs (a-B) = 1 & ut_line (a, B, width )){ Return true; } Return false; } // Determine whether two numbers are in the same row Function ut_line (a, B, width ){ If (parseInt (a/width) = parseInt (B/width )){ Return true; } Else { Return false; } } // Generate an Interchangeable set (a set that can be exchanged with gray blocks) Function ut_ SC (){ // Up to 4 Var li = new Array (); Var I = 0; If (a-width> = 0 ){ Li [I ++] = a-width; } If (a + width <= width * height-1 ){ Li [I ++] = a + width; } If (ut_line (a, A-1, width) & A-1> = 0 ){ Li [I ++] = A-1; } If (ut_line (a, a + 1, width) & a + 1 <= width * height-1 ){ Li [I ++] = a + 1; } // Alert (a + '-length:' + li. length) Return li; } // Block Click Event Function pt_click (){ If (! St ){ Return false; } // Index of the clicked Block Var index = get (). index ($ (this )); If (ut_ut( hui, index )){ Aggregate (hui, index ); // Determine whether to pass Pd (); } } // Two exchanges Function compute (hui _, index _){ // Alert (hui + 1 + "," + (index _ + 1 )); Var list = get (); // all blocks Var h = list. eq (hui _); // gray block Var d = list. eq (index _); // click the block. // Exchange styles H. removeClass (); D. addClass ('hui '); H.html(d.html ()); D.html (''); Hui = index _; // Exchange attributes Tg = h. attr ('tg '); H. attr ('tg ', d. attr ('tg ')); D. attr ('tg ', tg ); } Function init (){ // Cannot be too small... If (width <3 | height <3 ){ Return false; } Hui = width * height-1; Var pp = $ ("# pt_main "); Pp.html (''); // Create and initialize Var k = width * size + border_w * width * 2; Var g = height * size + border_w * height * 2; Pp.css ({'width': k, 'height': g }); For (I = 0; I <width * height; I ++ ){ Var n = $ ("<div>" + (I + 1) + "</div> "); N.css ({'width': size, 'Height': size, Border: border_w + 'px solid #000 ', LineHeight: size + 'px ', FontSize: parseInt (size/3) + 'px' }); If (imgurl! = ''){ } Pp. append (n ); // Alert (I) } Get().last().html (''); Get (). last (). addClass ('hui '); Get (). on ('click', pt_click ); // Append attributes for each block and record the current value $ ("# Pt_main div"). each (function (index, element ){ $ (Element). attr ('tg ', index ); }); Dl (); St = true; } // Determine whether to pass (when each block value corresponds to its own subscript) Function pd (){ Var B = true; Get (). each (function (index, element ){ If ($ (element). attr ('tg ')! = Index ){ B = false; Return false; } }); If (B ){ St = false; Alert ("Congratulations! "); Return true; } Else { Return false; } } // Obtain the set Function get (){ Return $ ("# pt_main div "); } // Disrupt Function dl (){ // Number of interruptions Var count = width * width; For (I = 0; I <count; I ++ ){ // Interchangeable set Var li = ut_ SC (hui ); Var num = parseInt (li. length) * Math. random ()); Aggregate (hui, li [num]); } } // Click events of the initialization button Function csh (){ Var dxv = $ ('# dx'). val (); If (! ParseInt (dxv )){ Alert ('enter a number between 3 and 10 '); $ ('# Dx'). val (''); Return false; } Var v = parseInt (dxv ); If (v <3 | v> 10 ){ Alert ('enter a number between 3 and 10 '); $ ('# Dx'). val (''); Return false; } Width = v; Height = v; Init (); } </Script> </Head> <Body> <Div id = 'menu '> Size: <input id = 'dx' style = "text-align: center; width: 40px;"/> <Button onclick = "csh ()"> initialization </button> <Button onclick = "init ()"> refresh </button> </Div> <Div id = "pt_main"> </Div> </Body> </Html> |