• 连连看的原生JS实现V2


    对上一次的连连看程序进行了一点修改:

       1 var llk = function () {
       2             this.ReStart();
       3         }
       4         llk.prototype = {
       5             Init: function () {
       6                 //初始化游戏界面
       7                 for (var i = 0; i < 5; i++) {
       8                     this.Data.sort(function () { return 0.5 - Math.random() });
       9                 }
      10                 for (var i = 0; i < 140; i++) {
      11                     var shucai = this.Images[i];
      12                     shucai.className = this.className;
      13                     if(this.Data[i]!==null && this.Data[i]!=undefined){
      14                         shucai.innerText = this.Data[i];
      15                     }else{
      16                         this.removeItem(shucai);
      17                     }
      18                 }
      19                 this.showLevel();
      20                 this.showLife();
      21                 this.Start();
      22 
      23 
      24                 if (!this.HasNext()) {
      25                     if (!this.decreaseLife()) {
      26                         return;
      27                     }
      28                     this.Init();
      29                 }
      30             },
      31             InitData: function () {//随机生成游戏数据
      32                 if(this.leaveCount ==0 ){
      33                     var Data = [];
      34                     var level = Math.min(this.currentLevel - 1, 5);
      35                     for (var i = 0; i < 70; i++) {
      36                         var _d = Math.round(Math.random() * (10+level));
      37                         Data.push(_d);
      38                         Data.push(_d);
      39                     }
      40                     this.Data = Data;
      41                     this.leaveCount =140;
      42                 }
      43             },
      44             Select: function (item) {
      45                 //点击元素,选择元素
      46                 if (this.isRemoved(item))
      47                     return;
      48                 var from = this.getFrom();
      49                 if (from == null) {
      50                     
      51                     this.UnselectAll();
      52 
      53                     this.setFrom(item);
      54                     //shucai.className = this.selectClassName;
      55                     
      56                 } else if (from.target == item) {
      57                     return;
      58                 }
      59                 else {
      60                     this.setTo(item);
      61                     this.Check();
      62                 }
      63             },
      64             clearFrom: function () {//清楚起始点
      65                 var from = this.getFrom();
      66                 if (from!=null) {
      67                     this.removeClassName(from.target, this.selectClassName);
      68                 }
      69                 this.From = null;
      70                 if (this.To != null) {
      71                     this.From = this.To;
      72                     this.To = null;
      73                 }
      74             },
      75             clearTo: function () {//清楚终止点
      76                 if (this.To != null) {
      77                     this.removeClassName(this.To.target, this.selectClassName);
      78                 }
      79                 this.To = null;
      80             },
      81             getFrom: function () { return this.From;},
      82             setFrom: function (item) {
      83                 this.From = this.Parse(item);
      84                 this.addClassName(item, this.selectClassName);
      85             },
      86             getTo: function () { return this.To;},
      87             setTo: function (item) {
      88                 this.To = this.Parse(item);
      89                 this.addClassName(item, this.selectClassName);
      90             },
      91             getItem: function (x, y) {
      92                 if(y == null||y == undefined){
      93                     return this.Images[x];
      94                 }else{
      95                     return this.Images[y*14+x];
      96                 }
      97             },
      98             addClassName: function (item, className) {
      99                 if (!this.hasClassName(item, className)) {
     100                     item.className = item.className + " " + className;
     101                 }
     102             },
     103             hasClassName: function (item, className) {
     104                 if (item.className.indexOf(className) > -1) {
     105                     return true;
     106                 }
     107                 return false;
     108             },
     109             removeClassName: function (item, className) {
     110                 if (this.hasClassName(item, className)) {
     111                     var _class = item.className.split(' ');
     112                     item.className = "";
     113                     for (var i = 0; i < _class.length; i++) {
     114                         if (_class[i] != className) {
     115                             item.className += _class[i] + " ";
     116                         }
     117                     }
     118                 }
     119             },
     120             isRemovedByXY: function (x, y) {
     121                 return this.isRemoved(this.getItem(x,y));
     122             },
     123             isRemoved: function (item) {
     124                 return this.hasClassName(item, this.removedClassName);
     125             },
     126             removeItem: function (item) {
     127                 item.innerHTML = "&nbsp;";
     128                 this.addClassName(item, this.removedClassName);
     129             },
     130             UnselectAll: function () {
     131                 var select = document.getElementsByClassName(this.selectClassName);
     132                 for (var i = select.length - 1; i >= 0; i--) {
     133                     this.removeClassName(select[i], this.selectClassName);
     134                 }
     135             },
     136             Unselect: function (index) {
     137                 this.removeClassName(this.Images[index], this.selectClassName);
     138 
     139                 this.From = null;
     140                 if(this.To!=null){
     141                     this.Select(this.To);
     142                 }
     143                 this.To = null;
     144             },
     145             Check: function () {
     146                 if (this.CheckType() && this.CheckPath()) {
     147                     var from = this.getFrom();
     148                     var to = this.getTo();
     149                     this.clearTo();
     150                     this.clearFrom();
     151                     this.Remove(from, to);
     152                 } else {
     153                     this.clearFrom();
     154                 }
     155             },
     156             CheckType: function (fIndex, tIndex) {
     157                 //检查选择的两个 点数据类型是否一致
     158                 if (fIndex != null && tIndex != null) {
     159                     if (this.Data[fIndex] != this.Data[tIndex]) {
     160                         return false;
     161                     }
     162                 } else {
     163                     var from = this.getFrom(),
     164                         to = this.getTo();
     165 
     166                     if (this.Data[from.index] != this.Data[to.index]) {
     167                         return false;
     168                     }
     169                 }
     170                 return true;
     171             },
     172             isBeside: function (p1, p2) {//相邻
     173                 if ((p1.x == p2.x && Math.abs(p1.y - p2.y) == 1) ||
     174                     (p1.y == p2.y && Math.abs(p1.x - p2.x) == 1)) {
     175                     this.addPath(p2);
     176                     return true;
     177                 }
     178                 return false;
     179             },
     180             isLine: function (from, to) {
     181                 //检查两个点是否直线相连
     182                 var flag = true;
     183                 if (from.x == to.x) {
     184                     for (var i = Math.min(from.y, to.y) + 1 ; i < Math.max(from.y, to.y) ; i++) {
     185                         if (!this.isRemovedByXY(from.x, i)) {
     186                             flag = false;
     187                             break;
     188                         }
     189                     }
     190                 } else {
     191                     for (var i = Math.min(from.x, to.x) + 1 ; i < Math.max(from.x, to.x) ; i++) {
     192                         if (!this.isRemovedByXY(i, from.y)) {
     193                             flag = false;
     194                             break;
     195                         }
     196                     }
     197                 }
     198                 return flag;
     199             },
     200             isBorder: function (p1, p2) {//邻边
     201                 if (0 == p1.x && 0 == p2.x) {
     202                     this.addPath([{ x: -1, y: p1.y }, { x: -1, y: p2.y },p2]);
     203                     return true;
     204                 } else if (0 == p1.y && 0 == p2.y) {
     205                     this.addPath([{ x: p1.x, y: -1 }, { x: p2.x, y: -1 },p2]);
     206                     return true;
     207                 } else if (13 == p1.x && 13 == p2.x) {
     208                     this.addPath([{ x: 14, y: p1.y }, { x: 14, y: p2.y },p2]);
     209                     return true;
     210                 } else if (9 == p1.y && 9 == p2.y) {
     211                     this.addPath([{ x: p1.x, y: 10 }, { x: p2.x, y: 10 },p2]);
     212                     return true;
     213                 }
     214                 return false;
     215             },
     216             isCross: function (p1, p2) {//对角相邻
     217                 if (this.isRemovedByXY(p1.x,p2.y)) {
     218                     this.addPath([{ x: p1.x, y: p2.y },p2]);
     219                     return true;
     220                 }
     221                 else if (this.isRemovedByXY(p2.x,p1.y)) {
     222                     this.addPath([{ x: p2.x, y: p1.y },p2]);
     223                     return true;
     224                 }
     225                 return false;
     226             },
     227             isHasPath: function (from, to) {
     228                 //从两点之间连接
     229                 var flag = true;
     230 
     231                 //矩形边相通
     232                 if (this.isRemovedByXY(from.x, to.y)) {
     233                     if (this.isLine(from, { x: from.x, y: to.y })
     234                         && this.isLine({ x: from.x, y: to.y }, to)) {
     235                         this.addPath([{ x: from.x, y: to.y },to]);
     236                         return true;
     237                     }
     238                 } else if (this.isRemovedByXY(to.x, from.y)) {
     239                     if (this.isLine(from, { x: to.x, y: from.y })
     240                         && this.isLine({ x: to.x, y: from.y }, to)) {
     241                         this.addPath([{ x: to.x, y: from.y }, to]);
     242                         return true;
     243                     }
     244                 } else {
     245                     flag = false;
     246                 }
     247 
     248                 var maxX = Math.max(from.x, to.x), minX = Math.min(from.x, to.x),
     249                     maxY = Math.max(from.y, to.y), minY = Math.min(from.y, to.y);
     250 
     251 
     252                 var y = from.x == minX ? from.y : to.y;
     253                 for (var x = minX + 1; x <= maxX; x++) {
     254                     flag = true;
     255                     if (!this.isRemovedByXY(x, y)) {
     256                         flag = false;
     257                         break;
     258                     }
     259 
     260                     var p1 = { x: x, y: y }, p2 = { x: x, y: y == minY ? maxY : minY };
     261                     if (!this.isRemovedByXY(p2.x, p2.y)) {
     262                         flag = false;
     263                         continue;
     264                     }
     265                     if (this.isLine(p1, p2)
     266                         && this.isLine(p2, { x: maxX, y: p2.y })) {
     267                         this.addPath([p1,p2]);
     268                         flag = true;
     269                         break;
     270                     } else {
     271                         flag = false;
     272                     }
     273                 }
     274 
     275                 if (!flag) {
     276                     var x = from.y == minY ? from.x : to.x;
     277 
     278                     for (var y = minY + 1; y <= maxY; y++) {
     279                         flag = true;
     280                         if (!this.isRemovedByXY(x, y)) {
     281                             flag = false;
     282                             break;
     283                         }
     284 
     285                         var p1 = { x: x, y: y }, p2 = { x: x == minX ? maxX : minX, y: y };
     286                         if (!this.isRemovedByXY(p2.x, p2.y)) {
     287                             flag = false;
     288                             continue;
     289                         }
     290                         if (this.isLine(p1, p2) && this.isLine(p2, { x: p2.x, y: maxY })) {
     291                             this.addPath([p1,p2]);
     292                             flag = true;
     293                             break;
     294                         } else {
     295                             flag = false;
     296                         }
     297                     }
     298                 }
     299 
     300                 if (flag) {
     301                     this.addPath(to);
     302                     return true;
     303                 }
     304 
     305                 //从左边往回连接
     306                 flag = true;
     307                 var y = from.x == minX ? from.y : to.y;
     308                 for (var x = minX - 1; x >= -1; x--) {
     309                     if (x == -1) {
     310                         var p2 = { x: 0, y: y == minY ? maxY : minY };
     311 
     312                         if (!this.isRemovedByXY(p2.x, p2.y)) {
     313                             flag = false;
     314                             break;
     315                         }
     316 
     317                         flag = this.isLine(p2, { x: maxX, y: p2.y });
     318 
     319                         if (flag) {
     320                             this.addPath([{ x: -1, y: from.y }, { x: -1, y: to.y }]);
     321                         }
     322 
     323                         break;
     324                     }
     325                     flag = true;
     326 
     327                     if (this.isRemovedByXY(x, minY) && this.isRemovedByXY(x, maxY)) {
     328                     } else {
     329                         flag = false;
     330                         break;
     331                     }
     332 
     333                     var p1 = { x: x, y: y }, p2 = { x: x, y: y == minY ? maxY : minY };
     334                     if (!this.isRemovedByXY(p2.x, p2.y)) {
     335                         flag = false;
     336                         continue;
     337                     }
     338                     if (this.isLine(p1, p2) && this.isLine(p2, { x: maxX, y: p2.y })) {
     339                         this.addPath([p1,p2]);
     340                         flag = true;
     341                         break;
     342                     } else {
     343                         flag = false;
     344                     }
     345                 }
     346                 if (flag) {
     347                     this.addPath(to);
     348                     return true;
     349                 }
     350 
     351 
     352                 //从右边连接
     353                 flag = true;
     354                 var y = from.x == maxX ? from.y : to.y;
     355                 for (var x = maxX + 1; x <= 14; x++) {
     356                     if (x == 14) {
     357                         var p2 = { x: 13, y: y == minY ? maxY : minY };
     358                         if (!this.isRemovedByXY(p2.x, p2.y)) {
     359                             flag = false;
     360                             break;
     361                         }
     362                         flag = this.isLine(p2, { x: minX, y: p2.y });
     363                         if (flag) {
     364                             this.addPath([{ x: x, y: from.y }, { x: x, y: to.y }]);
     365                         }
     366                         break;
     367                     }
     368                     flag = true;
     369 
     370                     if (this.isRemovedByXY(x, minY) && this.isRemovedByXY(x, maxY)) {
     371                     } else {
     372                         flag = false;
     373                         break;
     374                     }
     375                     var p1 = { x: x, y: y }, p2 = { x: x, y: y == minY ? maxY : minY };
     376                     if (!this.isRemovedByXY(p2.x, p2.y)) {
     377                         flag = false;
     378                         continue;
     379                     }
     380                     if (this.isLine(p1, p2) && this.isLine(p2, { x: minX, y: p2.y })) {
     381                         this.addPath([p1,p2]);
     382                         flag = true;
     383                         break;
     384                     } else {
     385                         flag = false;
     386                     }
     387 
     388                 }
     389                 if (flag) {
     390                     this.addPath(to);
     391                     return true;
     392                 }
     393 
     394 
     395                 //从上边连接
     396                 flag = true;
     397                 var x = from.y == minY ? from.x : to.x;
     398                 for (var y = minY - 1; y >= -1; y--) {
     399                     if (y == -1) {
     400                         var p2 = { x: x == minX ? maxX : minX, y: 0 };
     401                         if (!this.isRemovedByXY(p2.x, p2.y)) {
     402                             flag = false;
     403                             break;
     404                         }
     405                         flag = this.isLine(p2, { x: p2.x, y: maxY });
     406                         if (flag) {
     407                             this.addPath([{ x: from.x, y: y }, { x: to.x, y: y }]);
     408                         }
     409                         break;
     410                     }
     411                     flag = true;
     412 
     413                     if (this.isRemovedByXY(minX, y) && this.isRemovedByXY(maxX, y)) {
     414                     } else {
     415                         flag = false;
     416                         break;
     417                     }
     418 
     419                     var p1 = { x: x, y: y }, p2 = { x: x == minX ? maxX : minX, y: y };
     420                     if (!this.isRemovedByXY(p2.x, p2.y)) {
     421                         flag = false;
     422                         continue;
     423                     }
     424                     if (this.isLine(p1, p2) && this.isLine(p2, { x: p2.x, y: maxY })) {
     425                         this.addPath([p1,p2]);
     426                         flag = true;
     427                         break;
     428                     } else {
     429                         flag = false;
     430                     }
     431 
     432                 }
     433                 if (flag) {
     434                     this.addPath(to);
     435                     return true;
     436                 }
     437 
     438                 //从下边连接
     439                 flag = true;
     440                 var x = from.y == maxY ? from.x : to.x;
     441                 for (var y = maxY + 1; y <= 10; y++) {
     442                     if (y == 10) {
     443                         var p2 = { x: x == minX ? maxX : minX, y: 9 };
     444                         if (!this.isRemovedByXY(p2.x, p2.y)) {
     445                             flag = false;
     446                             break;
     447                         }
     448                         flag = this.isLine(p2, { x: p2.x, y: minY });
     449                         if (flag) {
     450                             this.addPath([{ x: from.x, y: y }, { x: to.x, y: y }]);
     451                         }
     452                         break;
     453                     }
     454                     flag = true;
     455 
     456                     if (this.isRemovedByXY(minX, y) && this.isRemovedByXY(maxX, y)) {
     457                     } else {
     458                         flag = false;
     459                         break;
     460                     }
     461 
     462                     var p1 = { x: x, y: y }, p2 = { x: x == minX ? maxX : minX, y: y };
     463                     if (!this.isRemovedByXY(p2.x, p2.y)) {
     464                         flag = false;
     465                         continue;
     466                     }
     467                     if (this.isLine(p1, p2) && this.isLine(p2, { x: p2.x, y: minY })) {
     468                         this.addPath([p1,p2]);
     469                         flag = true;
     470                         break;
     471                     } else {
     472                         flag = false;
     473                     }
     474                 }
     475                 if (flag) {
     476                     this.addPath(to);
     477                     return true;
     478                 }
     479                 return false;
     480             },
     481             CheckPath: function (fIndex,tIndex) {
     482                 var from , to ;
     483                 if (fIndex != null && tIndex != null) {
     484                     from = this.parseIndexToXY(fIndex);
     485                     to = this.parseIndexToXY(tIndex);
     486                 } else {
     487                     from = this.getFrom().pos;
     488                     to = this.getTo().pos;
     489                 }
     490 
     491                 if (from == null || to == null)
     492                     return false;
     493 
     494                 this.pointArray = [];
     495                 this.addPath(from);
     496                 //相邻
     497                 if (this.isBeside(from,to)) {
     498                     return true;
     499                 }
     500 
     501                 //邻边
     502                 if(this.isBorder(from,to)){                    
     503                     return true;
     504                 }
     505 
     506 
     507                 // 相同行
     508                 if (from.y == to.y)
     509                 {
     510                     return this.CheckSameRow(from,to);
     511                 }
     512 
     513                 //相同列
     514                 if (from.x == to.x)
     515                 {
     516                     return this.CheckSameColumns(from, to);
     517                 }
     518 
     519 
     520                 //对角相邻
     521                 if (Math.abs(from.x - to.x) == 1 && Math.abs(from.y - to.y) == 1)
     522                 {
     523                     if (this.isCross(from, to)) {
     524                         return true;
     525                     }
     526                     return false;
     527                 }
     528 
     529                 //任意位置
     530                 return this.isHasPath(from, to);
     531             },
     532             CheckSameColumns: function (from, to) {
     533                 var flag = true;
     534                 //直线路径
     535                 flag = this.isLine(from, to);
     536                 if (!flag) {
     537                     flag = true;
     538                     //往左
     539                     var x = from.x - 1;
     540                     var _pf, _pt;
     541                     while (x >= -1) {
     542                         flag = true;
     543                         _pf = { x: x, y: from.y };
     544                         _pt = { x: x, y: to.y };
     545 
     546                         if (x == -1) {
     547                             break;
     548                         }
     549 
     550                         if (this.isRemovedByXY(_pf.x, _pf.y) && this.isRemovedByXY(_pt.x, _pt.y)) {
     551                             flag = this.isLine(_pf, _pt);
     552                         }
     553                         else {
     554                             flag = false;
     555                             break;
     556                         }
     557 
     558                         if (flag)
     559                             break;
     560                         x--;
     561                     }
     562                     if (flag) {
     563                         this.addPath(_pf);
     564                         this.addPath(_pt);
     565                     }
     566                 }
     567 
     568 
     569                 if (!flag) {
     570                     flag = true;
     571                     //往下
     572                     var x = from.x + 1;
     573                     var _pf, _pt;
     574                     while (x <= 14) {
     575                         flag = true;
     576                         _pf = { x: x, y: from.y };
     577                         _pt = { x: x, y: to.y };
     578 
     579                         if (x == 14) {
     580                             break;
     581                         }
     582 
     583                         if (this.isRemovedByXY(_pf.x, _pf.y) && this.isRemovedByXY(_pt.x, _pt.y)) {
     584                             flag = this.isLine(_pf, _pt);
     585                         }
     586                         else {
     587                             flag = false;
     588                             break;
     589                         }
     590                         if (flag)
     591                             break;
     592                         x++;
     593                     }
     594                     if (flag) {
     595                         this.addPath(_pf);
     596                         this.addPath(_pt);
     597                     }
     598                 }
     599                 if (flag) {
     600                     this.addPath(to);
     601                 }
     602                 return flag;
     603             },
     604             CheckSameRow: function (from, to) {
     605                 var flag = true;
     606                 //直线路径
     607                 flag = this.isLine(from, to);
     608 
     609                 if (!flag) {
     610                     flag = true;
     611                     //往上
     612                     var y = from.y - 1;
     613                     var _pf, _pt;
     614                     while (y >= -1) {
     615                         flag = true;
     616                         _pf = { x: from.x, y: y };
     617                         _pt = { x: to.x, y: y };
     618 
     619                         if (y == -1) {
     620                             break;
     621                         }
     622 
     623                         if (this.isRemovedByXY(_pf.x, _pf.y) && this.isRemovedByXY(_pt.x, _pt.y)) {
     624                             flag = this.isLine(_pf, _pt);
     625                         }
     626                         else {
     627                             flag = false;
     628                             break;
     629                         }
     630                         if (flag) {
     631                             break;
     632                         }
     633                         y--;
     634                     }
     635                     if (flag) {
     636                         this.addPath(_pf);
     637                         this.addPath(_pt);
     638                     }
     639                 }
     640 
     641 
     642                 if (!flag) {
     643                     flag = true;
     644                     //往下
     645                     var y = from.y + 1;
     646                     var _pf, _pt;
     647                     while (y <= 10) {
     648                         flag = true;
     649                         _pf = { x: from.x, y: y };
     650                         _pt = { x: to.x, y: y };
     651 
     652                         if (y == 10) {
     653                             break;
     654                         }
     655 
     656                         if (this.isRemovedByXY(_pf.x, _pf.y) && this.isRemovedByXY(_pt.x, _pt.y)) {
     657                             flag = this.isLine(_pf, _pt);
     658                         } else {
     659                             flag = false;
     660                             break;
     661                         }
     662                         if (flag)
     663                             break;
     664                         y++;
     665                     }
     666                     if (flag) {
     667                         this.addPath(_pf);
     668                         this.addPath(_pt);
     669                     }
     670                 }
     671 
     672                 if (flag) {
     673                     this.addPath(to);
     674                 }
     675                 return flag;
     676             },
     677             Parse: function (item) {
     678                 return { index: item.id, target: item, pos: this.parseIndexToXY(item.id) };
     679             },
     680             parseIndexToXY: function (index) {
     681                 var y = Math.floor(index / 14);
     682                 var x = index % 14;
     683                 return { x: x, y: y };
     684             },
     685             getHasPath: function () {
     686                 var flag = false;
     687                 var i = 0, j = 0;
     688                 if (this.leaveCount > 0) {
     689                     for (; i < 140 - 1; i++) {
     690                         if (this.Data[i] != null) {
     691                             for (j = i + 1; j < 140; j++) {
     692                                 if (this.Data[j] != null) {
     693                                     if (this.CheckType(i, j)) {
     694                                         if (this.CheckPath(i, j)) {
     695                                             flag = true;
     696                                             break;
     697                                         }
     698                                     }
     699                                 }
     700                             }
     701 
     702                             if (flag)
     703                                 break;
     704                         }
     705                     }
     706                 }
     707 
     708                 if (flag)
     709                     return {from:i,to:j};
     710                 return null;
     711             },
     712             Clue: function () {
     713                 //提示可消除的两个点
     714                 this.clearTo();
     715                 this.clearFrom();
     716 
     717                 var next = this.getHasPath();
     718                 
     719                 if (next!=null) {
     720                     if (!this.decreaseLife())
     721                         return;
     722                     var pos = this.parseIndexToXY(next.from);
     723                     var item = this.getItem(pos.x, pos.y);
     724                     this.addClassName(item, this.selectClassName);
     725 
     726                     var pos = this.parseIndexToXY(next.to);
     727                     var item = this.getItem(pos.x, pos.y);
     728                     this.addClassName(item, this.selectClassName);
     729                 }
     730             },
     731             HasNext: function () {
     732                 //检查是否存在可消除的两个 点
     733                 return this.getHasPath() != null;
     734             },
     735             Remove: function (from, to) {
     736                 //消除元素
     737                 var lines = this.drawLine();
     738 
     739                 var _this = this;
     740                 this.addTime();
     741                 
     742                 this.addScores();
     743 
     744                 this.Data[from.index] = null;
     745                 this.Data[to.index] = null;
     746                 this.leaveCount -=2;
     747                 setTimeout(function () {
     748                     _this.removeItem(from.target);
     749                     _this.removeItem(to.target);
     750 
     751                     for (var i = 0; i < lines.length; i++) {
     752                         document.body.removeChild(lines[i]);
     753                     }
     754 
     755                     lines = null;                    
     756                     _this.Change();
     757                 }, 100);
     758             },
     759             Change: function () {
     760                 //关卡布局方法,比如左右分离,上下分离等
     761                 if (this.leaveCount == 0) {
     762                     this.NextLevel();
     763                     return;
     764                 }
     765 
     766                 var leavel = this.currentLevel;
     767 
     768                 if (leavel == 3) {//左右分离
     769                     for (var y = 0; y < 10; y++) {
     770                         for (var x = 0; x < 7; x++) {
     771                             var index = y * 14 + x;
     772                             if (this.Data[index] != null) {
     773                                 continue;
     774                             }
     775                             var flag = false;
     776                             for (var i = x + 1; i < 7; i++) {
     777                                 var _index = y * 14 + i;
     778                                 if (this.Data[_index] != null) {
     779                                     this.Data[index] = this.Data[_index];
     780                                     this.Data[_index] = null;
     781                                     flag = true;
     782                                     break;
     783                                 }
     784                             }
     785 
     786                             if (!flag)
     787                                 break;
     788                         }
     789 
     790                         for (var x = 13; x >= 7; x--) {
     791                             var index = y * 14 + x;
     792                             if (this.Data[index] != null) {
     793                                 continue;
     794                             }
     795                             var flag = false;
     796                             for (var i = x - 1; i >= 7; i--) {
     797                                 var _index = y * 14 + i;
     798                                 if (this.Data[_index] != null) {
     799                                     this.Data[index] = this.Data[_index];
     800                                     this.Data[_index] = null;
     801                                     flag = true;
     802                                     break;
     803                                 }
     804                             }
     805 
     806                             if (!flag)
     807                                 break;
     808                         }
     809                     }
     810                 }
     811                 else if (leavel == 4) {//上下分离
     812                     for (var x = 0; x < 14; x++) {
     813                         for (var y = 0; y < 5; y++) {
     814                             var index = y * 14 + x;
     815                             if (this.Data[index] != null) {
     816                                 continue;
     817                             }
     818                             var flag = false;
     819                             for (var i = y + 1; i < 5; i++) {
     820                                 var _index = i * 14 + x;
     821                                 if (this.Data[_index] != null) {
     822                                     this.Data[index] = this.Data[_index];
     823                                     this.Data[_index] = null;
     824                                     flag = true;
     825                                     break;
     826                                 }
     827                             }
     828 
     829                             if (!flag)
     830                                 break;
     831                         }
     832 
     833                         for (var y = 9; y >= 5; y--) {
     834                             var index = y * 14 + x;
     835                             if (this.Data[index] != null) {
     836                                 continue;
     837                             }
     838                             var flag = false;
     839                             for (var i = y - 1; i >= 5; i--) {
     840                                 var _index = i * 14 + x;
     841                                 if (this.Data[_index] != null) {
     842                                     this.Data[index] = this.Data[_index];
     843                                     this.Data[_index] = null;
     844                                     flag = true;
     845                                     break;
     846                                 }
     847                             }
     848 
     849                             if (!flag)
     850                                 break;
     851                         }
     852                     }
     853                 }
     854                 else if (leavel == 5) {//往左移
     855                     for (var y = 0; y < 10; y++) {
     856                         for (var x = 0; x < 14; x++) {
     857                             var index = y * 14 + x;
     858                             if (this.Data[index] != null) {
     859                                 continue;
     860                             }
     861                             var flag = false;
     862                             for (var i = x + 1; i < 14; i++) {
     863                                 var _index = y * 14 + i;
     864                                 if (this.Data[_index] != null) {
     865                                     this.Data[index] = this.Data[_index];
     866                                     this.Data[_index] = null;
     867                                     flag = true;
     868                                     break;
     869                                 }
     870                             }
     871 
     872                             if (!flag)
     873                                 break;
     874                         }
     875                     }
     876                 }
     877                 else if (leavel == 6) {//向右移
     878                     for (var y = 0; y < 10; y++) {
     879                         for (var x = 13; x >= 0; x--) {
     880                             var index = y * 14 + x;
     881                             if (this.Data[index] != null) {
     882                                 continue;
     883                             }
     884                             var flag = false;
     885                             for (var i = x - 1; i >= 0; i--) {
     886                                 var _index = y * 14 + i;
     887                                 if (this.Data[_index] != null) {
     888                                     this.Data[index] = this.Data[_index];
     889                                     this.Data[_index] = null;
     890                                     flag = true;
     891                                     break;
     892                                 }
     893                             }
     894 
     895                             if (!flag)
     896                                 break;
     897                         }
     898                     }
     899                 }
     900                 else if (leavel == 7) {//向下移
     901                     for (var x = 0; x < 14; x++) {
     902                         for (var y = 9; y >= 0; y--) {
     903                             var index = y * 14 + x;
     904                             if (this.Data[index] != null) {
     905                                 continue;
     906                             }
     907                             var flag = false;
     908                             for (var i = y - 1; i >= 0; i--) {
     909                                 var _index = i * 14 + x;
     910                                 if (this.Data[_index] != null) {
     911                                     this.Data[index] = this.Data[_index];
     912                                     this.Data[_index] = null;
     913                                     flag = true;
     914                                     break;
     915                                 }
     916                             }
     917 
     918                             if (!flag)
     919                                 break;
     920                         }
     921                     }
     922                 }
     923                 else if (leavel == 8) {//向上移
     924                     for (var x = 0; x < 14; x++) {
     925                         for (var y = 0; y < 10; y++) {
     926                             var index = y * 14 + x;
     927                             if (this.Data[index] != null) {
     928                                 continue;
     929                             }
     930                             var flag = false;
     931                             for (var i = y + 1; i < 10; i++) {
     932                                 var _index = i * 14 + x;
     933                                 if (this.Data[_index] != null) {
     934                                     this.Data[index] = this.Data[_index];
     935                                     this.Data[_index] = null;
     936                                     flag = true;
     937                                     break;
     938                                 }
     939                             }
     940 
     941                             if (!flag)
     942                                 break;
     943                         }
     944                     }
     945                 }
     946                 else if (leavel == 9) {//向内紧缩
     947                     for (var y = 5; y < 10; y++) {
     948                         for (var x = 7; x < 14; x++) {
     949                             var index = y * 14 + x;
     950                             if (this.Data[index] != null) {
     951                                 continue;
     952                             }
     953                             var flag = false;
     954                             for (var i = x + 1; i < 14; i++) {
     955                                 var _index = y * 14 + i;
     956                                 if (this.Data[_index] != null) {
     957                                     this.Data[index] = this.Data[_index];
     958                                     this.Data[_index] = null;
     959                                     flag = true;
     960                                     break;
     961                                 }
     962                             }
     963 
     964                             if (!flag)
     965                                 break;
     966                         }
     967 
     968                         for (var x = 6; x >= 0; x--) {
     969                             var index = y * 14 + x;
     970                             if (this.Data[index] != null) {
     971                                 continue;
     972                             }
     973                             var flag = false;
     974                             for (var i = x - 1; i >= 0; i--) {
     975                                 var _index = y * 14 + i;
     976                                 if (this.Data[_index] != null) {
     977                                     this.Data[index] = this.Data[_index];
     978                                     this.Data[_index] = null;
     979                                     flag = true;
     980                                     break;
     981                                 }
     982                             }
     983 
     984                             if (!flag)
     985                                 break;
     986                         }
     987                     }
     988 
     989                     for (var y = 4; y > 0; y--) {
     990                         for (var x = 7; x < 14; x++) {
     991                             var index = y * 14 + x;
     992                             if (this.Data[index] != null) {
     993                                 continue;
     994                             }
     995                             var flag = false;
     996                             for (var i = x + 1; i < 14; i++) {
     997                                 var _index = y * 14 + i;
     998                                 if (this.Data[_index] != null) {
     999                                     this.Data[index] = this.Data[_index];
    1000                                     this.Data[_index] = null;
    1001                                     flag = true;
    1002                                     break;
    1003                                 }
    1004                             }
    1005 
    1006                             if (!flag)
    1007                                 break;
    1008                         }
    1009 
    1010                         for (var x = 6; x >= 0; x--) {
    1011                             var index = y * 14 + x;
    1012                             if (this.Data[index] != null) {
    1013                                 continue;
    1014                             }
    1015                             var flag = false;
    1016                             for (var i = x - 1; i >= 0; i--) {
    1017                                 var _index = y * 14 + i;
    1018                                 if (this.Data[_index] != null) {
    1019                                     this.Data[index] = this.Data[_index];
    1020                                     this.Data[_index] = null;
    1021                                     flag = true;
    1022                                     break;
    1023                                 }
    1024                             }
    1025 
    1026                             if (!flag)
    1027                                 break;
    1028                         }
    1029                     }
    1030 
    1031                     for (var x = 0; x < 7; x++) {
    1032                         for (var y = 5; y < 10; y++) {
    1033                             var index = y * 14 + x;
    1034                             if (this.Data[index] != null) {
    1035                                 continue;
    1036                             }
    1037                             var flag = false;
    1038                             for (var i = y + 1; i < 10; i++) {
    1039                                 var _index = i * 14 + x;
    1040                                 if (this.Data[_index] != null) {
    1041                                     this.Data[index] = this.Data[_index];
    1042                                     this.Data[_index] = null;
    1043                                     flag = true;
    1044                                     break;
    1045                                 }
    1046                             }
    1047 
    1048                             if (!flag)
    1049                                 break;
    1050                         }
    1051 
    1052                         for (var y = 4; y >= 0; y--) {
    1053                             var index = y * 14 + x;
    1054                             if (this.Data[index] != null) {
    1055                                 continue;
    1056                             }
    1057                             var flag = false;
    1058                             for (var i = y - 1; i >= 0; i--) {
    1059                                 var _index = i * 14 + x;
    1060                                 if (this.Data[_index] != null) {
    1061                                     this.Data[index] = this.Data[_index];
    1062                                     this.Data[_index] = null;
    1063                                     flag = true;
    1064                                     break;
    1065                                 }
    1066                             }
    1067 
    1068                             if (!flag)
    1069                                 break;
    1070                         }
    1071                     }
    1072 
    1073                     for (var x = 7; x < 14; x++) {
    1074                         for (var y = 5; y < 10; y++) {
    1075                             var index = y * 14 + x;
    1076                             if (this.Data[index] != null) {
    1077                                 continue;
    1078                             }
    1079                             var flag = false;
    1080                             for (var i = y + 1; i < 10; i++) {
    1081                                 var _index = i * 14 + x;
    1082                                 if (this.Data[_index] != null) {
    1083                                     this.Data[index] = this.Data[_index];
    1084                                     this.Data[_index] = null;
    1085                                     flag = true;
    1086                                     break;
    1087                                 }
    1088                             }
    1089 
    1090                             if (!flag)
    1091                                 break;
    1092                         }
    1093 
    1094                         for (var y = 4; y >= 0; y--) {
    1095                             var index = y * 14 + x;
    1096                             if (this.Data[index] != null) {
    1097                                 continue;
    1098                             }
    1099                             var flag = false;
    1100                             for (var i = y - 1; i >= 0; i--) {
    1101                                 var _index = i * 14 + x;
    1102                                 if (this.Data[_index] != null) {
    1103                                     this.Data[index] = this.Data[_index];
    1104                                     this.Data[_index] = null;
    1105                                     flag = true;
    1106                                     break;
    1107                                 }
    1108                             }
    1109 
    1110                             if (!flag)
    1111                                 break;
    1112                         }
    1113                     }
    1114                 }
    1115                 for (var i = 0; i < 140; i++) {
    1116                     var shucai = this.getItem(i);
    1117                     shucai.className = this.className;
    1118                     if (this.Data[i] !== null && this.Data[i] != undefined) {
    1119                         shucai.innerText = this.Data[i];
    1120                     } else {
    1121                         this.removeItem(shucai);
    1122                     }
    1123                 }
    1124 
    1125                 if (!this.HasNext()) {
    1126                     if (!this.decreaseLife()) {
    1127                         return;
    1128                     }
    1129                     this.Init();
    1130                 }
    1131             },
    1132             NextLevel: function () {
    1133                 //下一关
    1134                 this.addLife();
    1135                 this.currentLevel++;
    1136                 if (this.currentLevel > this.maxLevel) {
    1137                     this.Stop();
    1138                 } else {
    1139                     this.InitData();
    1140                     this.Init();
    1141                 }
    1142             },
    1143             Start: function () {
    1144                 //开始游戏
    1145                 if (this.time == null || this.time == undefined) {
    1146                     this.timeout = this.getTime();
    1147                     this.showTime();
    1148 
    1149                     this.TimeStart();
    1150                 }
    1151             },
    1152             ReStart: function () {//游戏重新开始
    1153                 for (var i in this.setting) {
    1154                     this[i] = this.setting[i];
    1155                 }
    1156 
    1157                 var container = document.getElementById("Container");
    1158                 container.innerHTML = "";
    1159                 var _this = this;
    1160                 var Images = [];
    1161                 //生成游戏主要点,共14列*10行
    1162                 for (var i = 0; i < 140; i++) {
    1163                     var shucai = document.createElement("div");
    1164                     shucai.className = this.className;
    1165                     shucai.innerText = i;
    1166                     shucai.id = i;
    1167                     shucai.onclick = function () {
    1168                         _this.Select(this);
    1169                     }
    1170                     container.appendChild(shucai);
    1171 
    1172                     Images.push(shucai);
    1173                 }
    1174                 //保存游戏中元素数据的数组
    1175                 this.Data = [];
    1176                 //保存两点间折线的顶点坐标数组
    1177                 this.pointArray = [];
    1178                 //消除的起始点
    1179                 this.From = null;
    1180                 //结束点
    1181                 this.To = null;
    1182                 //保存显示的元素数据
    1183                 this.Images = Images;
    1184                 //剩余可消除数量
    1185                 this.leaveCount = 0;
    1186                 this.showScores();
    1187                 //随机生成游戏数据this.Data
    1188                 this.InitData();
    1189                 //初始化游戏,把数据随机显示在游戏界面
    1190                 this.Init();
    1191             },
    1192             TimeStart: function () {//游戏时间开始
    1193                 var _this = this;
    1194                 this.time = setTimeout(function () {
    1195                     _this.timeout--;
    1196                     _this.showTime();
    1197                     if (_this.timeout <= 0) {
    1198                         _this.Over();
    1199                     }
    1200                     else {
    1201                         _this.TimeStart();
    1202                     }
    1203                 }, 1000);
    1204             },
    1205             TimeStop: function () {//游戏时间暂停,开始
    1206                 if (this.time != null) {
    1207                     clearTimeout(this.time);
    1208                     this.time = null;
    1209 
    1210                     var div = document.createElement("div");
    1211                     div.className = "Pause";
    1212                     div.innerText = "暂停游戏";
    1213                     document.body.appendChild(div);
    1214                 } else {
    1215                     this.TimeStart();
    1216 
    1217                     var div = document.getElementsByClassName("Pause");
    1218                     if (div) {
    1219                         document.body.removeChild(div[0]);
    1220                     }
    1221                 }
    1222             },
    1223             Stop: function () {//通关游戏
    1224                 alert("恭喜你通关了游戏!");
    1225                 var container = document.getElementById("Container");
    1226                 container.innerHTML = "";
    1227                 container.innerText = "恭喜你通关了游戏!";
    1228             },
    1229             Over: function () {//游戏结束
    1230                 if (this.time != null) {
    1231                     clearTimeout(this.time);
    1232                     this.time = null;
    1233                 }
    1234                 alert("游戏结束了!");
    1235                 var container = document.getElementById("Container");
    1236                 container.innerHTML = "";
    1237                 container.innerText = "游戏结束了!";
    1238             },
    1239             addPath: function (pt) {//增加消除两点之间的拆线的顶点坐标
    1240                 if (Object.prototype.toString.call(pt) === '[object Array]') {
    1241                     for (var i = 0; i < pt.length; i++) {
    1242                         this.pointArray.push(pt[i]);
    1243                     }
    1244                 } else {
    1245                     this.pointArray.push(pt);
    1246                 }
    1247             },
    1248             drawLine: function () {//画轨迹
    1249                 var points = this.pointArray;
    1250                 if(points.length ==4){
    1251                     if(points[1].x!==points[0].x && points[1].y!=points[0].y){
    1252                         var temp = points[1];
    1253                         points[1]=points[2];
    1254                         points[2] = temp;
    1255                     }    
    1256                 }
    1257                 var lines = [];
    1258                 for (var i = 0; i < points.length - 1; i++) {
    1259                     lines.push(this.draw(points[i], points[i + 1]));
    1260                 }
    1261                 return lines;
    1262             },
    1263             draw: function (from, to) {//画消除时,两个元素之间的连线轨迹
    1264                 //同列
    1265                 var div = document.createElement("div");
    1266                 div.className = "line";
    1267                 if (from.x == to.x)
    1268                 {
    1269                     var minY = Math.min(from.y, to.y),
    1270                         maxY = Math.max(from.y, to.y);
    1271 
    1272                     div.style.width = "2px";
    1273 
    1274                     div.style.height = ((maxY - minY) * (30+2) +2) + "px";
    1275                         div.style.top = (100 + (30 +2) * minY + 15) + "px";
    1276                         div.style.left = (115 + (30 +2) * from.x) + "px";
    1277 
    1278                     //this.lines.push(div);
    1279 
    1280                 }
    1281                     //同行
    1282                 else if (from.y == to.y)
    1283                 {
    1284                     var minX = Math.min(from.x, to.x),
    1285                         maxX = Math.max(from.x, to.x);
    1286 
    1287                     div.style.height = "2px";
    1288 
    1289                     div.style.width = ((maxX - minX ) * (30+2) +2) + "px";
    1290                         div.style.left = (100 + (30+2) * minX +15) + "px";
    1291                         div.style.top = (100 + 15 + (30+2) * from.y) + "px";
    1292                     //this.lines.push(div);
    1293                 }
    1294 
    1295                 document.body.appendChild(div);
    1296                 return div;
    1297 
    1298             },
    1299             showScores: function () {//显示游戏所得分数
    1300                 document.getElementById("scores").innerText = this.Scores;
    1301             },
    1302             addScores: function () {//第消除一对,游戏分数会增加
    1303                 this.Scores += 20;
    1304                 this.showScores();
    1305             },
    1306             addTime: function () {//每消除一对,游戏时间会增加
    1307                 this.timeout += 5;
    1308                 this.timeout = Math.min(this.timeout, this.getTime());
    1309                 this.showTime();
    1310             },
    1311             showTime: function () {//显示游戏剩余时间
    1312                 this.timeout = Math.max(this.timeout, 0);
    1313                 document.getElementById("time").innerText = this.timeout;
    1314             },
    1315             showLife: function () {//显示生命值
    1316                 document.getElementById("life").innerText = this.Life;
    1317             },
    1318             addLife: function () {//过关后,生命值增加
    1319                 this.Life++;
    1320                 this.showLife();
    1321             },
    1322             decreaseLife: function () {//生命值减少
    1323                 this.Life--;
    1324                 //当生命值小于0时,游戏结束
    1325                 if (this.Life < 0) {
    1326                     this.Over();
    1327                     return false;
    1328                 }
    1329 
    1330                 this.showLife();
    1331                 return true;
    1332             },
    1333             showLevel: function () {//显示级别
    1334                 document.getElementById("level").innerText = this.currentLevel;
    1335             }
    1336         };
    1337         llk.prototype.setting = {
    1338             currentLevel: 1,//当前关数
    1339             maxLevel: 9,    //总关数
    1340             Scores: 0,      //分数
    1341             Life:6,         //生命值
    1342             minTime: 180,   //最少时间
    1343             className: "shucai",//默认CSS样式
    1344             removedClassName: "removed",//被消除的CSS样式
    1345             selectClassName:"select",//选择好的CSS样式
    1346             getTime:function(){//当前关闯关时间
    1347                 return (this.currentLevel -1) * 10 + this.minTime;
    1348             }
    1349         }
    如果写的内容对你有所帮助,请帮我点个赞吧,你的鼓励是对我最大的支持和动力
  • 相关阅读:
    Spring Security配置logout地址
    flex布局
    视口的学习笔记
    box-sizing属性
    css清除浮动
    line-height的理解
    position和float小结
    css居中方法小结
    margin重叠
    浅谈负margin
  • 原文地址:https://www.cnblogs.com/maerge/p/5452450.html
Copyright © 2020-2023  润新知