Checkers Game 2 (JavaScript implemented as a Class) + Add bulk move

Source: Internet
Author: User

Today, on the original basis, added a new function: When the blank square A with the mouse click on the same line of squares, or the same column, and when they are not next to each other, automatically will b to a square between, moving sequentially to fill the blank box.

varGame ={wid_num:3, Cotainerid:"Game_cotainer", Init:function(_num) {Console.log (' Init '); Game.wid_num= _num | | 3;        Console.log (Game.wid_num); varMax_num = Math.pow (Game.wid_num, 2); varArr=[]; vari = 0;  while(I <max_num) {Arr[i]= ++i;    } game.redraw (arr); }, Rand:function(_num) {Console.log (' Rand '); Game.wid_num=_num;        Console.log (Game.wid_num); varMax_num = Math.pow (Game.wid_num, 2); varArr=[]; vari = 0;  while(I <max_num) {Arr[i]= ++i; }        varRan_arr=[]; //after Random generation, the first element of the array must be 1Ran_arr[0]=1; Arr.splice (0,1); varj = 0; vararr_l =arr.length; vartmp;  while(arr_l) {tmp= Math.ceil (arr_l *math.random ()); Ran_arr.push (arr[tmp-1]); Arr.splice (TMP-1,1); arr_l=arr.length;    } game.redraw (Ran_arr); }, Redraw:function(arr) {Console.log (' Redraw '); varHtm_container= "" ; varM,n;  for(m=0;m<=game.wid_num; m++) {Htm_container+ = "<tr>";  for(n=0;n<game.wid_num;n++)             {                 if(M ==0 && n = = 0) {Htm_container+ = ' <td class= "canmove blank" value= "-1" ></td> "; }                 Else if(M = = 0 && n! = 0) {Htm_container+ = ' <td class= ' fixed ' > </td> '; }                 Else if(M! = 0) {Htm_container+ = (' <td class= ' canmove active "value=" ' + ((m-1) * game.wid_num +n) + ' "> ' + (((m-1) *game.wid_num +n) +1) + ' < /TD> ' ); }} Htm_container+ = "</tr>"; }        if(Htm_container! = undefined | | Htm_container! = "") {Htm_container= ' <table> ' + htm_container + ' </table> '; }        if(Htm_container! = undefined | | Htm_container! = "")        {             $("#"+game.cotainerid). html (Htm_container); Arr.foreach (function(Item,index) {$ ($ ("Td.active") [index]). text (item);        }); }}, move:function(event) {varConst_wid_num =Game.wid_num; varCur_obj = $ ( This); varCur_val = cur_obj.attr ("Value"); varCur_txt =Cur_obj.text (); //if she is next to the blank one or special one and then exchange the text of them         varCur_a = Math.floor (parseint (cur_val)/const_wid_num); varCur_b = parseint (cur_val)%Const_wid_num; varBla_obj = $ ($ ("Td.blank") [0]); varBla_val = bla_obj.attr ("Value"); varBla_a = Math.floor (parseint (bla_val)/const_wid_num); varBla_b = parseint (bla_val)%Const_wid_num; varI =0; if(Cur_val = = 0 && Bla_val = =-1) {game.exchange (bla_obj,cur_obj); }         Else if(Bla_val = =-1 && cur_val! = 0 && Cur_b = = 0 ){             varNextobj = $ ($ (' td[value= "0"] ') [0]);             Game.exchange (Bla_obj,nextobj); Bla_obj=Nextobj; varNextval = parseint (nextobj.attr ("value")) +Game.wid_num; Nextobj= $ ($ (' td[value= ' + nextval + ') ') [0]);  while(Nextval <=cur_val)                 {Game.exchange (bla_obj,nextobj); Bla_obj=Nextobj; Nextval= parseint (Nextobj.attr ("value")) +Game.wid_num; Nextobj= $ ($ (' td[value= ' + nextval + ') ') [0]); }         }         Else if(Cur_val = =-1 && bla_val = = 0) {game.exchange (bla_obj,cur_obj); }Else if(Cur_val = =-1 && bla_b = = 0 && bla_val! = 0 ){             //var nextobj = blankobj;             varNextval = parseint (bla_obj.attr ("value"))-Game.wid_num; varNextobj = $ ($ (' td[value= ' + nextval + ') ') [0]);  while(Nextval >=cur_val)                 {Game.exchange (bla_obj,nextobj); Bla_obj=Nextobj; Nextval= parseint (Nextobj.attr ("value"))-Game.wid_num; Nextobj= $ ($ (' td[value= ' + nextval + ') ') [0]); }             if(Cur_val = =-1) {Nextobj= $ ($ (' td[value= "-1"] ') [0]);             Game.exchange (Bla_obj,nextobj); }         }         Else if(cur_a = = bla_a && cur_b >bla_b) {             varNextval = parseint (bla_obj.attr ("value")) + 1 ; varNextobj = $ ($ (' td[value= ' + nextval + ') ') [0]);  while(Nextval <=cur_val)                 {Game.exchange (bla_obj,nextobj); Bla_obj=Nextobj; Nextval= parseint (Nextobj.attr ("value")) + 1; Nextobj= $ ($ (' td[value= ' + nextval + ') ') [0]); }         }         Else if(cur_a = = Bla_a && Cur_b <bla_b) {             varNextval = parseint (bla_obj.attr ("value"))-1 ; varNextobj = $ ($ (' td[value= ' + nextval + ') ') [0]);  while(Nextval >=cur_val)                 {Game.exchange (bla_obj,nextobj); Bla_obj=Nextobj; Nextval= parseint (Nextobj.attr ("value"))-1; Nextobj= $ ($ (' td[value= ' + nextval + ') ') [0]); }         }         Else if(Cur_b = = Bla_b && cur_a <bla_a) {             varNextval = parseint (bla_obj.attr ("value"))-Game.wid_num; varNextobj = $ ($ (' td[value= ' + nextval + ') ') [0]);  while(Nextval >=cur_val)                 {Game.exchange (bla_obj,nextobj); Bla_obj=Nextobj; Nextval= parseint (Nextobj.attr ("value"))-Game.wid_num; Nextobj= $ ($ (' td[value= ' + nextval + ') ') [0]); }         }         Else if(Cur_b = = Bla_b && cur_a >bla_a) {             varNextval = parseint (bla_obj.attr ("value")) +Game.wid_num; varNextobj = $ ($ (' td[value= ' + nextval + ') ') [0]);  while(Nextval <=cur_val)                 {Game.exchange (bla_obj,nextobj); Bla_obj=Nextobj; Nextval= parseint (Nextobj.attr ("value")) +Game.wid_num; Nextobj= $ ($ (' td[value= ' + nextval + ') ') [0]); }         }         Else if(cur_a = = Bla_a && Cur_b = = (Bla_b + 1)) | |//Blank in(cur_a = = Bla_a && Cur_b = = (bla_b-1)) | |(Cur_b= = Bla_b && cur_a = = (bla_a + 1)) | |(Cur_b= = Bla_b && cur_a = = (bla_a-1)) {game.exchange (bla_obj,cur_obj); /*Cur_obj.removeclass ("active");             Cur_obj.addclass ("blank");             Bla_obj.removeclass ("blank");             Bla_obj.addclass ("active");             Cur_obj.text ("");             Bla_obj.text (Cur_txt); */         }                  if(Cur_val = =-1) {game.success (); }}, Exchange:function(Blankobj, nextobj) {varNexttxt =Nextobj.text (); Nextobj.removeclass ("Active"); Nextobj.addclass ("Blank"); Blankobj.removeclass ("Blank"); Blankobj.addclass ("Active"); Nextobj.text ("");            Blankobj.text (Nexttxt); }, Success:function(){        $("Td.canmove"). Not ($ ("Td.blank")).function(index, domobj) {varval = parseint ($ (domobj). attr ("value"))); varTXT =parseint ($ (domobj). text ()); B_cor= val = = Txt-1  ; returnB_cor;        }); if(true==B_cor) {Alert ("Congratulation!!!!"); }            }};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.