<div id="wrapper">
<table width="100%" cellspacing="0px">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
*{
margin:0;
padding:0;
}
table{
width:700px;
margin-left:400px;
margin-top:10px;
}
td {
border:1px solid black;
background-color:silver;
float:left;
width:70px;
height:70px;
}
.bounceY {
background-color:orange;
}
.bounceB{
background-color:yellow;
}
.bounceG{
background-color:red;
}
.bounceR{
background-color:green;
}
bounceArray = [];
var colorArray = ["B", "Y", "G", "R"] ;
var cellArray=[];
var b = [[]];
var f = $('tr');
var s = [[]];
var fLength = f.length;
for(var i = 0; i < fLength; i += 1){
var a = $(f[i]).children('td');
var aLength = a.length;
if(typeof cellArray[i] == 'undefined'){
cellArray[i] = [];
bounceArray[i] = [];
}
for(var j = 0; j < aLength; j++)
{
cellArray[i][j] = a[j];
bounceArray[i][j] = 0;
}
}
var bounce = function (){
for(var z=0;z<3;z++){
cellRand = Math.floor( Math.random() * f.length);
rowRand = Math.floor( Math.random() * a.length);
colorRand = Math.floor( Math.random() * colorArray.length);
$(cellArray[cellRand][rowRand]).not(".bounceY, .bounceB, .bounceG, .bounceR").attr("class", "bounce"+colorArray[colorRand]);
bounceArray[cellRand][rowRand] = -1;
positionRand = new Array(cellRand,rowRand);
}
for(var i = 0; i < fLength; i += 1){
for(var j = 0; j < aLength; j += 1)
{
$(cellArray[i][j]).text(bounceArray[i][j]);
}}
addBounce();
}
var activeBounce;
function addBounce(){
$(".bounceY, .bounceB, .bounceG, .bounceR").unbind( "click");
$(".bounceY, .bounceB, .bounceG, .bounceR").bind("click", function (){
activeBounce = $(this);
positionEmpty = new Array($(this).parent().index(),$(this).index());
bounceColor = $(this).attr('class');
removeBounce();
})
}
function removeBounce(){
$("td").not(".bounceY, .bounceB, .bounceG, .bounceR").unbind();
$("td").not(".bounceY, .bounceB, .bounceG, .bounceR").bind("click", function(){
emptyBounce = $(this);
mat = [[[]]];
glob=[[]];
var rows = parseInt($(activeBounce).parent().index());
var colls = parseInt($(activeBounce).index());
count = 0;
endRow = $(emptyBounce).parent().index();
endColl = $(emptyBounce).index();
glob[0][0] = rows;
glob[0][1] = colls;
sortNumber(glob);
$(activeBounce).removeAttr("class");
bounceArray[$(activeBounce).parent().index()][$(activeBounce).index()] = 0;
$(this).addClass(bounceColor);
bounceArray[$(this).parent().index()][$(this).index()] = -1;
$('td').not(".bounceY, .bounceB, .bounceG, .bounceR").unbind('click');
addBounce();
bounce();
})
}
function sortNumber(m){
var t = false;
count++;
var j = 0;
var b = [[]];
for(var i=0;i<m.length;i++){
if(m[i][0] + 1 <= 8 && bounceArray[m[i][0] + 1][m[i][1]] == 0){
if((m[i][0]+1 == endRow) && (m[i][1] == endColl)){
return false;
}
bounceArray[m[i][0] + 1][m[i][1]] = count;
if(typeof b[j] == 'undefined'){
b[j] = [];
}
b[j][0] = m[i][0] + 1;
b[j][1] = m[i][1];
t=true;
j++;
}
if(m[i][1] - 1>= 0 && bounceArray[m[i][0]][m[i][1] - 1] == 0){
if((m[i][0] == endRow) && (m[i][1]-1 == endColl)){
return false;
}
bounceArray[m[i][0]][m[i][1] - 1] = count;
if(typeof b[j] == 'undefined'){
b[j] = [];
}
b[j][0] = m[i][0];
b[j][1] = m[i][1] - 1;
j++;
t = true;
}
if(m[i][0] - 1 >= 0 && bounceArray[m[i][0] - 1][m[i][1]] == 0){
if((m[i][0]-1 == endRow) && (m[i][1] == endColl)){
return false;
}
bounceArray[m[i][0] - 1][m[i][1]] = count;
if(typeof b[j] == 'undefined'){
b[j] = [];
}
b[j][0] = (m[i][0] - 1);
b[j][1] = m[i][1];
t=true;
j++;
}
if(m[i][1] + 1 <= 8 && bounceArray[m[i][0]][m[i][1] + 1] == 0){
if((m[i][0] == endRow) && (m[i][1]+1 == endColl)){
return false;
}
bounceArray[m[i][0]][m[i][1] + 1] = count;
if(typeof b[j] == 'undefined'){
b[j] = [];
}
b[j][0] = m[i][0];
b[j][1] = m[i][1] + 1;
t=true;
j++;
}
}
if(t){
sortNumber(b);
}
else {
return false;
}
trip(glob);
}
var h=0;
function trip(n){
var f = false;
var i = n.length - 1;
var j = n.length;
if(n[j- 1][0] == endRow && n[j - 1][1] == endColl){
alert('End point');
for(k=0; k<=i; k++){
mat[h][k][0]=n[k][0];
mat[h][k][1]=n[k][1];
}
h++;
return false;
}
if(n[i][0] + 1 <= 8 && bounceArray[n[i][0] + 1][n[i][1]] == j){
f = true;
n.push([n[i][0] + 1, n[i][1]]);
trip(n);
}
if(n[i][1] - 1>= 0 && bounceArray[n[i][0]][n[i][1] - 1] == j){
f = true;
n.push([n[i][0], n[i][1] - 1]);
trip(n);
}
if(n[i][0] - 1 >= 0 && bounceArray[n[i][0] - 1][n[i][1]] == j){
f = true;
n.push([n[i][0] - 1, n[i][1]]);
trip(n);
}
if(n[i][1] + 1 <= 8 && bounceArray[n[i][0]][n[i][1] + 1] == j){
f = true;
n.push([n[i][0], n[i][1] + 1]);
trip(n);
}
if(!f)
return false;
}
window.onload= bounce;
реализация игры lines надо найти кротчайший путь и написать функцию проверки нахождения 5 шариков рядом... Я новичок не судите строго только учусь