Html5+js "Five sub-flying" game implementation (v) Moving pieces

Source: Internet
Author: User

In the last chapter we know how to deal with two important chess action, want to eat each other's pieces, first to move their own pieces. Now Walt shares with everyone, how to move the pieces.

Want to move the pieces, on the page, first click on the pieces to be moved, and then click on the target location, if you can move, then move the pieces of the target position, the original position will be emptied .

The above sentence, we have to take two steps to deal with: 1, to determine whether the target can be moved, 2, you can move the pieces of the move.

1, determine whether the target can be moved.

You can only press a line when you first move, and you cannot have other pieces between the target position and the original position:

    //whether it can be moved     This. Canmove =function(PDest, PSRC) {varPI = This. GetIndex (PDest, PSRC); if(pi && Pi.destindex! =pi.srcindex) {if( This. Chesses[pi.destindex].player = = Player.none && This. chesses[pi.srcindex].player! =player.none) {varI1, I2, J;  for(vari = 0; I < This. lines.length; i++) {I1= $.inarray (Pi.destindex, This. Lines[i]); I2= $.inarray (Pi.srcindex, This. Lines[i]); if(I1! =-1 && i2! =-1) {                        if(Pi.destindex <pi.srcindex) { for(varj = math.min (I1, I2); J < Math.max (I1, I2)-1; J + +) {                                if( This. chesses[ This. lines[i][j]].player! =player.none) {return false; }                            }                        } Else {                             for(varj = math.min (I1, i2) + 1; J <= Math.max (I1, I2); J + +) {                                if( This. chesses[ This. lines[i][j]].player! =player.none) {return false; }                            }                        }                        returnPi; }                }            }        }        return false; };

2. Moving pieces:

Can move, to determine whether to move, you can clip the other side of a pair of the other, if you want to be eaten by the other pieces for their own pieces; at the same time, according to the function of the previous chapter, we know that a clip or a pair of pieces may appear at the same time there are two lines or more than one piece is eaten, (We'll talk about it next time). :

    //Moving pieces     This. movechess =function(PDest, PSRC) { This. Chessarray =NULL; varPI = This. Canmove (PDest, PSRC); if(pi) { This. Chesses[pi.srcindex].moveto ( This. Chesses[pi.destindex]); varChess = This. Chesses[pi.destindex]; varR1 = This. Cancarry (chess), r2 = This. Canclip (chess); if(R1 | |R2) {                varRL = (R1 r1.length:0) + (R2? r2.length:0); if(RL = = 1) {                    if(R1) {//the pieces that were replaced                         This. Chessreplace.push (r1[0][0]);  This. Chessreplace.push (r1[0][1]); //Replace                         This. Chesses[r1[0][0].point.index].player =Chess.player;  This. Chesses[r1[0][1].point.index].player =Chess.player; } Else {                        //the pieces that were replaced                         This. Chessreplace.push (r2[0][0]); //Replace                         This. Chesses[r2[0][0].point.index].player =Chess.player; }                } Else {                    //If there are many pieces of the route can eat, first saved up, and then prompted the user to choose which line to eat                     This. Chessarray = []; if(R1) { for(vari = 0; i < r1.length; i++) {                             This. Chessarray.push (R1[i]); }                    }                    if(R2) { for(vari = 0; i < r2.length; i++) {                             This. Chessarray.push (R2[i]); }                    }                }            }            return true; }        return false; };

This section is relatively simple, we first digest. In the next section we will implement the operation and hints of multiple pieces.

Html5+js "Five sub-flying" game implementation (a) Rules

Html5+js "Five sub-flying" Game Realization (ii) route analysis and resource preparation

Html5+js "Five sub-flying" game implementation (iii) pages and chess pieces

Html5+js "Five sub-flying" game realization (four) clip one and pick a pair

Html5+js "Five sub-flying" game implementation (v) Moving pieces

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.