Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2012, 07:30 PM   PM User | #1
Kayla12
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Kayla12 is an unknown quantity at this point
Javascript Help please!!

Hey all! I am having some trouble with my program for blackjack; my variables a,b,c and d will not store values from randomCard ( I use them later in the code to print out a card image on the screen). Does anyone see anything I might be overlooking? (randomCard DOES get a value from Deal(), which is a random number generator, I know this because randomC() utilizes randomCard).

Thanks for your time!


Code:
function NewGame(){
		player.length = 0;
		dealer.length = 0;
		playerTotal = 0;
		dealerTotal = 0;
		
		
		randomCard = Deal();
		a = randomCard;
		player [0] = randomC();

		
		randomCard = Deal();
		b = randomCard;
		player [1] = randomC();

		
		randomCard = Deal();
		c = randomCard;		
		dealer[0] = randomC();

		
		randomCard = Deal();
		d = randomCard;		
		dealer[1] = randomC();

		
		playerTotal = player [0] + player [1];
		dealerTotal = dealer [0] + dealer [1];
		document.form.player.value = playerTotal;
		document.form.dealer.value = dealerTotal;
Kayla12 is offline   Reply With Quote
Old 11-30-2012, 08:22 PM   PM User | #2
Kayla12
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Kayla12 is an unknown quantity at this point
I am putting the variables in the code shown below, if I enter a [0] or [1] the correct image shows up, but I need it to coincide with the the random variable instead of 0 and 1.

Code:
 <form NAME="form">
      <table height="165">
        <tr>
          <td height="137"><table width="814">
            <tr>
              <td><script>document.write('<img alt="random image" src="' + playerC[a] + '"/>');</script>
                <script>document.write('<img alt="random image" src="' + playerC[b] + '"/>');</script></td>
              <td><script>document.write('<img alt="random image" src="' + playerC[c] + '"/>');</script>
                <script>document.write('<img alt="random image" src="' + playerC[d] + '"/>');</script></td>
            </tr>
Kayla12 is offline   Reply With Quote
Old 11-30-2012, 09:03 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
This makes NO SENSE at all:
Code:
		randomCard = Deal();
		a = randomCard;
		player [0] = randomC();
You get a randomCard. Fine. Then, for some weird reason, you assign it to the variable a

Why didn't you simply code a = Deal();?

But then you set player[0] to the result of calling the function randomC()!

WHY???

*APPARENTLY* you have a Deal( ) function that returns one random card. That's great. But then why do you have *ANOTHER* function randomC() that does the same thing????

On top of that, you get the score for the player by adding player[0] and player[1]. But why in the world would *THAT* score match the sum of the values of a and b??? Except by the wildest coincidence?

*********

In your second post, you show this:
Code:
document.write('<img alt="random image" src="' + playerC[a] + '"/>');
Now, all of a sudden, you have introduced the array playerC[].

Where did *THAT* come from?

In the prior code, you had player[0] and player[1], but you certainly didn't have any playerC[ ].

On top of everything else, document.write is badly obsolete and you should *NOT* be using it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-30-2012, 09:47 PM   PM User | #4
Kayla12
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Kayla12 is an unknown quantity at this point
Deal() returns a value 0-51.
randomC() takes the RANDOMCARD and assigns it a card VALUE based on a case switch statement.

playerC[] are the cards, I have an array set up with 0-51 assigning a different card in the deck.

a & randomCard must be equal numbers so I assign the correct card, that is why I do not simply code a = Deal().

If document.write is so obsolete why not inform me of the appropriate code?

Thank you, I think.
Kayla12 is offline   Reply With Quote
Old 11-30-2012, 10:09 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay...begins to make more sense.

Your playerC is fine, though I would have used a more descriptive name. After all, you use those images for the dealer's cards, as well. Maybe cardImage[ ]?

But that's quibbling.

Quote:
randomC() takes the RANDOMCARD and assigns it a card VALUE based on a case switch statement.
Bad design. (Also misleading name.)

I would have written it as
Code:
function cardValue( card )
{
    ...
}
NEVER depend upon passing global variables around from function to function if you can help it!

If you were going to do that, you could have had Deal( ) set a global variable with the card's value. But even that is bad design.

*********

In place of document.write, you should use DOM methods.

For example:
Code:
              <td><img id="PC1" alt="player card 1"/></td>
              <td><img id="PC2" alt="player card 2"/></td>
              <td><img id="DC1" alt="dealer card 1"/></td>
              <td><img id="DC2" alt="dealer card 2"/></td>

....

<script type="text/javascript">
function dealTo( whom, imagePrefix )
{
    var total = 0;
    for ( var c = 0; c <= 1; ++c )
    {
        var card = Deal();
        total += cardValue( card );
        document.getElementById( imagePrefix + (c+1) ).src = playerC[card]; // I'd still rename playerC
        whom[c] = card;
    }
    return total;
}

function newGame( )
{
    playerCards = [];
    dealerCards = []; // better than setting length to zero...start with clean array

    playerTotal = dealTo( playerCards, "PC" );
    dealerTotal = dealTo( dealerCards, "DC" );
}
... other code ...
</script>
Yes, you should put the JS code *AFTER* all the HTML, just before the </body> tag.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 11-30-2012 at 10:16 PM..
Old Pedant is offline   Reply With Quote
Old 11-30-2012, 10:13 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
By the by, there is no need for a switch( ) in your cardValue( ) function (what you were calling playerC( ).

Assuming that 0-12 are the Ace through King of Clubs, 13-25 are Ace through King of Diamonds, etc.:
Code:
function cardValue( card )
{
    card %= 13; // reduce to 0 through 12
    if ( card >= 10 ) { return 10; } /* J, Q, K */
    if ( card == 0 ) { return 11; } /* Ace */
    return card+1; // all other cards
}
If you use some other numbering system for the card images, you can still do it without switch( ), almost surely.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-30-2012, 10:19 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh...and named <form>s are also obsolete. Give the form an ID, instead, and then use
Code:
     var form = document.getElementById("theForm");
or similar to refer to it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-01-2012, 12:19 AM   PM User | #8
Kayla12
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Kayla12 is an unknown quantity at this point
Thank you so very much for your help! This cleared up a lot and I have the images showing with appropriate values.
Kayla12 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:00 PM.


Advertisement
Log in to turn off these ads.