Last active
September 26, 2015 05:37
-
-
Save zeuxisoo/1048403 to your computer and use it in GitHub Desktop.
javascript random cards on Big2 / DuDiZhu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script lanugage="javascript" type="text/javascript"> | |
| /* | |
| * Author: Zeuxis.Lo | |
| * Description: random cards on Big2 / DuDiZhu | |
| */ | |
| (function(window, undefined) { | |
| var DealCard = function() { | |
| this.roleA = []; | |
| this.roleB = []; | |
| this.roleC = []; | |
| this.roleD = []; | |
| this.remnant = []; | |
| this.id = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53]; | |
| this.icon = ['葵扇', '紅心', '梅花', '街塼']; | |
| this.name = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; | |
| this.joker= ["Joker S", "Joker B"]; | |
| this.cardName = []; | |
| for (var i=0, k = 0; i<this.icon.length; i++) { | |
| for(var j=0; j<this.name.length; j++) { | |
| this.cardName[k++] = this.icon[i] + " " + this.name[j]; | |
| } | |
| } | |
| this.cardName[52] = this.joker[0]; | |
| this.cardName[53] = this.joker[1]; | |
| } | |
| DealCard.prototype.rand = function() { | |
| var i, j; | |
| for(var k=0; k<1000; k++) { | |
| i = Math.ceil(Math.random() * 51); | |
| j = Math.ceil(Math.random() * 51); | |
| if (i != j) { | |
| this.permute(i, j); | |
| } | |
| } | |
| } | |
| DealCard.prototype.permute = function(a, b) { | |
| this.id[a] = parseInt(this.id[a]) + parseInt(this.id[b]); | |
| this.id[b] = parseInt(this.id[a]) - parseInt(this.id[b]); | |
| this.id[a] = parseInt(this.id[a]) - parseInt(this.id[b]); | |
| } | |
| DealCard.prototype.deal = function() { | |
| for (var i=0; i<13; i++) { | |
| this.roleA.push(this.id[i*4].toString()); | |
| this.roleB.push(this.id[i*4 + 1].toString()); | |
| this.roleC.push(this.id[i*4 + 2].toString()); | |
| this.roleD.push(this.id[i*4 + 3].toString()); | |
| } | |
| this.remnant.push(this.id[52].toString()); | |
| this.remnant.push(this.id[53].toString()); | |
| } | |
| DealCard.prototype.roleName = function(cards) { | |
| var temp = []; | |
| for(var id in cards) { | |
| temp.push( this.cardName[cards[id]] ); | |
| } | |
| return temp.join(" "); | |
| } | |
| var dealCard = new DealCard(); | |
| dealCard.rand(); | |
| dealCard.deal(); | |
| console.log("A: " + dealCard.roleA.join(" ")); | |
| console.log("*: " + dealCard.roleName(dealCard.roleA)); | |
| console.log("B: " + dealCard.roleB.join(" ")); | |
| console.log("*: " + dealCard.roleName(dealCard.roleB)); | |
| console.log("C: " + dealCard.roleC.join(" ")); | |
| console.log("*: " + dealCard.roleName(dealCard.roleC)); | |
| console.log("D: " + dealCard.roleD.join(" ")); | |
| console.log("*: " + dealCard.roleName(dealCard.roleD)); | |
| console.log("=: " + dealCard.remnant.join(" ")); | |
| console.log("*: " + dealCard.roleName(dealCard.remnant)); | |
| })(window); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment