Quotes help make search much faster. Example: "Practice Makes Perfect"

Monday, December 10, 2012

Codeyear: Casino Dealer


//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.hitMe = function(){
  if(this.score===21){
   console.log("You already have a score of 21!");
  } else {
   handArray.push(deal());
  }
 };
 this.printHand = function(){
  suitsArray = ["clubs","diamonds","hearts","spades"];
  numberArray = ["ace",2,3,4,5,6,7,8,9,10,"jack","queen","king"];
  var tempString = "";
  for(var i=0; i<handArray.length; i++){
   tempString += numberArray[handArray[i].getNumber()-1]+" of "+suitsArray[handArray[i].getSuit()-1]+", ";
  }
  return tempString;
 };
 this.score = function(){
  scoresum = 0;
  for(var i=0; i<handArray.length; i++){
   if(handArray[i].getValue()===11){
    //console.log("We have an ace case:");
    if(handArray[i].getValue()+scoresum>21){
     scoresum += 1;
    } else {
     scoresum += handArray[i].getValue();
    }
   } else {
    scoresum += handArray[i].getValue();
   }
  }
  return scoresum;
 };
}

function playAsDealer(){
var newHand = new Hand();
while(newHand.score()<17){
newHand.hitMe();
}
return newHand;
}

var newHand = playAsDealer();
console.log("Playing as a dealer:");
console.log(newHand.printHand());
console.log("The score of the hand is "+newHand.score()+".");

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts