標籤:匯總 color .so ova text pre compareto math cto
<script type="text/javascript">/** * 這個函數建立一個新的枚舉類型,實參對象表示類的每個執行個體的名字和值 * 傳回值是一個建構函式,它標識這個新類 * 注意,這個建構函式也會拋出異常,不能使用它來建立該類型的新執行個體 * 返回的建構函式包含名/值對的映射表 * 包括由值組成的數組,以及以個foreach()迭代器函數 */function enumeration(namesToValues){ //這個虛擬建構函式式傳回值 var enumeration = function(){throw "Can‘t Instantiate Enumeration"}; //枚舉值繼承自這個對象 var proto = enumeration.prototype = { constructor:enumeration, //標識類型 toString : function(){ return this.name; }, //返回名字 valueOf : function(){ return this.value;}, //傳回值 toJSON : function(){ return this.name; }, //轉為JSON }; enumeration.values =[]; //用以存放枚舉對象的數組 //現在建立新類型的執行個體 for( name in namesToValues){ //遍曆每個值 var e = Object.create(proto); //建立一個代表它的對象 e.name = name ; //給它一個名字 e.value = namesToValues[name]; //給它一個值 enumeration[name] = e; //將它設定為建構函式的屬性 enumeration.values.push(e); //將它儲存到值數組中 } //一個類方法,用來對類的執行個體進行迭代 enumeration.foreach = function(f,c){ for(var i = 0 ; i<this.values.length; i++) f.call(c,this.values[i]); }; //返回表示這個新類型的建構函式 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 ; //以及點數}//使用枚舉類定義換色和點數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;};//以撲克牌的玩法規則對牌進行排序的函數Card.orderByRank = function(a,b){ return a.compareTo(b)};//以橋牌的玩法規則對撲克牌進行排序的函數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;};//定義用以表示一副標準撲克牌的類function Deck(){ var cards = this.cards =[]; //一副牌就是有牌組成的數組 Card.Suit.foreach(function(s){ //初始化這個數組 Card.Rank.foreach( function(r){ cards.push(new Card(s,r)); }); });}//洗牌的方法:重新洗牌並返回洗好的牌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[i] ,deck[r] = temp; //交換 } return this;};//發牌的方法:返回牌的數組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;};// 建立一副新撲克牌,洗牌並發牌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>
JavaScript一個簡易枚舉類型實現撲克牌