HTML5 + JS game Implementation of wuzifei (4) one and one matching, and html5 wuzifei

Source: Internet
Author: User

HTML5 + JS game Implementation of wuzifei (4) one and one matching, and html5 wuzifei

In the first chapter, we have already said how to "folder one" and how to pick one, but it is only written. For coders, it is easier to understand the code.

 

In order to facilitate comparative analysis, we will first post the route again:

// Optional route this. lines = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [0, 5, 10, 15, 20], [1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24], [0, 6, 12, 18, 24], [4, 8, 12, 16, 20], [2, 6, 10], [2, 8, 14], [10, 16, 22], [14, 18, 22];

 

1. Folder one:

According to the finite route given above, to implement "folder one", the index of the home page pawn must be in [, 2... in 24], we cyclically search for each route. If we find the correct route and position, we can eat each other's pawns.

First, we can find out the target position of the chess piece in which route:

Int index = $. inArray (chess. point. index, this. lines [I]); // chess moves the pawn, the same below
If (index! =-1 )//...

Then find out which piece of chess can be eaten online. If, in the horizontal direction, a piece that has been eaten may be on the left or on the right. If it is on the left, another piece should be on the left of the piece that has been eaten:

Var p1 = index> 1? This. chesses [this. lines [I] [index-1]. player: Player. None; if (p1! = Player. None & p1! = Chess. player) {if (this. chesses [this. lines [I] [index-2]. player = chess. player ){//... locate a qualified route

Similarly, if a piece is on the right, the Party should have a piece on the right of the piece:

Var p2 = index <this. lines [I]. length-2? This. chesses [this. lines [I] [index + 1]. player: Player. None; if (p2! = Player. None & p2! = Chess. player) {if (this. chesses [this. lines [I] [index + 2]. player = chess. player ){//... locate a qualified route

However, according to the rules, when the opponent's chess pieces can be clipped, only three chess pieces can be placed on the path, two on both sides, and one on the other, A pawn cannot exist in other positions:

On the left side:

Var bfind = true; // whether to find the pawn that can be eaten for (j = 0; j <index-2; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) return; bfind = true; for (j = index + 1; j <this. lines [I]. length; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) return; chessArray. push ([this. chesses [this. lines [I] [index-1]); // found

For the case on the right:

Var bfind = true; for (j = 0; j <index; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) return; bfind = true; for (j = index + 3; j <this. lines [I]. length; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) return; chessArray. push ([this. chesses [this. lines [I] [index + 1]); // found

We record the pieces that can be clipped and store them in chessArray for other operations.

2. Pick one:

Similarly, we should first find out which path the pawn is in:

index = $.inArray(chess.point.index, this.lines[i]);if (index > 0 && index < this.lines[i].length - 1) {

Then it is much simpler than holding a piece. We only need to find that the two adjacent pieces of the piece are the pieces of the other side, and the other positions on the straight line are empty.

First, find two adjacent pawns:

var p1 = this.chesses[this.lines[i][index - 1]].player;var p2 = this.chesses[this.lines[i][index + 1]].player;if (p1 != chess.player && p2 != chess.player && p1 != Player.None && p2 != Player.None) {

Then we can determine that other locations are vacant:

Var bfind = true; for (j = 0; j <index-1; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) return; bfind = true; for (j = this. lines [I]. length-1; j> index + 1; j --) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) return; chessArray. push ([this. chesses [this. lines [I] [index-1], this. chesses [this. lines [I] [index + 1]); // found

 

Now we have implemented two basic functions. In the next chapter, let's talk about moving chess pieces. The two functions implemented in this chapter are summarized as follows:

// Can I select a pair? this. canCarry = function (chess) {var p1, p2, j, index, bfind, chessArray = []; for (var I = 0; I <this. lines. length; I ++) {index = $. inArray (chess. point. index, this. lines [I]); if (index> 0 & index <this. lines [I]. length-1) {p1 = this. chesses [this. lines [I] [index-1]. player; p2 = this. chesses [this. lines [I] [index + 1]. player; if (p1! = Chess. player & p2! = Chess. player & p1! = Player. None & p2! = Player. none) {bfind = true; for (j = 0; j <index-1; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) continue; bfind = true; for (j = this. lines [I]. length-1; j> index + 1; j --) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) continue; chessArray. push ([this. chesses [this. lines [I] [index-1], this. chesses [this. lines [I] [index + 1]) ;}} return chessArray. length = 0? False: chessArray;}; // specifies whether to "clip" this. canClip = function (chess) {var p1, p2, j, index, bfind, chessArray = []; for (var I = 0; I <this. lines. length; I ++) {index = $. inArray (chess. point. index, this. lines [I]); if (index! =-1) {p1 = index> 1? This. chesses [this. lines [I] [index-1]. player: Player. None; p2 = index <this. lines [I]. length-2? This. chesses [this. lines [I] [index + 1]. player: Player. None; if (p1! = Player. None & p1! = Chess. player) {if (this. chesses [this. lines [I] [index-2]. player = chess. player) {bfind = true; for (j = 0; j <index-2; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) continue; bfind = true; for (j = index + 1; j <this. lines [I]. length; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) continue; chessArray. push ([this. chesses [this. lines [I] [index-1]);} else if (p2! = Player. None & p2! = Chess. player) {if (this. chesses [this. lines [I] [index + 2]. player = chess. player) {bfind = true; for (j = 0; j <index; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) continue; bfind = true; for (j = index + 3; j <this. lines [I]. length; j ++) {if (this. chesses [this. lines [I] [j]. player! = Player. None) {bfind = false; break ;}} if (! Bfind) continue; chessArray. push ([this. chesses [this. lines [I] [index + 1]) ;}}} return chessArray. length = 0? False: chessArray ;};View Code

 

HTML5 + JS game Implementation of wuzifei (1) Rules

HTML5 + JS game Implementation of wuzifei (2) route analysis and resource preparation

HTML5 + JS game Implementation of wuzifei (3) page and board chess

Related Article

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.