欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript学习笔记整理_简单实现枚举类型,扑克牌应用

程序员文章站 2023-10-26 20:49:28
如下所示: //实现枚举类型,扑克牌应用 function createnum(p){ //构造函数 var enumeration = funct...

如下所示:

//实现枚举类型,扑克牌应用
function createnum(p){
  //构造函数
  var enumeration = function(){throw 'can not instantiate enumerations';};
  //重写原型并将原型赋值给变量proto
  var proto = enumeration.prototype = {
    constructor:enumeration,
    tostring:function(){return this.name;},
    valueof:function(){return this.value;},
    tojson:function(){return this.name;}
  };
  //添加类属性,方法
  enumeration.values = [];
  for(var n in p){ //将对象p的每个元素都单独转存到一个单独的对象o里面,并将这些对象o存入类属性values数组中
    var o = object.create(proto); //对象o继承了enumeration的3个实例方法和构造函数
    enumeration.prototype.valueof = function(){return this.value*1;}; //重写原型的valueof方法
    o.name = n;
    o.value = p[n];
    enumeration[n] = o; //添加类属性name,值为对象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);
/*结果:枚举对象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: 250] name: 'quarter', value: 25 },
 foreach: [function] }
 */
console.log(coin.dime+2); //102 coin.dime本身继承自枚举对象,继承并修改了valueof方法用来将value转化为数字做计算
 
//===使用函数createnum()来表示一副54张的扑克牌==
function card(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,ace: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;
};
//定义一副标准扑克牌
function deck(){
  var cards = this.cards = [];
  card.suit.foreach(function(s){ //对每个花色执行
    if(s!=5) {
      card.rank.foreach(function (r) {
        if (r != 16 && r != 17) {
          cards.push(new card(s, r));
        }
      });
    }else{
      card.rank.foreach(function (r){
        if(r == 16) cards.push(new card(s, r));
        if(r == 17) 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[r], 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);
};
//开始:
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);
}

以上就是小编为大家带来的javascript学习笔记整理_简单实现枚举类型,扑克牌应用的全部内容了,希望对大家有所帮助,多多支持~