虽然说是从美国传过来的,不过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!");
冷风吹得头晕脑胀,,,,,,我还有得学呢