Randomly generate bucket landlord hand with JavaScript

Source: Internet
Author: User

Learning JavaScript reckoned for more than half a month, at least I have the basics of programming, learning is still easy, but JS in the face of the object is really hit the skull, but do not understand, and I have learned the C # is so far from the difference

Today wrote a randomly generated bucket landlord player hand code, I am quite satisfied, a slip of the bed, poke open the computer to share the first

Although this kind of stuff doesn't work at all, haha

Card list array element type:

[1, "Spades", One, "K"]

  

Each card is an array, there are 4 elements, the No. 0 element represents the current color size, the 2nd element represents the current card value size, 1th and 3rd are strings, used to represent the card surface information

The comparison size is divided into two cases, the card value is different and the card value is the same

Card value in different cases directly compared to the card value size to sort, the card value of the same will need to compare color size to sort

The difficulty lies in sorting hands, with the idea of a good after doing, directly affixed to the code!

//Create a cardfunctionCreatepoker () {Let color= [[1, "Spades",], [2, "Red Peach"], [3, "Plum"], [4, "block"]]; Let number= [[1, "3"], [2, "4"], [3, "5"], [4, "6"], [5, "7"], [6, "8"], [7, "9"], [8, "ten"], [9, "J"], [Ten, "Q"], [One, "K"], [12, " A "], [13," 2 "]]; //create a card array with each color corresponding to a valueLet pokerlist = [];  for(Let i = 0; I!== color.length; i++) {         for(Let j = 0; J!== Number.length; j + +{Pokerlist.push ([... color[i], ... number[j]); }    }    //Add size KingPokerlist.push (["", "", 14, "Xiao Wang")]); Pokerlist.push (["", "", 15, "King"]); //Shuffle the card array, return randomly-1 or 1 to sortPokerlist.sort (() = {        returnMath.random () > 0.5? -1:1;    }); returnPokerlist;//Finally, the resulting card array is returned}functionGivepoker (pokerlist) {//remove 17 items directly from the random ordinal group, then sortLet Sortarr = Sortpoker (Pokerlist.splice (0, 17)); //formatted outputLet str = "";  for(Let poker of Sortarr) {str+ = Poker[1] + poker[3] + "\ T"; }    returnstr;}//Sort ArraysfunctionSortpoker (pokerlist) {pokerlist.sort (Poker1, Poker2)= {        //The card value is the same as the comparison suit        if(poker1[2] = = = Poker2[2]) {            returnPoker1[0]-poker2[0]; }        //Card Value different comparison value        returnPOKER1[2]-poker2[2];    }); returnPokerlist;} Let Pokerlist=Createpoker (); let Player1poker=Givepoker (pokerlist); let Player2poker=Givepoker (pokerlist); let Player3poker=Givepoker (pokerlist); let Lastpoker=Givepoker (pokerlist); Console.log ("Player 1:" +player1poker); Console.log ("Player 2:" +player2poker); Console.log ("Player 3:" +player3poker); Console.log ("Hole Card:" + Lastpoker);

In the afternoon writing code has a full 80 lines, home after thinking after optimization only 40 line, personal feeling good, haha, expanded expanded, sleep! Continue to study hard tomorrow!

Randomly generate bucket landlord hand with JavaScript

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.