


JS Lianliankan source code perfect annotated version (recommended)_javascript skills
When I have nothing to do, I also wrote a javascript to read in succession. The comments are relatively complete. Friends who want to learn should read it.
The most difficult part of Lianliankan is probably the path search, that is, to see if there is a path between two points clicked by the mouse. I saw someone's recursive writing method, and I felt itchy, so I figured it out and found that it's not that difficult without recursion.
The path search is analyzed from simple to difficult. First analyze whether a straight line can be connected in a straight line, then analyze whether two points on a straight line can be connected by taking two turns, and finally analyze the situation when they are not on a straight line.
Tested under IE6, IE8, firefox3.0.3.
<script><BR>//以下部分为路径搜索算法部分,与表现层无关 <P>//全局变量<BR>var X = 16;//总行数<BR>var Y = 14;//总列数<BR>var types = 15;//图形种类 <P>//布局矩阵<BR>//为了算法方便,矩阵的第一行,第一列,最后一行,最后一列都标注为0,天然通路。<BR>var arr = new Array(Y);<BR>var tbl;//显示布局的table元素 <P>var p1 = null;//搜索路径用的第1个点的坐标<BR>var p2 = null;//搜索路径用的第2个点的坐标<BR>var e1 = null;//第1个点对应的元素<BR>var e2 = null;//第2个点对应的元素<P>//Path search, given two points, search for a path<BR>//The path is represented by connectable points<BR>function getPath(p1, p2){<BR> //P1 before starting the search , p2 is sorted so that p2 is as far to the lower right of p1 as possible. <BR> //This can simplify the algorithm<BR> if(p1.x>p2.x){<BR> var t = p1; <BR> p1 = p2;<BR> p2 = t; <BR> } <BR> else if(p1.x==p2.x){<BR> if(p1.y>p2.y){<BR> var t = p1; <BR> p1 = p2;<BR> p2 = t; <BR> }<BR> }<BR> //By analyzing the positional relationship between the two points, gradually analyze each type from simple to difficult <BR> //The first type, two points Whether it is on a straight line and the two points can be connected by a straight line<BR> if((onlineY(p1, p2)||onlineX(p1, p2)) && hasLine(p1, p2)){<BR> status = ' type 1';<BR> return [p1,p2];<BR> }<BR> //The second type, if any one of the two points is completely surrounded, it will not work. <BR> if( !isEmpty({x:p1.x, y:p1.y 1}) && !isEmpty({x:p1.x, y:p1.y-1}) && !isEmpty({x: p1.x-1, y:p1.y}) && !isEmpty({x:p1.x 1, y:p1.y}) ){<BR> status = 'type 2';<BR> return null; <BR> }<BR> if( !isEmpty({x:p2.x, y:p2.y 1}) && !isEmpty({x:p2.x, y:p2.y-1}) && !isEmpty ({x:p2.x-1, y:p2.y}) && !isEmpty({x:p2.x 1, y:p2.y}) ){<BR> status = 'type 2';<BR> return null;<BR> }<BR> //The third type, two points are on a straight line, but cannot be connected by a straight line<BR> var pt0, pt1, pt2, pt3;<BR> //If they are all in x axis, scan possible paths from left to right, <BR> //Construct 4 vertices pt0, pt1, pt2, pt3 each time, and then see if they are connected between each other <BR> if(onlineX(p1, p2 )){<BR> for(var i=0; i<Y; i ){<BR> if(i==p1.y){<BR> continue;<BR> }<BR> pt0 = p1;<BR> pt1 = {x: p1.x, y: i};<BR> pt2 = {x: p2.x, y: i};<BR> pt3 = p2;<BR> //If the vertex is not empty , then the road is blocked. <BR> if(!isEmpty(pt1) || !isEmpty(pt2)){<BR> continue;<BR> }<BR> if( hasLine(pt0, pt1) && hasLine(pt1, pt2) && hasLine(pt2 , pt3) ){<BR> status = '(x:' pt0.x ',y:' pt0.y ')' ', (x:' pt1.x ',y:' pt1.y ')' ' , (x:' pt2.x ',y:' pt2.y ')' ', (x:' pt3.x ',y:' pt3.y ')';<BR> return [pt0, pt1, pt2 , pt3];<BR> }<BR> }<BR> }<BR> //If they are all on the y-axis, scan possible paths from top to bottom, <BR> //Construct 4 vertices pt0 each time, pt1, pt2, pt3, and then see if they are connected between each other <BR> if(onlineY(p1, p2)){<BR> for(var j=0; j<X; j ){<BR> if( j==p1.x){<BR> continue; <BR> }<BR> pt0 = p1;<BR> pt1 = {x:j, y:p1.y};<BR> pt2 = {x:j , y:p2.y};<BR> pt3 = p2;<BR> //If the vertex is not empty, the road is blocked. <BR> if(!isEmpty(pt1) || !isEmpty(pt2)){<BR> continue;<BR> }<BR> if( hasLine(pt0, pt1) && hasLine(pt1, pt2) && hasLine(pt2 , pt3) ){<BR> status = '(x:' pt0.x ',y:' pt0.y ')' ', (x:' pt1.x ',y:' pt1.y ')' ' , (x:' pt2.x ',y:' pt2.y ')' ', (x:' pt3.x ',y:' pt3.y ')';<BR> return [pt0, pt1, pt2 , pt3];<BR> }<BR> }<BR> }<BR> //The fourth type, the two points are not on a straight line.<BR> //先纵向扫描可能的路径<BR> //同样,每次构造4个顶点,看是否可通<BR> for(var k=0; k<Y; k++){<BR> pt0 = p1;<BR> pt1 = {x:p1.x, y:k};<BR> pt2 = {x:p2.x, y:k};<BR> pt3 = p2;<BR> status = '(x:' + pt0.x + ',y:' + pt0.y + ')' + ', (x:' + pt1.x + ',y:' + pt1.y + ')' + ', (x:' + pt2.x + ',y:' + pt2.y + ')' + ', (x:' + pt3.x + ',y:' + pt3.y + ')';<BR> //特殊情况,如果pt0和pt1重合<BR> if(equal(pt0,pt1)){<BR> //如果pt2不为空,则此路不通<BR> if(!isEmpty(pt2)){<BR> continue;<BR> }<BR> if( hasLine(pt1, pt2) && hasLine(pt2, pt3) ){<BR> return [pt1, pt2, pt3];<BR> }<BR> else{<BR> continue;<BR> }<BR> }<BR> //特殊情况,如果pt2和pt3重合<BR> else if(equal(pt2,pt3)){<BR> //如果pt1不为空,则此路不通<BR> if(!isEmpty(pt1)){<BR> continue;<BR> }<BR> if( hasLine(pt0, pt1) && hasLine(pt1, pt2) ){<BR> return [pt0, pt1, pt2];<BR> }<BR> else{<BR> continue;<BR> }<BR> }<BR> //如果pt1, pt2都不为空,则不通<BR> if(!isEmpty(pt1) || !isEmpty(pt2)){<BR> continue;<BR> }<BR> if( hasLine(pt0, pt1) && hasLine(pt1, pt2) && hasLine(pt2, pt3) ){<BR> return [pt0, pt1, pt2, pt3];<BR> }<BR> }<BR> //横向扫描可能的路径<BR> for(var k=0; k<X; k++){<BR> pt0 = p1;<BR> pt1 = {x:k, y:p1.y};<BR> pt2 = {x:k, y:p2.y};<BR> pt3 = p2;<BR> status = '(x:' + pt0.x + ',y:' + pt0.y + ')' + ', (x:' + pt1.x + ',y:' + pt1.y + ')' + ', (x:' + pt2.x + ',y:' + pt2.y + ')' + ', (x:' + pt3.x + ',y:' + pt3.y + ')';<BR> if(equal(pt0,pt1)){<BR> if(!isEmpty(pt2)){<BR> continue;<BR> }<BR> if( hasLine(pt1, pt2) && hasLine(pt2, pt3) ){<BR> return [pt1, pt2, pt3];<BR> }<BR> }<BR> if(equal(pt2,pt3)){<BR> if(!isEmpty(pt1)){<BR> continue;<BR> }<BR> if( hasLine(pt0, pt1) && hasLine(pt1, pt2) ){<BR> return [pt0, pt1, pt2];<BR> }<BR> }<BR> if(!isEmpty(pt1) || !isEmpty(pt2)){<BR> continue;<BR> }<BR> if( hasLine(pt0, pt1) && hasLine(pt1, pt2) && hasLine(pt2, pt3) ){<BR> return [pt0, pt1, pt2, pt3];<BR> }<BR> }<BR> //status='type4';<BR> return null;<BR> /********** end type 4 **************/<BR>} <P>function equal(p1, p2){<BR> return ((p1.x==p2.x)&&(p1.y==p2.y));<BR>} <P>function onlineX(p1, p2){<BR> return p1.y==p2.y;<BR>} <P>function onlineY(p1, p2){<BR> return p1.x==p2.x; <BR>} <P>function isEmpty(p){<BR> return (arr[p.y][p.x]==0); <BR>} <P>function hasLine(p1, p2){<BR> if(p1.x==p2.x&&p1.y==p2.y){<BR> return true; <BR> }<BR> if(onlineY(p1, p2)){<BR> var i = p1.y>p2.y?p2.y:p1.y;<BR> i = i+1;<BR> var max = p1.y>p2.y?p1.y:p2.y;<BR> for(; i<max; i++){<BR> var p = {x: p1.x, y: i};<BR> if(!isEmpty(p)){<BR> break<BR> }<BR> }<BR> if(i==max){<BR> return true;<BR> }<BR> return false;<BR> }<BR> else if(onlineX(p1, p2)){<BR> var j = p1.x>p2.x?p2.x:p1.x;<BR> j = j+1;<BR> var max = p1.x>p2.x?p1.x:p2.x;<BR> for(; j<max; j++){<BR> var p = {x: j, y: p1.y};<BR> if(!isEmpty(p)){<BR> break<BR> }<BR> }<BR> if(j==max){<BR> return true;<BR> }<BR> return false;<BR> }<BR>}<BR>//以下部分为表现层部分,包括绘图, 初始化矩阵, 绑定鼠标事件...<BR>function $(id){return document.getElementById(id)}<P>var t1, t2;//For testing<BR>//Image base path<BR>var IMG_PATH = 'http://www.jb51.net';<BR>//Initialization<BR>function init( ){<BR> //Construct image library<BR> var imgs = new Array(30);<BR> for(var i=1; i<=30; i ){<BR> imgs[i] = 'r_ ' i '.gif';<BR> }<BR> tbl = $('tbl');<BR> //Construct table<BR> for(var row=0;row<Y-2;row ){<BR> var tr=tbl.insertRow(-1);<BR> for(var col=0;col<X-2;col ) {<BR> var td=tr.insertCell(-1);<BR> } <BR> }<BR> //Construct matrix <BR> for(var i=0; i<Y; i ){<BR> arr[i] = new Array(X);<BR> for(var j= 0; j<X; j ){<BR> arr[i][j] = 0;<BR> }<BR> }<BR> var total = (X-2)*(Y-2);<BR> var tmp = new Array(total);//Generate random positions using <BR> for (var i=0; i<total; i ){<BR> tmp[i] = 0;<BR> }<BR> for(var i=0; i<total; i ){<BR> if(tmp[i]==0){<BR> var t = Math.floor(Math.random()*types) 1;<BR> tmp[i] = t;<BR> while(true){<BR> var c = Math.floor(Math.random()*(total-i)) i;<BR> if(tmp[c]= =0){<BR> tmp[c] = t;<BR> break;<BR> }<BR> }<BR> }<BR> }<BR> var c = 0;<BR> for(var i =1; i<Y-1; i ){<BR> for(var j=1; j<X-1; j ){<BR> arr[i][j] = tmp[c ];<BR> tbl.rows[i-1].cells[j-1].innerHTML = '<img src="' IMG_PATH imgs[arr[i][j]] '" / alt="JS Lianliankan source code perfect annotated version (recommended)_javascript skills" >';<BR> } <BR> }<BR> //Bind mouse events<BR> var img1, img2;<BR> document.body.onclick = function(e){<BR> var el = document.all?event.srcElement:e.target ;<BR> if(el.parentNode.tagName!='TD'){<BR> return;<BR> }<BR> if(!img1){<BR> img1 = el;<BR> }<BR> else{<BR> img2 = el;<BR> }<BR> el.style.border = 'solid #3399FF 3px';<BR> el = el.parentNode;<BR> if(el.innerHTML=='' ){<BR> p1 = p2 = e1 = e2 = null;<BR> }<BR> var r = el.parentNode.rowIndex 1;<BR> var c = el.cellIndex 1;<BR> if(p1= =null){<BR> //el.childNodes[0].style.border = 'solid #ccc 3px';<BR> p1 = {x:c, y:r};<BR> e1 = el;<BR> }<BR> else{<BR> p2 = {x:c, y:r};<BR> e2 = el;<BR> if(!equal(p1, p2)&&e1.innerHTML==el.innerHTML ){<BR> var path = getPath(p1, p2);<BR> if(path!=null){<BR> e1.innerHTML = e2.innerHTML = '';<BR> arr[p1.y][ p1.x] = arr[p2.y][p2.x] = 0;<BR> }<BR> }<BR> if(t1){t1.style.backgroundColor = '';}<BR> t1 = e1;<BR> if(t2){t2.style.backgroundColor = '';}<BR> t2 = e2;<BR> img1.style.border = 'solid #fff 3px';<BR> img2.style. border = 'solid #fff 3px';<BR> p1 = p2 = e1 = e2 = img1 = img2 = null;<BR> t1.style.backgroundColor = t2.style.backgroundColor = 'lightpink';<BR> }<BR> }<BR>}<BR></script>
js Lianliankan perfect annotated version
< ;table id="tbl" cellspacing="0" cellpadding="0" border="1">

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software