JavaScript realizes the game of bucket landlord _javascript

Source: Internet
Author: User
Tags shuffle

This article to share the knowledge of the use of JS to write the landlord's ideas, code writing is not good, please forgive me.

Here said the main features of the Landlord: Shuffle, licensing, players out of the card, computer licensing, licensing rules of the verification, win or lose what is not judged, but to achieve the main functions, the following in turn say several functions of the implementation:

1. Shuffle:  

var pukes=this.manage.pukes;//holds the array of poker cards
//Shuffle for
(var i=;i<pukes.length;i++) {
var tmp=pukes[i];
var index=util.random (i,pukes.length);//random exchange of two cards
Pukes[i]=pukes[index];
pukes[index]=tmp;

  2. Licensing (simple version of the default is the player himself as the landlord, the computer for farmers), because the front shuffle has been the order of the cards disrupted, so the licensing is simply a cycle of pukes elements in each player instance in the Pukes field.

Licensing
var start=;
for (Var i=;i<this.manage.pukes.length-;i++) {
if (start==this.manage.players.length) {
start=
}
This.manage.pukes[i].status=true;
this.manage.players[start].pukeslen++;
This.manage.players[start++].pukes.push (This.manage.pukes[i]);
for (Var i=this.manage.pukes.length-;i<this.manage.pukes.length;i++) {
//landlord more than three
this.manage.pukes[i]. Status=true;
this.manage.players[this.manage.curplayerindex].pukeslen++;
This.manage.players[this.manage.curplayerindex].pukes.push (This.manage.pukes[i]);

  3. Players out of the card, players out of the card into the computer and the player's own two steps, the computer card is a very fool's way out of the card (a card on the Out):

The card if (this.options.playerindex==this.manage.curplayerindex) {var spks = [],gz=false; if ( 
This.manage.curMaxPlayerIndex = = This.options.playerIndex) {this.manage.deskPukes = [];} if (This.iscompute) {//Computer automatic card
var start =;
var len=this.manage.deskpukes.length| |; 
while (Start < this.pukes.length) {spks = []; for (var i =, J=start i <len&&j<this.pukes.length; i++) {
Pick a random one to go out on the line if (this.pukes[j].status) {Spks.push (this.pukes[j++]);}
if (spks.length) {if (Rules.valids (Spks, This.manage.deskPukes)) {gz = true; break;}} start++; } else {//player chooses to license for (var i = i < this.pukes.length i++) {if this.pukes[i].selected && this.pukes[i].st
ATUs) {Spks.push (this.pukes[i]);} if (Rules.valids (Spks, This.manage.deskPukes)) {gz=true;} else{alert ("does not match the rules!")
");
}
} if (GZ) {this.manage.curmaxplayerindex=this.options.playerindex; this.manage.deskPukes = []; for (var i =; I < Spks.len Gth
i++) {this.pukeslen--; This.manage.deskPukes.push (Spks[i]);Spks[i].status = false;
} this.manage.renderPukes ();
This.manage.renderCurDiscard (); if (this.iscompute| |
GZ) {This.manage.nextPlayer ();}} else{alert ("It's not your turn to play!") ");
}

4. Authentication of the licensing rules, is a combination of a number of functions, and then a circular call, if encountered return ture is the license to comply with the rule:

Following is the licensing rule var rules={_rules:[new Danzrule (), New Duirule (), New Sandrule (), New Zandrule (), New Shunzrule (), New Liandr Ule ()], valids:function (_pukes,_curpukes) {for (Var i=;i<this._rules.length;i++) {if This._rules[i].valid (_pukes
, _curpukes)) {return true;}}
return false;
}
}; function Danzrule () {//single-sheet rule} danzrule.prototype.valid=function (_pukes,_curpukes) {//validate var pukes=_pukes;//player's card var curpukes=_curpukes;//to the left of the card if (pukes&&pukes.length==) {//Compare card face value if (!curpukes| |! Curpukes.length) {return true;} if (curpukes[].dians==&&pukes[].dians<) {//special handling return FALSE;} if (pukes[].
dians==&&curpukes[].dians<) {//special handling return true;} return pukes[].dians>curpukes[].dians;
return false; } function Duirule (_pukes,_curpukes) {//Two rule} duirule.prototype.valid=function (_pukes,_curpukes) {//validate Var pukes=_ pukes;//player's brand Var curpukes=_curpukes;//to the left of the card if (pukes&&pukes.length==) {//Compare card face value if (pukes[].dians>& &pukes[].dians> {return true;} if (pukes[].dians!=pukes[].dians) {return false;} if (!curpukes| |! Curpukes.length) {return true;} 
else {if (curpukes.length!=) {return false;} if (Curpukes[].dians > && curpukes[].dians >) {return false;
} if (Curpukes[].dians!= Curpukes[].dians) {return false;} if (Curpukes[].dians = =) {return false;}}
if (pukes[].dians==) {return true;} return pukes[].dians>curpukes[].dians;
return false; function Sandrule () {//triple band} sandrule.prototype.valid=function (_pukes,_curpukes) {//verify var pukes=_pukes;//player's card var curpukes=_curpukes;//to the left of the card if (pukes&& (pukes.length>=)) {//Compare card face value var books=getbooks (pukes); if (!valid (
Books)), return false; if (!curpukes| |!
Curpukes.length) return true;
if (curpukes.length!=books.length) return false;
var books=getbooks (curpukes);
if (!valid) return false;
Return to Getsum (books) >getsum (books);
return false; function Getsum (books) {var sum=; s (var i=;i<books.length;i++) {if (books[i]==) {if (i==) return; sum+=i;}
Um } function valid (books) {//Verify that the three bands are valid Var counts=, countsd=, d=true,start=false,startindex=-; for (Var i=;i<books.length;i++ {if (start&&books[i]==&&startindex!= (i)) {return false;} 
else{startindex=i} if (books[i]==) {if (!start) {start = true; StartIndex = i} counts++;} if (books[i]==) {d=false;} for (Var i=;i<books.length;i++) {if (d&&books[i]==) {countsd++} else if (!d&&books[i]==) {countsd+
+;
} return counts>&&counts==countsd; function Getbooks (pukes) {//returns the number of each point in three bands var books=[]; for (Var i=;i<pukes.length;i++) {if (!books[pukes[i].dians)) {books[pukes[i].dians]=;}
else{books[pukes[i].dians]++}}
return to books; } function Zandrule () {//Bomb} zandrule.prototype.valid=function (_pukes,_curpukes) {var pukes=_pukes;//player's card var curpukes=_curpukes;//to the left of the card if (pukes&&pukes.length==) {if (!allequal (pukes)) {return false;} if (!curpukes| | (curpukes.length>&&curpukes.length!=) | |!
Allequal (Curpukes)) {return true;} else{if (pukes[].dians==) {return true;} if (curpukes[].dians==) {return false;} return pukes[].dians>curpukes[].dians;
return false; function Allequal (pukes) {if!pukes| |!
Pukes.length) return false;
var Base=pukes[].dians;
for (Var i=;i<pukes.length;i++) {if (Base!=pukes[i].dians) {"Return false;}}" return true; The function Liandrule () {//hyphen} liandrule.prototype.valid=function (_pukes,_curpukes) {var pukes=_pukes;//player's card var Curpu kes=_curpukes;//to the left of the card if (pukes&&pukes.length>=) {if (!verificationcoherence (pukes)) {return false;} if (! curpukes| | curpukes.length<=) {return true;} if (!verificationcoherence (Curpukes)) {return false;} if (pukes.length!=
Curpukes.length) {return false;} return Getsumdians (pukes) >getsumdians (curpukes);
return false; function Getsumdians (pukes) {var sum=; for (var i=;i<pukes.length;i++) {Sum+=pukes[i].dians;} return sum;} function v Erificationcoherence (pukes) {//Verify coherence if!pukes| |!
Pukes.length) return false;
var books=[]; for (Var i=;i<pukes.length;i++) {if (pukes[i].dians==| | pukes[i].dians>) {return false;} if (!books[pukes[i].dians]) {books[pukes[i].dians]=}
else{books[pukes[i].dians]++} if (books[pukes[i].dians]>) {return false;}}
var Start=false;
for (Var i=;i<books.length;i++) {if (books[i]&&books[i]!=) {return false;} if (Books[i]==&&!start) {
Start=true;
} if (start&&books[i]!=) {return false;}}
return true; } function Shunzrule () {//CIS} shunzrule.prototype.valid=function (_pukes,_curpukes) {var pukes=_pukes;//player's card var
curpukes=_curpukes;//to the left of the card if (pukes&&pukes.length>=) {if (!verificationcoherence (pukes)) {return false;} if (!curpukes| | curpukes.length<=) {return true;} if (!verificationcoherence (Curpukes)) {return false;} if (pukes.length!=
Curpukes.length) {return false;} return Getsumdians (pukes) >getsumdians (curpukes);
return false; function Getsumdians (pukes) {var sum=; for (var i=;i<pukes.length;i++) {Sum+=pukes[i].dians;} return sum;} function Verificationcoherence (pukes) {//Verify coherence if (!pukes| |!
Pukes.length) return false;
var books=[]; for (Var i=;i<pukes.length;i++) {if (pukes[i].dians==| | pukes[i].dians>) {return false;} if (!books[pukes[i].dians]) {books[pukes[i].dians]=}
else{return false;}
var Start=false;
for (Var i=;i<books.length;i++) {if (Books[i]==&&!start) {start=true} if (Start&&!books[i)) {
return false;
} return true; }
}

The above 4 steps, is I think the main 4 functions, other functions, such as initialization, event registration, in the source code already has a comment, write not to spray.

JavaScript to achieve the idea of the landlord game to introduce so many people, I hope to help you!

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.