CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Javascript Help please!! (http://www.codingforums.com/showthread.php?t=283304)

Kayla12 11-30-2012 07:30 PM

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 11-30-2012 08:22 PM

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>


Old Pedant 11-30-2012 09:03 PM

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.

Kayla12 11-30-2012 09:47 PM

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.

Old Pedant 11-30-2012 10:09 PM

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.

Old Pedant 11-30-2012 10:13 PM

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.

Old Pedant 11-30-2012 10:19 PM

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.

Kayla12 12-01-2012 12:19 AM

Thank you so very much for your help! This cleared up a lot and I have the images showing with appropriate values. :thumbsup:


All times are GMT +1. The time now is 03:52 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.