Blimey. Week 5 and onto objects. There was loads of new concepts here, so kudos to teach for getting it all across in an understandable way. I think there was some kind of beta testing involved before the exercises went out, so this probably went a long way to help polish it up - thanks to all that did it!
Won't go into detail about the exercises, other than a note to self to read the question properly. Sat at one point getting the right result (or so I thought) and the dreaded 'oops - try again' message only to realise after about half an hour of trying to do in different ways that I had not capitalised the first character of what was being output to console. Grrrr.
But objects and methods open up a MASSIVE new horizon. I'm going to need to try and find time to practice as have a feeling that a sound grasp of these concepts is going to be vital in going forwards. I'm definitely not there yet - had to revisit the courses in order to create the below - a proper deck of cards!
//Create constructor
function Card (value, suit, name) {
this.value = value;
this.suit = suit;
this.name = name;
}
//Get card value for purpose of BlackJack
var getCardValue = function(cardNumber) {
switch(cardNumber) {
case 0: return 10;
case 11: return 10;
case 12: return 10;
default: return cardNumber;
}
};
//Get card suit
var getCardSuit = function(cardQuadrant) {
switch (cardQuadrant) {
case 1: return "Heart";
case 2: return "Club";
case 3: return "Diamond";
case 4: return "Spade";
default: return "Error - cardQuadrant not 1-4!";
}
};
//Get name of card (UK standard!)
var getCardName = function(cardNumber) {
switch (cardNumber) {
case 0: return "Jack";
case 1: return "Ace";
case 11: return "Queen";
case 12: return "King";
default: return cardNumber;
}
};
//Get which 'quadrant' the card is in
var getCardQuad = function(cardPosition) {
return Math.floor((cardPosition/13)+1);
};
//Get position of card within that quadrant
var getCardNum = function(cardPosition) {
return cardPosition % 13;
};
//Create new 'deck' array in global scope
var deck = new Array();
//cycle through numbers 0 to 51 and populate array with Card objects
for (i=0; i<52; i++) {
var cardNum = getCardNum(i);
var cardQuad = getCardQuad(i);
deck[i] = new Card(getCardValue(cardNum), getCardSuit(cardQuad),
getCardName(cardNum));
}
//Cycle through card objects in array and check that they are correctly populated
for (i=0; i<52; i++) {
console.log(deck[i].name + " of " + deck[i].suit +"s has a value of " + deck[i].value);
}
Code can also be seen here): http://labs.codecademy.com/JYb#:workspace
There's loads of potential practice here:
- Shuffling - loads of different types to model to practice array manipulation (http://en.wikipedia.org/wiki/Shuffling)
- Dealing into hand arrays
- Method on card object to change value of Ace
- Pots, hitting, sticking, twisting, splitting, winning hands, moving money from player to player - actually, loads of stuff!
If anyone wants to have a punt at some interesting shuffle functions, or the method on the card object to change Ace value (with user prompts and input validation), or anything else, please post and I'll put up on the site. Only rules are:
- Clarity over efficient code - I want to be able to understand it! Commented code also good
- Use only functions/concepts taught by codecademy so far (see previous post)
Also - comments on my code are areas for improvement greatly welcomed!