HTML5 + JS "wuzifei" game implementation (8) Man-Machine combat, html5 "wuzifei"

Source: Internet
Author: User

HTML5 + JS "wuzifei" game implementation (8) Man-Machine combat, html5 "wuzifei"

To enable human-computer combat, you must enable the computer to play chess automatically, and you must know that You can automatically search for the opponent's pawns to see if you can pick a pair and hold a pair of clips, this makes it interesting.

After a computer user finishes playing a game, the computer should immediately search for the user's chess piece, and then, if it is not eaten, then a random chess piece will be taken (to make the computer a player, you cannot play a random game, you need to consider whether the game will be eaten by the other party after playing the game, etc. Here we just do it randomly ).

 

Complete "five flying" Game Man-Machine battle: http://www.lyout.com/projects/Fiveflychess/FiveflyChess8.htm

 

// Find out if there is any piece to pick (I = 0; I <this. lines. length; I ++) {for (j = 0; j <this. lines [I]. length; j ++) {if (this. chesses [this. lines [I] [j]. player = p1 & j <this. lines [I]. length-2) {if (j + 2) <this. lines [I]. length & this. chesses [this. lines [I] [j + 1]. player = Player. none) {if (this. chesses [this. lines [I] [j + 2]. player = p1 ){

After finding two pieces that can be selected, you must determine that there cannot be other pieces on this line:

isfind = true;for (k = j + 3; k < this.lines[i].length; k++) {    if (this.chesses[this.lines[i][k]].player != Player.None) {        isfind = false;        break;    }}

Make sure that this line only has two chess pieces from the other party and can be picked. Then, check whether the computer can pick these two chess pieces:

Var result; for (k = 0; k <this. lines. length; k ++) {pos = $. inArray (this. lines [I] [j + 1], this. lines [k]); if (pos! =-1) {for (n = pos-1; n> = 0; n --) {p2 = this. chesses [this. lines [k] [n]. player; if (p2 = Player. none) continue; if (p2 = p1) break; // The Position of the Selected Part 1, the selected part 2, result = [this. lines [I] [j], this. lines [I] [j + 2], this. lines [I] [j + 1], this. lines [k] [n]; break;} for (n = pos + 1; n <this. lines [k]. length; n ++) {p2 = this. chesses [this. lines [k] [n]. player; if (p2 = Player. none) continue; if (p2 = p1) break; // The Position of the Selected Part 1, the selected part 2, result = [this. lines [I] [j], this. lines [I] [j + 2], this. lines [I] [j + 1], this. lines [k] [n]; break ;}}}

If you can't find a piece that can be picked, you can find the piece that can be clipped (the left and right sides of the piece may be used for the clip, and only the left side is analyzed here ):

// Find out if any piece can be clipped for (I = 0; I <this. lines. length; I ++) {for (j = 0; j <this. lines [I]. length; j ++) {if (this. chesses [this. lines [I] [j]. player = p1 & j> 0 & j <(this. lines [I]. length-1) {// if (this. chesses [this. lines [I] [j-1]. player = player) {// whether the opponent's pawns exist on the left

After finding the clip, you need to judge that only one of the existing ones and one of the other's ones can be found on this line, and you can see that:

Isfind = true; // other positions can only be empty for (k = 0; k <j-1; k ++) {if (this. chesses [this. lines [I] [k]. player! = Player. none) {isfind = false; break;} if (isfind) {for (k = j + 1; k <this. lines [I]. length; k ++) {if (this. chesses [this. lines [I] [k]. player! = Player. None) {isfind = false; break ;}}}

Finally, you can find another piece that can be used to execute the clip:

Var result; for (k = 0; k <this. lines. length; k ++) {pos = $. inArray (this. lines [I] [j + 1], this. lines [k]); if (pos! =-1) {for (n = pos-1; n> = 0; n --) {p2 = this. chesses [this. lines [k] [n]. player; if (p2 = Player. none) continue; if (p2 = p1) break; // The Position of the piece to be held. The result = [this. lines [I] [j], this. lines [I] [j + 1], this. lines [k] [n]; break;} for (n = pos + 1; n <this. lines [k]. length; n ++) {p2 = this. chesses [this. lines [k] [n]. player; if (p2 = Player. none) continue; if (p2 = p1) break; // The Position of the piece to be held. The result = [this. lines [I] [j], this. lines [I] [j + 1], this. lines [k] [n]; break ;}}}

 

After finding a piece that can be picked or clipped, you can directly execute the moveChess function, and then judge whether the game is over after execution.

If no suitable chess piece is found, you need to play the game automatically. For the time being, we have made the lowest level and randomly placed the next piece:

Var I, j, k, index, moved, mover; var items = []; // chess piece for (I = 0; I <this. chesses. length; I ++) {if (this. chesses [I]. player = this. currentPlayer) items. push (I);} if (items. length = 0) return; k = 0; moved = 0; while (k <20) {// a random pawn is obtained to move var movec = items. length = 1? 0: Math. floor (Math. random () * (items. length-1 + 1); var dirs = []; for (I = 0; I <this. lines. length; I ++) {index = $. inArray (items [movec], this. lines [I]); if (index! =-1) dirs. push (I);} // randomly obtain a movable route var linec = Math. floor (Math. random () * (dirs. length-1 + 1); // move the pawn index = $. inArray (items [movec], this. lines [dirs [linec]); mover = 0; if (index = 0 | index = (this. lines [dirs [linec]. length-1) {if (index = 0) mover = 1;} else {// random direction var dirc = Math. floor (Math. random () * (1 + 1); if (dirc = 0) mover = 1;} if (mover) {if (this. moveChess (this. lines [dirs [linec] [I Ndex + 1], items [movec]) {this. currentIndex = this. lines [dirs [linec] [index + 1]; console. log (this. currentIndex); moved = 1; break;} else {if (this. moveChess (this. lines [dirs [linec] [index-1], items [movec]) {this. currentIndex = this. lines [dirs [linec] [index-1]; console. log (this. currentIndex); moved = 1; break;} k ++;} if (moved) {if (! This. chessarray) {var player = this. currentPlayer; this. currentPlayer = this. getAnotherPlayer (player); this. changePlayer (); if (this. isGameOver (this. currentPlayer) {this. winner = player; this. isover = true ;}} else {this. winner = player; this. isover = true ;}

All right, the game "five child flying" is over now.

There are related comments in the Code. Let's talk about it with friends. If you don't know, you can leave a message. For source code, you can directly view the source code of the webpage.

There is still a small BUG, and there is no prompt at the end of the game. To start the game again, you need to refresh the page.

The purpose of liwater is to share with you how to start with a small game. The code is not fully considered and the partners can communicate with each other. Please advise the experts as well :)

 

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

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

HTML5 + JS game Implementation of wuzifei (5) mobile pawns

HTML5 + JS game Implementation of wuzifei (6) mouse response and multiple choices

HTML5 + JS game Implementation of wuzifei (7) Game trial

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.