每天进步一点点-------JS之21点

虽然说是从美国传过来的,不过21点在天朝还是很有名气的,重点是规则有些区别,我上维基百科看了一下才了解,这个脚本有点复杂,大概因为最近一直在看python所以JS有点记混了,反正做完了还是没完全弄清楚,哎,慢慢想呗

下面是代码


function Card(n,s) {
        var number = n;
        var suit = s;
        //getters
        this.getNumber = function(){          //最想不通的地方就是这里,前面题目要求说不能用this需要用var来保证不作弊,但是后面如果不能调用这几个对象就会 
                return number;                  报错
        };
        this.getSuit = function(){
                return suit;
        };
        this.getValue = function(){
            if (number > 10 || number === 0){                //规定如果是J,Q,K就统一算作10点
                return 10;
            }
            else if (number ===1){                           //为了方便计算,A算作11点
                return 11;
            }
            else{
                return number;
            }
        };
}

// Make a deal function here.  It should return a new card with a suit

// that is a random number from 1 to 4, and a number that is a random
// number between 1 and 13
var deal = function(){
        randNum = Math.floor(Math.random() * 13) + 1;         
        randSuit = Math.floor(Math.random() * 4) + 1;
        //the card
        return new Card(randNum, randSuit);
};
// examples of the deal function in action

function Hand(){                                 //定义一个随机的点数
    this.card1 = deal();
    this.card2 = deal(); 
    this.score = function(){
        var score1 = this.card1.getValue()
        var score2 = this.card2.getValue()
        return score1 + score2
    };
}

var myHand = new Hand();
var yourHand = new Hand();

console.log("I scored a "+myHand.score()+" and you scored a "+
yourHand.score());

if(yourHand.score() > myHand.score())
console.log("you win!");
else if(yourHand.score() < myHand.score())
console.log("I win!");
else
console.log("We tied!");

冷风吹得头晕脑胀,,,,,,我还有得学呢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值