RSS

Tag Archives: blackjack

Stuck at 98%

I’ve been procrastinating – with a lot of things really.

The one that brought me to writing this is that I am 98% finished the codecademy Javascript course with only the Blackjack game remaining. And I’ve already put a lot into that as well. I just need to get off my lazy ass and finish it. Instead, I’ve allowed myself to get distracted by the new courses in python and ruby. I think I’m going to have to set aside a night and get this finished though.

The problem has been that I put javascript aside for long enough that I’m starting to forget the syntax. I think tonight I’ll print out a couple of my old syntax guides (perhaps update them and post them here) and get back in the saddle.

I also have an ePublishing workshop coming up this Thursday. My (biology) class has been given the day off and I’ve hired a babysitter so that I can go. In my mind it is a good way to network with some people who are interested in ePublishing but not comfortable enough to jump in and hopefully learn some things too.

I’ll be taking the iPad with The Thirteenth Labor of Heracles as a demonstration of a finished product and I’m hoping to have The Curse of Sisyphus far enough along that it can be demonstrated as well. I like the way The Curse is coming along – it incorporates more self-testing opportunities than the Thirteenth Labor. I’m also looking into how to develop my own widgets (another project! Great!), but realistically, I recognize that this is probably a long way off for me. I would love to be able to develop a more versatile version of the interactive image widget to use with mathematical equations.

Oh right, I’m also supposed to preparing for an exam! I’ve written all of about ten questions so far and haven’t even looked at my Jeopardy – review yet. And then the lab… damn. I did make that promise didn’t I?

Well, let’s just see how things go one day at a time.

One last thing: My wife is away for what amounts to two weeks. Day one – two minutes after she left: Dingleberry on the dog. I don’t think the cat has realized what’s up yet because I haven’t found any surprises.

 
1 Comment

Posted by on October 16, 2012 in Uncategorized

 

Tags: , , , , , , , , , , , ,

Finishing Touches

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);

 

Tags: , , , ,

Adding the Methods

Step5: Adding methods to interpret actual suit and value of cards

 

//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);

};

 

//Main Function

var myHand = new Hand();

myHand.card1.getSuit(myHand.card1.suit);

myHand.card1.getValue(myHand.card1.number);

console.log(“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(“card2 is a “+myHand.card2.suit+ ” of value “+myHand.card2.value);

 

 

In this section I added two methods (these are just functions that handle objects). Methods are placed inside of the objects they work on (is this right?Always?). In this program my methods interpret the suit and number variables as the suit that the number represents (clubs, diamonds, etc) and the value of the card (ace = 11, face cards are all worth 10 and numbered cards are worth their numerical value).

As usual, I have included a couple console.log statements so that I could track where problems cropped up along the way.

 

 

Tags: , , , , , ,

Getting Started with rebuilding blackjack from scratch with notes (help requested)

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?

 

 
Leave a comment

Posted by on July 24, 2012 in Codecademy, Coding

 

Tags: , , , , , ,

Blackjack Flow

In order to make sense of the blackjack project, I made up this flow diagram to describe how the program creates ‘Hand’ objects consisting of two ‘card’ objects, and that these cards have properties that need to be generated and interpreted. The two properties are number (1-13, where 1 is Ace, 11 is Jack, 12, is Queen, etc) and suit (1-4, where each number is a given suit).

The properties of the cards can be set using a function, deal.

Once cards are made, they use two methods (because we are handling objects) to interpret the suit and value of each card.

Finally, we have two cards in a hand and can proceed to the next step…

Here’s a flow diagram of this process:Image

 
1 Comment

Posted by on July 24, 2012 in Codecademy, Coding

 

Tags: , , , ,

Blackjack game is driving me crazy

I’ve been working on the blackjack name for codecademy all day today without luck.  The challenge / puzzle / assignment is to have a hand of cards generated and scored.

The hand is an object that includes two ‘card’ objects. each card is generated randomly with two features (number and suit*).The suit is defined by a random number 1-4 and defined by a method for matching this numeral to a suit. The number is defined as 1-13 and set to values such that 1 (ace) = 11, 1-10 =face value and 11-13 are face cards worth 10pts each.

 

I simply can’t do it. My code looks reasonable to me (but I don’t really feel comfortable with it at all), but I’m getting crazy results. Here’s what I have, try copying it into a javascript field and see if you can make heads or tails of it:

 

// Make your card constructor again here, but make sure to use private
// variables!
function Card(s,n){
this.number = n;
this.suit = s;

this.getSuit = function(suit){
//console.log(“I’m about to switch”);
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(number){
if(this.number === 1){
//console.log(“value = 11”);
this.value = 11;
return 11;
}
else if (this.number >=11){
//console.log(“value = 10”);
this.value = 10;
return 10;
}
else{
//console.log(“value = “+ this.number);
this.value = number;
return this.number;
}
};
};

// Make a deal function here. It should return a new card with a suit
// that is a random number from 1 to 4, and a number that is a random
// number between 1 and 13
var deal = function(){
var suit = Math.floor(Math.random()*4)+1;
console.log(suit);
var number = Math.floor(Math.random()*13)+1;
console.log(number);
return new Card(suit,number);
};

var Hand = function(){
this.card1 = deal();
console.log(“card1 is “+card1.number+ ” of “+card1.suit);
this.card2 = deal();
console.log(“card2 is “+card2.number+ ” of “+card2.suit);
};

Hand();
console.log(Hand);

 

 

*by the way, it is clear that there is nothing that prevents the same card from being drawn twice from what presumably is a single deck.

 
Leave a comment

Posted by on July 24, 2012 in Codecademy, Coding

 

Tags: , , , , , ,