Implement enumeration type, poker application function Createnum (p) {//constructor var enumeration = function () {Throw ' can not instantiate enumerations ';}
; Rewrite the prototype and assign the prototype to the variable proto var proto = Enumeration.prototype = {constructor:enumeration, tostring:function () {return T
His.name}, Valueof:function () {return this.value;}, Tojson:function () {return this.name;}
};
Add Class attribute, method enumeration.values = []; for (var n in P) {//separate each element of the object p into a separate object o, and store those objects o in the class attribute values array var o = object.create (proto);//Object o inherits the enumeration 3 Instance methods and Constructors Enumeration.prototype.valueOf = function () {return this.value*1;};
The valueof method of rewriting the prototype O.name = n;
O.value = P[n]; Enumeration[n] = o;
Add Class Property name, Value is Object o Enumeration.values.push (o);
} Enumeration.foreach = function (f,c) {for (var i =0;i<this.values.length;i++) {F.call (c,this.values[i]);
}
};
Return enumeration;
}//=== var Coin = Createnum ({penny:1,nickel:5,dime:10,quarter:25});
Console.log (Coin); /* Result: Enumeration object coin {[Function] ValUEs: [{[number:10] name: ' Penny ', value:1}, {[number:50] name: ' Nickel ', value:5}, {[number:100] name: '
Dime ', value:10}, {[number:250] name: ' Quarter ', value:25}], Penny: {[number:10] name: ' Penny ', value:1}, Nickel: {[number:50] name: ' Nickel ', value:5}, Dime: {[number:100] name: ' Dime ', value:10}, Quarter: {[Number: () Name: ' Quarter ', value:25}, foreach: [Function]}/* Console.log (COIN.DIME+2); The 102 Coin.dime itself inherits from the enumeration object, inheriting and modifying the ValueOf method to convert value to numeric computations//=== use the function createnum () to represent a pair of 54 card = = function cards (suit,
Rank) {this.suit = suit;
This.rank = rank;
} Card.suit = Createnum ({clubs:1,diamonds:2,heates:3,spades:4,joker:5}); Card.rank = Createnum ({three:3,four:4,five:5,six:6, seven:7,eight:8,nine:9,ten:10, Jack:11,queen:12,king:13,a
CE:14,TWO:15,SMALLJOKER:16,BIGJOKER:17});
Card.prototype.toString = function () {return this.rank.toString () + ' of ' +this.suit.tostring ();};
Card.prototype.compareTo = function (that) {if (This.rank<that.rank) return-1;
if (This.rank>that.rank) return 1;
return 0;
};
Card.orderbysuit = function (a,b) {if (a.suit< b.suit) return-1;
if (a.suit> b.suit) return 1;
return 0;
};
Card.orderbyrank = function (a,b) {if (a.rank< b.rank) return-1;
if (a.rank> B.rank) return 1;
return 0;
};
Define a pair of standard poker function Deck () {var cards = This.cards = []; Card.Suit.foreach (function (s) {//Perform if (s!=5) {Card.Rank.foreach for each suit (function (r) {if (R!= &&A mp
R!=) {Cards.push (new card (S, r));
}
});
}else{Card.Rank.foreach (function (r) {if (r = =) Cards.push (new card (S, r));
if (r = =) Cards.push (new card (S, r));
});
}
});
//Shuffle, and return the washing card Deck.prototype.shuffle = function () {var Deck = this.cards, len = deck.length;
for (var i = len-1;i>0;i--) {var r = Math.floor (Math.random () * (i+1)), temp;
temp = Deck[i], deck[i] = Deck[r], deck[r] = temp; } returnThis
};
Licensing, and returns an array of cards Deck.prototype.deal = function (n) {if (this.cards.length<n) throw ' out of cards ';
Return This.cards.splice (This.cards.length-n, N);
};
Start: var deck = new deck ();
var deck1 =deck.shuffle ();
var n = 17;
var hand1 = deck1.deal (n). sort (Card.orderbyrank);
for (var i = 0;i<n;i++) {var body = document.getElementById (' body ');
var div = document.createelement (' div ');
Div.style.width = ' 50px ';
Div.style.height = ' 100px ';
Div.style.border = ' 1px solid gray ';
Div.style.float = ' Left ';
div.innerhtml = Hand1[i].suit.name+ ' +hand1[i].rank.name;
Body.appendchild (DIV);
Console.log (hand1[i].suit.name+ ' +hand1[i].rank.name); }
The above is a small series for everyone to bring the JavaScript learning Note-sorting _ simple to implement the enumeration type, poker application of all the content, I hope to help you, a lot of support cloud Habitat Community ~