Step 6: You get a hand, Determining scores and judging winner
Here are the last touches that make this code into the bare-bones structure of the game. Cards are dealt out to two hands (myHand and yourHand), each card is given a suit and a value and the two hands are compared to determine a winner. (I haven’t added anything about ‘Hitting’ or ‘Holding’, and since you start with only two cards, you can’t go over 21. I’m assuming that these things are all part of the final project).
//Hand Constructor
var Hand = function(){
this.card1 = deal();
//console.log(“card1 is “+this.card1.number+” of ” + this.card1.suit);
this.card2 = deal();
//console.log(“card2 is “+this.card2.number+” of ” + this.card2.suit);
};
//Card Constructor
var Card = function(s,n){
this.number = n;
this.suit = s;
this.getSuit = function(s){
//console.log(“I’m here in getSuit”);
switch(this.suit){
case 1:
this.suit = “clubs”;
break;
case 2:
this.suit = “diamonds”;
break;
case 3:
this.suit = “hearts”;
break;
case 4:
this.suit = “spades”;
break;
}
};
this.getValue = function(n){
// console.log(“I’m here in getValue”);
if (n === 1){
this.value = 11;
}
else if (n >= 11){
this.value = 10;
}
else{
this.value = n;
}
};
};
//Deal function
var deal = function(){
var suit = Math.floor(Math.random()*4)+1;
//console.log(“deal function suit: “+suit);
var number = Math.floor(Math.random()*13)+1;
//console.log(“deal function number: “+number);
return new Card(suit,number);
};
//Score function
var score = function(one, two){
return one+two;
};
//comparison of hands
var judgement = function(mine,yours){
if (mine>yours){
console.log(“I win “+mine+ ” vs “+yours);
}
else if (yours>mine){
console.log(“You win “+mine+ ” vs “+yours);
}
else{
console.log(“We tied “+mine+ ” vs “+yours);
}
};
//Main Function
var myHand = new Hand();
myHand.card1.getSuit(myHand.card1.suit);
myHand.card1.getValue(myHand.card1.number);
console.log(“my card1 is a “+myHand.card1.suit+” of value “+myHand.card1.value);
myHand.card2.getSuit(myHand.card2.suit);
myHand.card2.getValue(myHand.card2.number);
console.log(“my card2 is a “+myHand.card2.suit+ ” of value “+myHand.card2.value);
var yourHand = new Hand();
yourHand.card1.getSuit(yourHand.card1.suit);
yourHand.card1.getValue(yourHand.card1.number);
console.log(“your card1 is a “+yourHand.card1.suit+” of value “+yourHand.card1.value);
yourHand.card2.getSuit(yourHand.card2.suit);
yourHand.card2.getValue(yourHand.card2.number);
console.log(“your card2 is a “+yourHand.card2.suit+ ” of value “+yourHand.card2.value);
var myScore = score(myHand.card1.value,myHand.card2.value);
var yourScore = score(yourHand.card1.value,yourHand.card2.value);
judgement(myScore,yourScore);