The stack, queue (data structure and pull train game) of JS data structures

Source: Internet
Author: User

1.js implementation of queue data structure (FIFO)

functionQueue (array) {if(Object.prototype.toString.call (array)! = "[Object array]") {        Throw"Target isn't an Array!"    }     This. Queue =Array;  This. Pushqueue =function(ele) { This. Queue.push (ele)} This. Delqueue =function () {         This. Queue.shift ()} This. GetFirst =function () {        return  This. queue[0]    }     This. GetLast =function () {        return  This. queue[ This. Queue.length]}  This. IsEmpty =function () {        return  This. queue.length?false:true    }     This. GetArray =function () {        return  This. Queue}//out of the queue and get the elements out of the queue     This. Delget =function () {        varLast = This. GetFirst ();  This. Delqueue (); returnLast ; }}

2. JS implementation Stack (advanced post-out)

functionStack (array) {if(Object.prototype.toString.call (array)! = "[Object array]")Throw"Target isn't an Array!" This. Stack =Array;  This. Addstack =function(ele) { This. Stack.push (ele)} This. Delstack =function () {         This. Stack.pop (); }    //stack and get the elements out of the stack     This. Delget =function () {        varLast = This. GetLast ();  This. Delstack (); returnLast ; }     This. GetFirst =function () {        return  This. stack[0]    }     This. GetLast =function () {        return  This. stack[ This. stack.length-1]    }     This. GetArray =function () {        return  This. Stack}}

Realize pull Train game

The game conventions are as follows:

1. First given the same number of cards

2. Different players take turns to play cards

3. When the card is released, all the cards between the two nearest identical cards can be acquired by the current player and placed in the rear end

4. The player who first finishes the card loses

Define player Classes

//Name: Player's name queue: Queue data Structure PlayGround: A game console for storing players ' cardsfunctionDragtrainplayer (name, queue, playGround) {if(Object.prototype.toString.call (array)!== "[Object array]")Throw"Target isn't an Array!" This. Name =name;  This. Cards =queue;  This. GetName =function () {        return  This. Name} This. GetArray =function () {        return  This. Cards.getarray (); }    //Each card must be checked once     This. Givecard =function () {        if( This. Cards.getarray (). length==0)return0Playground.push ( This. Cards.delget ()) This. Check ()}//Check to see if a card can be acquired     This. Check =function () {        //In turn, the most recent cards between the same card interception, and the player's current hands of the card splicing         for(varI=playground-1; i>0; i--) {            if(Playground[playground.length-1] = = =Playground[i]) {                 This. Cards.getarray (). Concat ( This. Cards.slice (i))return            }        }    }}

Defining the game start function

functionGamestart (player) {varFlag = 1; //If the player does not appear without a card, it will continue to execute     while(flag) { for(vari = 0; i<arguments.length; i++) {            if(Arguments[i].getarray (). length = = 0) {flag= 0; //returns the name of the first player to finish the card                returnArguments[i].getname ();            } arguments[i].givecard (); //The following code block is used for each test of the cardConsole.log ("---------");  for(vari=0;i<arguments.length; i++) {Console.log (Arguments[i].getname ());                Console.log (Arguments[i].getarray ());            Console.log (PlayGround);            }; Console.log ("---------"); }    }}

Create an object and execute

varStack1 =NewQueue ([5,8,6,9,8]);varStack2 =NewQueue ([9,9,6,5,3]);varStack3 =NewQueue ([7,4,2,5,1]);varPlayer1 =NewDragtrainplayer ("Player1", Stack1, PlayGround)varPlayer2 =NewDragtrainplayer ("Player2", Stack2, PlayGround)varPlayer3 =NewDragtrainplayer ("Player3", Stack2, PlayGround) Console.log (Gamestart (Player1, Player2, Player3))

The stack, queue (data structure and pull train game) of JS data structures

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.