Native JavaScript implementation of lianliankan game of source code

  • 2020-03-26 23:03:09
  • OfStack

Recommend a native JavaScript version lianliankan game, (link: http://xiazai.jb51.net/201311/yuanma/linkgame (jb51.net). Rar), home page as shown in the figure below:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/201311051543191.gif? 2013105154346 ">  
First, let's look at the layout of the HTML in the index.html file:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<title> lianliankan </title> 
<link rel="stylesheet" type="text/css" href="css/index.css"/> 
</head> 
<body> 
<center> 
<div id="whole"> 
<div id="gamePanel" tabindex="0"> 
<div id="pieces"> 
</div> 
</div> 
<div id="gameLogo"> 
</div> 
<div id="scorePanel"> 
<span> points   The number </span> 
</div> 
<div id="score"> 
<span>0</span> 
</div> 
<div id="timePanel"> 
<span> when   between </span> 
</div> 
<div id="time"> 
<span>0</span> 
</div> 
<div id="button"> 
<input id="start" type="button" onclick="Start();" value=" start "></input> 
<input id="reset" type="button" onclick="Reset();"value=" reset "></input> 
</div> 
</div> 
</center> 
<script type="text/javascript" src="js/map.js"></script> 
<script type="text/javascript" src="js/piece.js"></script> 
<script type="text/javascript" src="js/game.js"></script> 
</body> 
</html> 

The index.css file under the CSS folder is as follows:
 
body { 
font-size : 16px; 
font-weight : bold; 
color : grey; 
} 
#whole { 
border : 1px double #999999; 
border-width : 5px; 
width : 800px; 
height : 505px; 
position : relative; 
} 
#gamePanel { 
margin: 1px 1px 1px 1px; 
width : 602px; 
height : 502px; 
background : url(../img/background.gif) repeat; 
position : absolute; 
} 
#pieces { 
margin-top : 35px; 
border : 1px solid #999999; 
width : 546px; 
height : 434px; 
position: relative; 
} 
#pieces .piece { 
width : 32px; 
height : 36px; 
position : relative; 
cursor : pointer; 
float : left; 
} 
#pieces .track { 
width : 32px; 
height : 36px; 
position : relative; 
float : left; 
} 
#pieces .track2 { 
width : 32px; 
height : 36px; 
position : relative; 
float : left; 
background : red; 
} 
#gameLogo { 
margin-top : 60px; 
border : 1px solid #999999; 
left : 607px; 
width : 187px; 
height : 73px; 
background : url(../img/logo.gif); 
position: absolute; 
} 
#scorePanel { 
border : 1px solid #999999; 
left : 607px; 
top : 200px; 
width : 187px; 
height : 30px; 
position : absolute; 
} 
#score { 
border : 1px solid #999999; 
left : 607px; 
top : 240px; 
width : 187px; 
height : 30px; 
position : absolute; 
} 
#timePanel { 
border : 1px solid #999999; 
left : 607px; 
top : 300px; 
width : 187px; 
height : 30px; 
position : absolute; 
} 
#time { 
border : 1px solid #999999; 
left : 607px; 
top : 340px; 
width : 187px; 
height : 30px; 
position : absolute; 
} 
#button { 
border : 1px solid #999999; 
left : 607px; 
top : 400px; 
width : 187px; 
height : 30px; 
position : absolute; 
} 

Next, let's take a look at the core js implementation code. The js part is divided into three source files, namely, game.js, map.js and piece.js. Each source file corresponds to a class.
The game.js code is as follows:
 
//Game control
var Game = { 
//Game background
gamePanel : null, 
//  score  
score : 0, 
//  time  
time : 0, 
//Picture map
pieceMap : null, 
//Image list
pieceList : [], 
//The pictures list does not contain pictures
pieceImgList : [], 
//Image random number list
randomList : [], 
//Track list
trackList : [], 
//Game start or not
isGameBigin : false, 
//Is the game over
isGameOver : false, 
//Whether the game is reset or not
isGameReset : false, 
//Whether the image element is clicked for the first time
isFirstClick : true, 
//Start the game
start : function() { 
document.getElementById("start").disabled = true; 
document.getElementById("reset").disabled = false; 
if (this.isGameReset) { 
this.isGameOver = false; 
this.startTime(); 
return; 
} else if (this.isGameBegin) { 
return; 
} else { 
this.init(); 
return; 
} 
}, 
reset : function() { 
document.getElementById("start").disabled = false; 
document.getElementById("reset").disabled = true; 
this.clear(); 
this.initPieces(); 
this.initImgPieces(); 
this.time = 0; 
document.getElementById("time").innerHTML = 0; 
this.score = 0; 
document.getElementById("score").innerHTML = 0; 
this.isGameReset = true; 
this.isGameBegin = true; 
}, 
//Initialize the
init : function() { 
if (this.isGameBegin) { 
return; 
} 
this.pieceMap = new Map(); 
var _this = this; 
this.time = 0; 
this.startTime(); 
this.gamePanel = document.getElementById("pieces"); 
this.initPieces(); 
this.initImgPieces(); 
this.isGameBegin = true; 
}, 
//Add 150 randomly generated images to the canvas
initPieces : function() { 
var _this = this; 
this.initRandomList(); 
//Shuffle the random list sort
this.messRandomList(); 
for (var i = 0; i < 204; i ++) { 
var piece = new Piece(this); 
this.pieceList.push(piece); 
var x = (i%17); 
var y = Math.floor(i/17); 
this.pieceMap.put(x+","+y, piece); 
piece.setPosition(x, y); 
this.gamePanel.appendChild(piece.dom); 
if (x == 0 || x == 16 || y == 0 || y == 11) { 
piece.track = document.createElement("div"); 
piece.track.className = "track"; 
piece.dom.appendChild(piece.track); 
piece.isTracked = true; 
continue; 
} else { 
if (x == 1 || x == 15 || y == 1 || y == 10) { 
piece.setAtEdge(true); 
} 
this.pieceImgList.push(piece); 
} 
} 
}, 
//Initialization picture
initImgPieces : function() { 
for (var i = 0; i < this.pieceImgList.length; i ++) { 
this.pieceImgList[i].initImg(); 
this.pieceImgList[i].img.src = "img/pieces/"+this.randomList[i]+".gif" 
this.pieceImgList[i].setImgSrc(this.pieceImgList[i].img.src); 
//Execute the image click event
this.pieceImgList[i].onClick(); 
} 
}, 
//Initializes a random table
initRandomList : function() { 
//Get a random sequence of Numbers that appear in pairs
for (var i = 0; i < 75; i ++) { 
var random = parseInt(Math.random()*22*10000, 10); 
var number = random%23; 
this.randomList.push(number); 
this.randomList.push(number); 
} 
}, 
//Scrambling table
messRandomList : function() { 
for (var i = 0; i < this.randomList.length; i ++) { 
var random = parseInt(Math.random()*15*10000, 10); 
var number = random%150; 
var temp; 
temp = this.randomList[i]; 
this.randomList[i] = this.randomList[number]; 
this.randomList[number] = temp; 
} 
}, 
//Start the time
startTime : function() { 
var _this = this; 
if (this.isGameOver) { 
return; 
} else { 
this.time ++; 
document.getElementById("time").innerHTML = this.time; 
this.isGameBegin = true; 
setTimeout(function() {_this.startTime();}, 1000); 
} 
}, 
//  remove  
clear : function() { 
for (var i = 0; i < this.pieceList.length; i ++) { 
this.gamePanel.removeChild(this.pieceList[i].dom); 
} 
this.pieceList = []; 
this.randomList = []; 
this.pieceImgList = []; 
this.isGameOver = true; 
this.isGameBegin = false; 
} 
} 
window.onload = function() { 
document.getElementById("start").disabled = false; 
document.getElementById("reset").disabled = true; 
} 
//Game start entry
function Start() { 
Game.start(); 
} 
//Game reset entry
function Reset() { 
Game.reset(); 
} 

The custom js version of the mapping structure map.js source file is as follows:
 
var Map = function(){ 
this.data = []; 
} 
Map.prototype = { 
put : function(key, value) { 
this.data[key] = value; 
}, 
get : function(key) { 
return this.data[key]; 
}, 
remove : function(key) { 
this.data[key] = null; 
}, 
isEmpty : function() { 
return this.data.length == 0; 
}, 
size : function() { 
return this.data.length; 
} 
} 

The picture class piece.js source file is as follows:
 
var Piece = function(game) { 
//The game object
this.game = game; 
//Is an edge element
this.isEdge = false; 
//Whether it's next to an edge element
this.atEdge = false; 
//Image dom element
this.dom = null; 
//Image element
this.img = null; 
//Source of image elements
this.src = null; 
//Trajectory elements
this.track = null; 
//Whether it can be a trajectory
this.isTracked = false; 
//Select the tag element
this.selected = null; 
//Horizontal arrangement of pictures
this.x = 0; 
//Vertical arrangement of pictures
this.y = 0; 
//Picture flashing Id
this.flashId = null; 
//Whether the picture is clicked or not
this.onClicked = false; 
//Flicker frequency
this.flashCount = 0; 
this.init(); 
} 
Piece.prototype = { 
//Initialize the
init : function() { 
this.dom = document.createElement("div"); 
this.dom.className = "piece"; 
this.selected = document.createElement("img"); 
}, 
//Initialization picture
initImg : function() { 
this.img = document.createElement("img"); 
this.dom.appendChild(this.img); 
}, 
//Initialize the track element after satisfying the algorithm
initTrack : function() { 
if (this.flashId != null) { 
//Stop flashing
this.stopFlash(); 
} 
//alert("initTrack middle"); 
if (this.track != null) { 
return; 
} 
this.onClicked = false; 
this.dom.removeChild(this.img); 
this.track = document.createElement("div"); 
this.track.className = "track"; 
this.dom.appendChild(this.track); 
}, 
//Bit image Settings source
setImgSrc : function(src) { 
this.src = src; 
}, 
//Set the two-dimensional arrangement position for the picture
setPosition : function(x, y) { 
this.x = x; 
this.y = y; 
}, 
//Set the selected element for the image
setSelected : function() { 
if (this.flashCount ++ % 2 == 0) { 
//this.dom.removeChild(this.img); 
//this.selected.src = "img/selected.gif"; 
//this.dom.appendChild(this.selected); 
this.img.src = "img/pieces/flash.gif"; 
} else { 
//if (this.selected != null) { 
// this.dom.removeChild(this.selected); 
//} 
this.img.src = this.src; 
//this.dom.appendChild(this.img); 
} 
}, 
//Sets whether to be an edge element
setEdge : function(isEdge) { 
this.isEdge = isEdge; 
}, 
//Sets whether the edge element is next to it
setAtEdge : function(atEdge) { 
this.atEdge = atEdge; 
}, 
//Start flashing
flash : function() { 
var _this = this; 
this.flashId = setInterval(function() {_this.setSelected();}, 500); 
}, 
//Stop flashing
stopFlash : function() { 
clearInterval(this.flashId); 
if (this.flashCount % 2 == 1) { 
//if (this.selected != null) { 
// this.dom.removeChild(this.selected); 
//} 
this.img.src = this.src; 
//this.dom.appendChild(this.img); 
} 
}, 
//The inner function of the selected object
onClick : function() { 
if (this.onClicked) { 
return; 
} 
var _this = this; 
this.img.onclick = function() { 
if (!document.getElementById("start").disabled) { 
return; 
} 
if (_this.onClicked) { 
return; 
} 
if (_this.checkPiece()) { 
return; 
} 
_this.flash(); 
_this.onClicked = true; 
}; 
}, 
//Check if there are any pictures that have been clicked
checkPiece : function() { 
for (var i = 0; i < this.game.pieceList.length; i ++) { 
if (this.game.pieceList[i].onClicked && !this.game.pieceList[i].equal(this)) { 
if (this.game.pieceList[i].equalImage(this)) { 
//alert("The same Image"); 
this.searchTrack(this.game.pieceList[i]); 
} else { 
this.game.pieceList[i].stopFlash(); 
this.game.pieceList[i].onClicked = false; 
this.onClicked = false; 
return false; 
} 
return true; 
} else { 
continue; 
} 
} 
return false; 
}, 
//Whether it is the same object
equal : function(piece) { 
return (this.x == piece.x && this.y == piece.y); 
}, 
//Whether for the same picture
equalImage : function(piece) { 
return this.src == piece.src; 
}, 
//Search path
searchTrack : function(piece) { 
if (this.isNear(piece)) { 
this.linkTrack(piece); 
return; 
} 
if (this.isReach(piece) || this.isReach2(piece)) { 
this.linkTrack(piece); 
return; 
} 
}, 
//Whether the adjacent
isNear : function(piece) { 
var a = (Math.abs(piece.x - this.x) == 1) && (piece.y == this.y) 
|| (Math.abs(piece.y - this.y) == 1) && (piece.x == this.x); 
return a; 
}, 
//  A straight line  
isStraightReach : function(piece) { 
//alert("isStraightReach"); 
if (this.isNear(piece)) { 
return true; 
} 
var a = false; 
var b = false; 
//Search along the Y-axis
if (this.x == piece.x) { 
//alert("!!!!!!!!!!!"); 
for (var i = this.min(this.y, piece.y) + 1; i < this.max(this.y, piece.y); i ++) { 
//alert("this.x == piece.x: " + piece.x + "," + i); 
if (this.game.pieceMap.get(piece.x + "," + i).isPass()) { 
a = true; 
this.game.trackList.push(this.game.pieceMap.get(piece.x + "," + i)); 
continue; 
} else { 
a = false; 
this.game.trackList = []; 
return a; 
} 
} 
} 
//Search along the X-axis
if (this.y == piece.y) { 
//alert("!!!!!!!!!!!"); 
for (var i = this.min(this.x, piece.x) + 1; i < this.max(this.x, piece.x); i ++) { 
//alert("this.y == piece.y: " + i + "," + piece.y); 
if (this.game.pieceMap.get(i + "," + piece.y).isPass()) { 
b = true; 
this.game.trackList.push(this.game.pieceMap.get(i + "," + piece.y)); 
continue; 
} else { 
b = false 
this.game.trackList = []; 
return b; 
} 
} 
} 
return a || b; 
}, 
//Take a turn and search
isReach1 : function(piece) { 
//alert("isReach1"); 
var corner_1 = this.game.pieceMap.get(this.x + "," + piece.y); 
var corner_2 = this.game.pieceMap.get(piece.x + "," + this.y); 
var _this = this; 
if ((_this.isStraightReach(corner_1)) 
&& (corner_1.isStraightReach(piece)) 
&& corner_1.isPass()) { 
//alert("corner_1: " + this.x + "," + piece.y); 
this.game.trackList.push(corner_1); 
return true; 
} 
if ((_this.isStraightReach(corner_2)) 
&& (corner_2.isStraightReach(piece)) 
&& corner_2.isPass()) { 
//alert("corner_2: " + piece.x + "," + this.y); 
this.game.trackList.push(corner_2); 
return true; 
} 
return false; 
}, 
//Search directly or take a turn
isReach : function(piece) { 
var a = this.isStraightReach(piece); 
var b = this.isReach1(piece); 
return a || b; 
}, 
//Take two turns to search
isReach2 : function(piece) { 
//Search along the X-axis
for (var i = this.x + 1; i < 17; i ++) { 
if (!this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece) 
&& this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y)); 
return true; 
} 
} 
//Search along the X-axis
for (var i = this.x - 1; i >= 0; i --) { 
if (!this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece) 
&& this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y)); 
return true; 
} 
} 
//Search along the Y-axis
for (var i = this.y - 1; i >= 0; i --) { 
if (!this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece) 
&& this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i)); 
return true; 
} 
} 
//Search forward along the Y-axis
for (var i = this.y + 1; i < 12; i ++) { 
if (!this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece) 
&& this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i)); 
return true; 
} 
} 
return false; 
}, 
//Path connecting
linkTrack : function(piece) { 
this.initTrack(); 
piece.initTrack(); 
this.changeScore(); 
this.showTrack(piece); 
}, 
//Show footprint
showTrack : function(piece) { 
this.game.trackList.push(piece); 
this.track.className = "track2"; 
for (var i = 0; i < this.game.trackList.length; i ++) { 
//alert(i); 
this.game.trackList[i].track.className = "track2"; 
} 
var _this = this; 
setTimeout(function() {_this.hideTrack()}, 500); 
}, 
//The hidden tracks
hideTrack : function() { 
for (var i = 0; i < this.game.trackList.length; i ++) { 
this.game.trackList[i].track.className = "track"; 
} 
this.game.trackList = []; 
this.track.className = "track"; 
this.isTracked = true; 
}, 
//Scores increase
changeScore : function() { 
this.game.score += 100; 
document.getElementById("score").innerHTML = this.game.score; 
}, 
min : function(a, b) { 
if (a < b) { 
return a; 
} else { 
return b; 
} 
}, 
max : function(a, b) { 
if (a > b) { 
return a; 
} else { 
return b; 
} 
}, 
//Pass judgment
isPass : function() { 
return this.track != null; 
} 
} 

Above is the source file code, the specific implementation code please pay attention to the download of zhangjinpeng66 in CSDN. Let's talk about the most core part of lianliankan game, js search path.
Js implementation of the search path algorithm is the first and most simple is to determine whether the two images can reach the function code as follows:
 
//  A straight line  
isStraightReach : function(piece) { 
//alert("isStraightReach"); 
if (this.isNear(piece)) { 
return true; 
} 
var a = false; 
var b = false; 
//Search along the Y-axis
if (this.x == piece.x) { 
//alert("!!!!!!!!!!!"); 
for (var i = this.min(this.y, piece.y) + 1; i < this.max(this.y, piece.y); i ++) { 
//alert("this.x == piece.x: " + piece.x + "," + i); 
if (this.game.pieceMap.get(piece.x + "," + i).isPass()) { 
a = true; 
this.game.trackList.push(this.game.pieceMap.get(piece.x + "," + i)); 
continue; 
} else { 
a = false; 
this.game.trackList = []; 
return a; 
} 
} 
} 
//Search along the X-axis
if (this.y == piece.y) { 
//alert("!!!!!!!!!!!"); 
for (var i = this.min(this.x, piece.x) + 1; i < this.max(this.x, piece.x); i ++) { 
//alert("this.y == piece.y: " + i + "," + piece.y); 
if (this.game.pieceMap.get(i + "," + piece.y).isPass()) { 
b = true; 
this.game.trackList.push(this.game.pieceMap.get(i + "," + piece.y)); 
continue; 
} else { 
b = false 
this.game.trackList = []; 
return b; 
} 
} 
} 
return a || b; 
}, 

This function realizes the simplest step of lianliankan to determine whether two images meet the connection conditions, followed by a turn search.
 
//Take a turn and search
isReach1 : function(piece) { 
//alert("isReach1"); 
var corner_1 = this.game.pieceMap.get(this.x + "," + piece.y); 
var corner_2 = this.game.pieceMap.get(piece.x + "," + this.y); 
var _this = this; 
if ((_this.isStraightReach(corner_1)) 
&& (corner_1.isStraightReach(piece)) 
&& corner_1.isPass()) { 
//alert("corner_1: " + this.x + "," + piece.y); 
this.game.trackList.push(corner_1); 
return true; 
} 
if ((_this.isStraightReach(corner_2)) 
&& (corner_2.isStraightReach(piece)) 
&& corner_2.isPass()) { 
//alert("corner_2: " + piece.x + "," + this.y); 
this.game.trackList.push(corner_2); 
return true; 
} 
return false; 
}, 

The direct search function is called in the one turn search function, and the most complex two turns search also calls the one turn search function.
 
//Take two turns to search
isReach2 : function(piece) { 
//Search along the X-axis
for (var i = this.x + 1; i < 17; i ++) { 
if (!this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece) 
&& this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y)); 
return true; 
} 
} 
//Search along the X-axis
for (var i = this.x - 1; i >= 0; i --) { 
if (!this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece) 
&& this.game.pieceMap.get(i + "," + this.y).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y)); 
return true; 
} 
} 
//Search along the Y-axis
for (var i = this.y - 1; i >= 0; i --) { 
if (!this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece) 
&& this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i)); 
return true; 
} 
} 
//Search forward along the Y-axis
for (var i = this.y + 1; i < 12; i ++) { 
if (!this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList = []; 
break; 
} else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece) 
&& this.game.pieceMap.get(this.x + "," + i).isPass()) { 
this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i)); 
return true; 
} 
} 
return false; 
}, 

The function searches along the X-axis and the Y-axis, centered on the image being clicked.
The above is the full content of the game code. Please download the specific game source code of zhangjinpeng66 in CSDN.

Related articles: