How JavaScript implements a landlord game _ javascript skills

Source: Internet
Author: User
This article mainly introduces the related information about how JavaScript implements the game of landlords. If you need it, you can refer to this article to share with you the idea of using js to write the game of landlords. The code is not well written, sorry.

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
 
  

  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
   
    

  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
     
      

  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
       
        
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
        
         
& Counts = countsd;} function getBooks (pukes) {// returns the number of points in the Three-band var books = []; for (var I =; I
         
          
& 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
          
           
=) {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
           
            
) {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
            
              =) {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
             
               ) {Return false;} if (! Books [pukes [I]. dians]) {books [pukes [I]. dians] =;} else {return false ;}} var start = false; for (var I =; I
              
               

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.