日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

JavaScript一个简易枚举类型实现扑克牌

發(fā)布時間:2023/12/20 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript一个简易枚举类型实现扑克牌 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<script type="text/javascript"> /*** 這個函數(shù)創(chuàng)建一個新的枚舉類型,實(shí)參對象表示類的每個實(shí)例的名字和值* 返回值是一個構(gòu)造函數(shù),它標(biāo)識這個新類* 注意,這個構(gòu)造函數(shù)也會拋出異常,不能使用它來創(chuàng)建該類型的新實(shí)例* 返回的構(gòu)造函數(shù)包含名/值對的映射表* 包括由值組成的數(shù)組,以及以個foreach()迭代器函數(shù)*/ function enumeration(namesToValues){//這個虛擬的構(gòu)造函數(shù)式返回值var enumeration = function(){throw "Can't Instantiate Enumeration"};//枚舉值繼承自這個對象var proto = enumeration.prototype = {constructor:enumeration, //標(biāo)識類型toString : function(){ return this.name; }, //返回名字valueOf : function(){ return this.value;}, //返回值toJSON : function(){ return this.name; }, //轉(zhuǎn)為JSON };enumeration.values =[]; //用以存放枚舉對象的數(shù)組//現(xiàn)在創(chuàng)建新類型的實(shí)例for( name in namesToValues){ //遍歷每個值var e = Object.create(proto); //創(chuàng)建一個代表它的對象e.name = name ; //給它一個名字e.value = namesToValues[name]; //給它一個值enumeration[name] = e; //將它設(shè)置為構(gòu)造函數(shù)的屬性enumeration.values.push(e); //將它存儲到值數(shù)組中 }//一個類方法,用來對類的實(shí)例進(jìn)行迭代enumeration.foreach = function(f,c){for(var i = 0 ; i<this.values.length; i++)f.call(c,this.values[i]);};//返回表示這個新類型的構(gòu)造函數(shù)return enumeration; }//枚舉類型使用示例: console.log("枚舉類型使用示例:"); var e = new enumeration({a:1,b:2,c:3}); console.log(e); console.log(e.values); e.foreach(console.log,e);//枚舉類型表示一副撲克牌: console.log("枚舉類型表示一副撲克牌:");//定義個已個表示“玩牌”的類 function Card( suit , rank ){this.suit = suit ; //每張牌都有花色this.rank = rank ; //以及點(diǎn)數(shù) }//使用枚舉類定義換色和點(diǎn)數(shù) Card.Suit = enumeration( {Clubs:1,Diamonds:2,Hearts : 3,Spades:4});Card.Rank = enumeration( {Two: 2, Three:3,Four: 4, Five:5,Six : 6,Seven:7,Eight:8,Nine:9,Ten:10,Jack :11,Queen:12 , King:13,Ace : 14} ) ; //定義用一描述牌面的文本 Card.prototype.toString = function(){this.rank.toString( ) + this.suit.toString(); }; //比較撲克牌中兩張牌的大小 Card.prototype.compareTo = function( that ){if(this.rank<that.rank ) return -1;if(this.rank > that.rank ) return 1;return 0; }; //以撲克牌的玩法規(guī)則對牌進(jìn)行排序的函數(shù) Card.orderByRank = function(a,b){ return a.compareTo(b)};//以橋牌的玩法規(guī)則對撲克牌進(jìn)行排序的函數(shù) Card.orderBySuit = function(a,b){if(a.suit < b.suit ) return -1;if(a.suit > b.suit ) return 1;if(a.rank < b.rank ) return -1;if(a.rank > b.rank ) return 1;return 0; };//定義用以表示一副標(biāo)準(zhǔn)撲克牌的類 function Deck(){var cards = this.cards =[]; //一副牌就是有牌組成的數(shù)組Card.Suit.foreach(function(s){ //初始化這個數(shù)組Card.Rank.foreach( function(r){ cards.push(new Card(s,r));});}); }//洗牌的方法:重新洗牌并返回洗好的牌 Deck.prototype.shuffle = function(){//遍歷數(shù)組匯總的每個元素,隨機(jī)找出牌面最小的元素,并與之(當(dāng)前遍歷的元素)交換var deck = this.cards,len = deck.length;for( var i = len-1 ; i>0 ; i--){var r = Math.floor(Math.random()*(i+1)),temp; //隨機(jī)蘇temp = deck[i],deck[i] = deck[i] ,deck[r] = temp; //交換 }return this; };//發(fā)牌的方法:返回牌的數(shù)組 Deck.prototype.deal = function(n){if(this.cards.length < n ) throw "Out of cards";return this.cards.splice(this.cards.length-n,n); };Deck.prototype.toString = function(){var s = "";for( var x in this.cards){console.log(this.cards[x].suit);console.log(this.cards[x].rank);s += "{"+this.cards[x].suit.toString()+" "+this.cards[x].rank.value+"} ";}return s; };// 創(chuàng)建一副新?lián)淇伺?#xff0c;洗牌并發(fā)牌 var deck = (new Deck()).shuffle(); var hand = deck.deal(13).sort(Card.orderBySuit); deck.toString(); console.log(deck.toString()); //console.log(hand); // console.log(hand.name);</script>

?

轉(zhuǎn)載于:https://www.cnblogs.com/ctsch/p/6915334.html

總結(jié)

以上是生活随笔為你收集整理的JavaScript一个简易枚举类型实现扑克牌的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。