Using the flowchart in the previous post, I’ve started walking through how to build this program.
Step 1: Outline:
Hand Constructor
Card Constructor
getSuit (method to interpret suit numbers)
getValue(method to interpret value unbers)
Deal function
Main Function
Step2: Fleshing out early program (make myHand)
//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
getSuit (method to interpret suit numbers)
getValue(method to interpret value unbers)
Deal function
//Main Function
var myHand = new Hand();
The main function immediately generates a hand “myHand” for the player.
Going to the Hand constructor, we know that we need two cards, and that they will be made by calling the deal function. I’ve included a console.log to immediately provide feedback showing that the cards have correct properties (simple numbers right now)
Step3: Generating cards with the ‘Deal’ function
//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
getSuit (method to interpret suit numbers)
getValue(method to interpret value unbers)
//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);
};
//Main Function
var myHand = new Hand();
Deal is called twice from the Hand Constructor. We should flesh out the deal function now. This function generates two random numbers (‘suit’ : 1-4) and (‘number’ : 1-13).
As before, I’ve added some console.log statements to make sure this is working correctly. I include a statement about being in the deal function as a check for how the program is walking through its steps.
Problem of Scope: am I making variables with appropriate scope in this function?