var Piece = function(game) {
// Game object
= game;
// Is it an edge element?
= false;
// Whether to be next to the edge element
= false;
// Picture dom element
= null;
// Image elements
= null;
// Source of image elements
= null;
// Track elements
= null;
// Is it possible to be used as a track
= false;
// Select the marker element
= null;
// Horizontal arrangement of pictures
= 0;
//Picture vertical arrangement
= 0;
// The picture is flashing Id
= null;
// Whether to click on the picture
= false;
// Number of flashes
= 0;
();
}
= {
// Initialization
init : function() {
= ("div");
= "piece";
= ("img");
},
// Initialize the picture
initImg : function() {
= ("img");
();
},
// Initialize the track element after satisfying the algorithm
initTrack : function() {
if ( != null) {
// Stop flashing
();
}
//alert("initTrack middle");
if ( != null) {
return;
}
= false;
();
= ("div");
= "track";
();
},
//Bit image setting source
setImgSrc : function(src) {
= src;
},
// Set two-dimensional arrangement position for the picture
setPosition : function(x, y) {
= x;
= y;
},
// Set selected elements for the image
setSelected : function() {
if ( ++ % 2 == 0) {
//();
// = "img/";
//();
= "img/pieces/";
} else {
//if ( != null) {
// ();
//}
= ;
//();
}
},
// Set whether it is an edge element
setEdge : function(isEdge) {
= isEdge;
},
// Set whether to be next to the edge element
setAtEdge : function(atEdge) {
= atEdge;
},
// Start flashing
flash : function() {
var _this = this;
= setInterval(function() {_this.setSelected();}, 500);
},
// Stop flashing
stopFlash : function() {
clearInterval();
if ( % 2 == 1) {
//if ( != null) {
// ();
//}
= ;
//();
}
},
// The internal function of the object being selected
onClick : function() {
if () {
return;
}
var _this = this;
= function() {
if (!("start").disabled) {
return;
}
if (_this.onClicked) {
return;
}
if (_this.checkPiece()) {
return;
}
_this.flash();
_this.onClicked = true;
};
},
// Check if there are any pictures that are clicked
checkPiece : function() {
for (var i = 0; i < ; i ++) {
if ([i].onClicked && ![i].equal(this)) {
if ([i].equalImage(this)) {
//alert("The same Image");
([i]);
} else {
[i].stopFlash();
[i].onClicked = false;
= false;
return false;
}
return true;
} else {
continue;
}
}
return false;
},
// Whether it is the same object
equal : function(piece) {
return ( == && == );
},
// Is it the same picture
equalImage : function(piece) {
return == ;
},
// Search for paths
searchTrack : function(piece) {
if ((piece)) {
(piece);
return;
}
if ((piece) || this.isReach2(piece)) {
(piece);
return;
}
},
// Whether it is adjacent
isNear : function(piece) {
var a = (( - ) == 1) && ( == )
|| (( - ) == 1) && ( == );
return a;
},
// Straight line
isStraightReach : function(piece) {
//alert("isStraightReach");
if ((piece)) {
return true;
}
var a = false;
var b = false;
// Search along the y-axis direction
if ( == ) {
//alert("!!!!!!!!!!!");
for (var i = (, ) + 1; i < (, ); i ++) {
//alert(" == : " + + "," + i);
if (( + "," + i).isPass()) {
a = true;
(( + "," + i));
continue;
} else {
a = false;
= [];
return a;
}
}
}
// Search along the x-axis direction
if ( == ) {
//alert("!!!!!!!!!!!");
for (var i = (, ) + 1; i < (, ); i ++) {
//alert(" == : " + i + "," + );
if ((i + "," + ).isPass()) {
b = true;
((i + "," + ));
continue;
} else {
b = false
= [];
return b;
}
}
}
return a || b;
},
// Turn a corner and search
isReach1 : function(piece) {
//alert("isReach1");
var corner_1 = ( + "," + );
var corner_2 = ( + "," + );
var _this = this;
if ((_this.isStraightReach(corner_1))
&& (corner_1.isStraightReach(piece))
&& corner_1.isPass()) {
//alert("corner_1: " + + "," + );
(corner_1);
return true;
}
if ((_this.isStraightReach(corner_2))
&& (corner_2.isStraightReach(piece))
&& corner_2.isPass()) {
//alert("corner_2: " + + "," + );
(corner_2);
return true;
}
return false;
},
// Search directly or turn a corner
isReach : function(piece) {
var a = (piece);
var b = this.isReach1(piece);
return a || b;
},
// Two turns search
isReach2 : function(piece) {
// Forward search along the x-axis
for (var i = + 1; i < 17; i ++) {
if (!(i + "," + ).isPass()) {
= [];
break;
} else if ((i + "," + ).isReach(piece)
&& (i + "," + ).isPass()) {
((i + "," + ));
return true;
}
}
// Search along the x-axis
for (var i = - 1; i >= 0; i --) {
if (!(i + "," + ).isPass()) {
= [];
break;
} else if ((i + "," + ).isReach(piece)
&& (i + "," + ).isPass()) {
((i + "," + ));
return true;
}
}
// Search along the y-axis
for (var i = - 1; i >= 0; i --) {
if (!( + "," + i).isPass()) {
= [];
break;
} else if (( + "," + i).isReach(piece)
&& ( + "," + i).isPass()) {
(( + "," + i));
return true;
}
}
// Forward search along the y-axis
for (var i = + 1; i < 12; i ++) {
if (!( + "," + i).isPass()) {
= [];
break;
} else if (( + "," + i).isReach(piece)
&& ( + "," + i).isPass()) {
(( + "," + i));
return true;
}
}
return false;
},
// Path connection
linkTrack : function(piece) {
();
();
();
(piece);
},
// Show footprints
showTrack : function(piece) {
(piece);
= "track2";
for (var i = 0; i < ; i ++) {
//alert(i);
[i]. = "track2";
}
var _this = this;
setTimeout(function() {_this.hideTrack()}, 500);
},
// Hidden footprints
hideTrack : function() {
for (var i = 0; i < ; i ++) {
[i]. = "track";
}
= [];
= "track";
= true;
},
// Increase score
changeScore : function() {
+= 100;
("score").innerHTML = ;
},
min : function(a, b) {
if (a < b) {
return a;
} else {
return b;
}
},
max : function(a, b) {
if (a > b) {
return a;
} else {
return b;
}
},
// Determine whether it is passed
isPass : function() {
return != null;
}
}