How JavaScript implements the landlords game and how javascript fights landlords

Source: Internet
Author: User

How JavaScript implements the landlords game and how javascript fights landlords

I would like to share with you the idea of using js to write landlords. If the code is not well written, please forgive me.

Here we will talk about the main functions of landlords: shuffling, licensing, player playing, computer playing, verification of playing rules, and no judgment on winning or losing. These functions are just implemented, the following describes the implementation of several functions in sequence:

1. Shuffling:  

Var pukes = this. manage. pukes; // an array for storing playing cards // shuffling for (var I =; I <pukes. length; I ++) {var tmp = pukes [I]; var index = util. random (I, pukes. length); // randomly exchange two cards pukes [I] = pukes [index]; pukes [index] = tmp ;}

  2. Licensing(By default, players are landlords and computers are farmers, therefore, licensing simply adds the pukes element to the pukes field of each player instance in a loop.

// Licensed 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 ++) {// The landlord has three more 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 play cardsPlayers playing cards are divided into two steps: the computer and the player's own two steps, the computer playing card is a very dumb way to play cards (with a card ):

// If (this. options. playerIndex = this. manage. curPlayerIndex) {var spks = [], gz = false; if (this. manage. curMaxPlayerIndex = this. options. playerIndex) {this. manage. required pukes = [];} if (this. isCompute) {// automatically issued by the computer var start =; var len = this. manage. invalid pukes. length |; while (start <this. pukes. length) {spks = []; for (var I =, j = start; I <len & j <this. pukes. length; I ++) {// You can select any one that can be output. if (this. pukes [j]. status) {s Pks. push (this. pukes [j ++]) ;}} if (spks. length) {if (rules. valids (spks, this. manage. shortpukes) {gz = true; break ;}} start ++ ;}} else {// players choose to play for (var I =; I <this. pukes. length; I ++) {if (this. pukes [I]. selected & this. pukes [I]. status) {spks. push (this. pukes [I]) ;}} if (rules. valids (spks, this. manage. invalid pukes) {gz = true;} else {alert ("the rule is not met! ") ;}} If (gz) {this. manage. curMaxPlayerIndex = this. options. playerIndex; this. manage. required pukes = []; for (var I =; I <spks. length; I ++) {this. pukesLen --; this. manage. invalid pukes. 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. Verify the card picking rules,A lot of functions are combined and then called cyclically. If true is returned, it indicates that the playing rule is correct:

// The following are the out-of-the-box rules var rules = {_ rules: [new danzRule (), new duiRule (), new sandRule (), new zandRule (), new shunzRule (), new liandRule ()], 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 rule} danzRule. prototype. valid = function (_ pukes, _ curPukes) {// verify var pukes = _ pukes; // the player's card var curPukes = _ curPukes; // if (Pukes & pukes. length =) {// compare the card face value if (! CurPukes |! CurPukes. length) {return true;} if (curPukes []. dians = & pukes []. dians <) {// special processing return false;} if (pukes []. dians ==& & curPukes []. dians <) {// special processing return true;} return pukes []. dians> curPukes []. dians;} return false;} function duiRule (_ pukes, _ curPukes) {// two rules} duiRule. prototype. valid = function (_ pukes, _ curPukes) {// verify var pukes = _ pukes; // the player's card var curPukes = _ curPukes; // if (pukes & pukes. length =) {// compare the nominal 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 () {// three-band} sandRule. prototype. valid = function (_ pukes, _ curPukes) {// verify var pukes = _ pukes; // the player's card var curPukes = _ curPukes; // if (pukes & (pukes. length >=) {// compare the 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 (books) return false; return getSum (books)> getSum (books);} return false; function getSum (books) {var sum =; for (var I =; I <books. length; I ++) {if (books [I] =) {if (I =) return; sum + = I ;}} return sum ;} function valid (books) {// verify whether 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 points for each of the 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 books;} function zandRule () {// bomb} zandRule. prototype. valid = function (_ pukes, _ curPukes) {var pukes = _ pukes; // the player's card var curPukes = _ curPukes; // 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 ;}} function liandRule () {// connected to} liandRule. prototype. valid = function (_ pukes, _ curPukes) {var pukes = _ pukes; // the player's card var curPukes = _ curPukes; // 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 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 () {// shunzi} shunzRule. prototype. valid = function (_ pukes, _ curPukes) {var pukes = _ pukes; // the player's card var curPukes = _ curPukes; // 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 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 ;}}

In the above four steps, I think the main four functions, other functions, such as initialization and event registration, are commented out in the source code. Do not spray if the writing is not good.

The idea of implementing the landlords game in JavaScript is described so much and I hope it will be helpful to you!

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.