Over 20 lines of js Code to write a simple 3x3 puzzle game

Source: Internet
Author: User
By convention, it is necessary: gameplay: direction keys: 12345678 _ out of order, even if the game is successful! Compatible with various browsers and uses reverse order and determination to ensure certain solutions. The next step is the js Code, which does not require HTML combination. Simply put the code in the body. Click here for a direct experience. & Lt; SC... SyntaxHighlighter. all ();

 

By convention, it is required:

Gameplay: direction keys

Winning or losing: 12345678 _ out of order, even if it is successful!

Compatible with various browsers and uses reverse order and judgment to ensure certain solutions.

The next step is the js Code, which does not require HTML combination. Simply put the code in the body. Click here for a direct experience.

 

 

Script

Function gd (){

Var arr = [1, 2, 3, 4, 5, 6, 7, 8]. sort (function () {return Math. random ()>. 5 });

For (var I = 0, k = 0, n = arr. length; I

For (var j = I + 1; j

Arr [I]> arr [j] & (k = 1-k );

K & (I = arr [N-2]) & (arr [N-2] = arr [n-1]) & (arr [n-1] = I );

Return arr. concat (0 );

}

Function v (){

Return (d + ''). replace (0,''). replace (/(. {5}),/g, '$1 \ r \ n ');

}

Document. writeln ('');

Var p = 8, d = gd (), t1 = document. getElementById ("t1 ");

SetTimeout ('T1. value = v () ', 10 );

Document. onkeyup = function (e ){

Var k = (e | window. event). keyCode-36, q;

If (k = 1 & (p % 3 <2) | k = 3 & (p % 3> 0) | k = 2 & p <6 | k = 4 & p> 2 ){

Q = p + (k> 2? 1:-1) * (k % 2*2-3 );

D [p] = d [q], d [p = q] = 0;

If (t1.value = v (). match (/^ 1, 2, 3 [^ \ d] * 4, 5, 6 [^ 0] * $ /))

Alert ('You WIN! ');

}

}

Script

Replay supplement: I re-wrote a version of any row and column and made some enhancements. For details, see "write an M * N puzzle game with over 30 lines of js Code". choose M * nbu, instant switch, instant replay.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.