Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;

  • #2
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

    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.
    Last edited by Old Pedant; 11-30-2012 at 10:16 PM.
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so very much for your help! This cleared up a lot and I have the images showing with appropriate values.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •