//sample solution
// Card Constructor
function Card(num1,num2) {
//properties
var suit = num1;
var number = num2;
//methods
this.getNumber = function(){
return number;
};
this.getSuit = function(){
return suit;
};
this.getValue = function(){
if(this.getNumber()>=10){
return 10;
} else if(this.getNumber() === 1){
return 11;
} else {
return number;
}
};
}
function deal(){
var newSuit = Math.floor(Math.random()*4+1);
var newNumber = Math.floor(Math.random()*13+1);
var newCard = new Card(newSuit,newNumber);
return newCard;
}
function Hand(){
//properties
var newCard1 = deal();
var newCard2 = deal();
var handArray = [newCard1, newCard2];
//methods
this.getHand = function(){
return handArray;
};
this.score = function(){
scoresum = 0;
for(var i=0; i<handArray.length; i++){
scoresum += handArray[i].getValue();
}
return scoresum;
};
}
var newHand = new Hand();
var tempArrayOfCards = newHand.getHand();
console.log(tempArrayOfCards[0].getSuit());
console.log(tempArrayOfCards[0].getNumber());
console.log(tempArrayOfCards[0].getValue());
console.log(tempArrayOfCards[1].getSuit());
console.log(tempArrayOfCards[1].getNumber());
console.log(tempArrayOfCards[1].getValue());
console.log("The score of the hand is "+newHand.score()+".");
No comments:
Post a Comment