Home Web Front-end JS Tutorial JS Lianliankan source code perfect annotated version (recommended)_javascript skills

JS Lianliankan source code perfect annotated version (recommended)_javascript skills

May 16, 2016 pm 05:10 PM
js

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.

Copy the code The code is as follows:




JS连连看源码完美注释版


<script><br>//以下部分为路径搜索算法部分,与表现层无关 <p>//全局变量<br>var X = 16;//总行数<br>var Y = 14;//总列数<br>var types = 15;//图形种类</p> <p>//布局矩阵<br>//为了算法方便,矩阵的第一行,第一列,最后一行,最后一列都标注为0,天然通路。<br>var arr = new Array(Y);<br>var tbl;//显示布局的table元素</p> <p>var p1 = null;//搜索路径用的第1个点的坐标<br>var p2 = null;//搜索路径用的第2个点的坐标<br>var e1 = null;//第1个点对应的元素<br>var e2 = null;//第2个点对应的元素</p> <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> <P>function equal(p1, p2){<BR> return ((p1.x==p2.x)&&(p1.y==p2.y));<BR>}</P> <P>function onlineX(p1, p2){<BR> return p1.y==p2.y;<BR>}</P> <P>function onlineY(p1, p2){<BR> return p1.x==p2.x; <BR>}</P> <P>function isEmpty(p){<BR> return (arr[p.y][p.x]==0); <BR>}</P> <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><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]] '" />';<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">



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

How to use JS and Baidu Maps to implement map heat map function How to use JS and Baidu Maps to implement map heat map function Nov 21, 2023 am 09:33 AM

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file

See all articles