Current location: Hot Scripts Forums » General Web Coding » JavaScript » Variables...


Variables...

Reply
  #1 (permalink)  
Old 07-13-06, 08:56 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
Question Variables...

Hello all! I am a php programmer. I dont know javascript too well so i thought i would post my problem on here. What i have is a blackjack script. I want the hand to be either all set ($card1 $card2 $card3... ect for 8 cards) and hide them all until they hit the "Hit Meh" button and then one more shows... i think with div tags... or my second choice was to just have the hand.. and each time they hit the hit meh button it would insert a card into the hand... i think the div tag would be easier but also easier to cheat by just looking at the source... any ideas?

EDIT: Also I don't want the page to refresh, but have the hand be updated when the button is clicked...

Last edited by stormshadow; 07-13-06 at 09:03 PM.
Reply With Quote
  #2 (permalink)  
Old 07-13-06, 10:30 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Do you have any code to begin with? If you do, it would be much easier to give a suitable example of how to do this with actual code.
Is this going to be in a commercial application? If so, don't use JavaScript since you will be guaranteed to have cheaters.

I think I would do it by having an array containing all the cards to begin with. Then select and remove one of them at random each time a card is dealt.
If it's your card, simply load the corresponding image into a div container.

All the divs would be "visible" at all times, representing possible card positions, but those without a card are left empty. Img tags could also be used if you have a transparent image show when no card is at that position.

The "Hit Me" button would simply call a function which (if allowed) selects a new random card from the deck array, checks which position it should go into and loads the image into the corresponding div or img tag. When cards are removed, you'd just need to clear the placeholder tags and recreate the deck array.

There would be no page refresh since that would destroy any JavaScript on that page.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #3 (permalink)  
Old 07-14-06, 11:40 AM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
exactly what i want!!!!!

except i dont know how to use the div tag thingy....

PHP Code:

<?

function get_total($c1){
switch(
$c1){
//aces
case "Ace of Hearts":
    
$c1points 11;
    break;
case 
"Ace of Clubs":
    
$c1points 11;
    break;
case 
"Ace of Spades":
    
$c1points 11;
    break;
case 
"Ace of Diamonds":
    
$c1points 11;
    break;
//twos
case "Two of Hearts":
    
$c1points 2;
    break;
case 
"Two of Clubs":
    
$c1points 2;
    break;
case 
"Two of Spades":
    
$c1points 2;
    break;
case 
"Two of Diamonds":
    
$c1points 2;
    break;
//threes
case "Three of Hearts":
    
$c1points 3;
    break;
case 
"Three of Clubs":
    
$c1points 3;
    break;
case 
"Three of Spades":
    
$c1points 3;
    break;
case 
"Three of Diamonds":
    
$c1points 3;
    break;
//fours
case "Four of Hearts":
    
$c1points 4;
    break;
case 
"Four of Clubs":
    
$c1points 4;
    break;
case 
"Four of Spades":
    
$c1points 4;
    break;
case 
"Four of Diamonds":
    
$c1points 4;
    break;
//fives
case "Five of Hearts":
    
$c1points 5;
    break;
case 
"Five of Clubs":
    
$c1points 5;
    break;
case 
"Five of Spades":
    
$c1points 5;
    break;
case 
"Five of Diamonds":
    
$c1points 5;
    break;
//sixes
case "Six of Hearts":
    
$c1points 6;
    break;
case 
"Six of Clubs":
    
$c1points 6;
    break;
case 
"Six of Spades":
    
$c1points 6;
    break;
case 
"Six of Diamonds":
    
$c1points 6;
    break;
//sevens
case "Seven of Hearts":
    
$c1points 7;
    break;
case 
"Seven of Clubs":
    
$c1points 7;
    break;
case 
"Seven of Spades":
    
$c1points 7;
    break;
case 
"Seven of Diamonds":
    
$c1points 7;
    break;
//eights
case "Eight of Hearts":
    
$c1points 8;
    break;
case 
"Eight of Clubs":
    
$c1points 8;
    break;
case 
"Eight of Spades":
    
$c1points 8;
    break;
case 
"Eight of Diamonds":
    
$c1points 8;
    break;
//Nines
case "Nine of Hearts":
    
$c1points 9;
    break;
case 
"Nine of Clubs":
    
$c1points 9;
    break;
case 
"Nine of Spades":
    
$c1points 9;
    break;
case 
"Nine of Diamonds":
    
$c1points 9;
    break;
//everything else
default:
    
$c1points 10;
    break;
}
//switch
return $c1points;
}
// Set bet
$bet 500;
//set cards as an array
$card = array("Ace of Hearts","Two of Hearts","Three of Hearts","Four of Hearts","Five of Hearts","Six of Hearts","Seven of Hearts","Eight of Hearts","Nine of Hearts","Ten of Hearts","Jack of Hearts","Queen of Hearts","King of Hearts","Ace of Diamonds","Two of Diamonds","Three of Diamonds","Four of Diamonds","Five of Diamonds","Six of Diamonds","Seven of Diamonds","Eight of Diamonds","Nine of Diamonds","Ten of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds","Ace of Clubs","Two of Clubs","Three of Clubs","Four of Clubs","Five of Clubs","Six of Clubs","Seven of Clubs","Eight of Clubs","Nine of Clubs","Ten of Clubs","Jack of Clubs","Queen of Clubs","King of Clubs","Ace of Spades","Two of Spades","Three of Spades","Four of Spades","Five of Spades","Six of Spades","Seven of Spades","Eight of Spades","Nine of Spades","Ten of Spades","Jack of Spades","Queen of Spades","King of Spades");
//Set dealer cards..
shuffle($card); // randomize the array 
$dcard1 array_pop($card); // get and remove the last element in the array 
$dcard2 array_pop($card); // get the next card 
    //get dealer total
    
$dealer get_total($dcard1) + get_total($dcard2);
//player
$pcard1 array_pop($card);
$pcard2 array_pop($card);
    
//get player total
    
$player get_total($pcard1) + get_total($pcard2);
echo
"Dealer: $dcard1, Hidden<br>";
echo
"Your Hand: Div tags should go here";
if(
$player==21){
    
$totalwinnings $bet 2;
    echo
"BLACKJACK! You win double your bet! (<b>$totalwinnings</b>) <a href=blackjack.php?step=play&bet=$bet>Play again?</a>";
}elseif(
$dealer==21){
    echo
"Sorry, the dealer got blackjack. You lose. <a href=blackjack.php>Play again?</a>";
}else{
    
// get dealers full hand.
    
while($dealer<16){
        
$pop array_pop($card);
        
$dealer get_total($pop) + $dealer;
    }
//while dealer
    
if($dealer>21){
        
$totalwinnings $bet 2;
        echo
"Dealer busted! You win double your bet! ($totalwinnings)! <a href=blackjack.php>Play again?</a>";
    }
    echo
'<input name="" type="button" value="Hit Meh" onclick="hit();"/>';
}
?>
On this line i want to have the hand...
PHP Code:

echo"Your Hand: Div tags should go here"
so i would need 8 div tags because thats the max ammount of cards you can have in one game... and on this line...

PHP Code:

echo'<input name="" type="button" value="Hit Meh" onclick="hit();"/>'
that would call the php function hit. and i dont know how to do that with javascript...
Reply With Quote
  #4 (permalink)  
Old 07-14-06, 12:15 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
okay.. i think i got some of it...

but it says that three is undefined and im assuming the rest also...?

Code:
if(document.getElementById(three).stlye.classname=="show"){ var whichone = "three"; }
if(document.getElementById(four).stlye.classname=="show"){ var whichone = "four"; }
if(document.getElementById(five).stlye.classname=="show"){ var whichone = "five"; }
if(document.getElementById(six).stlye.classname=="show"){ var whichone = "six"; }
if(document.getElementById(seven).stlye.classname=="show"){ var whichone = "seven"; }
if(document.getElementById(eight).stlye.classname=="show"){ var whichone = "eight"; }
thats the javascript (under the script tag)

PHP Code:

echo"Your Hand: <table border=\"0\">

  <tr>
    <td>
$card1</td>
    <td>
$card2</td>
    <td><div id=three class=hide>
$card3</div></td>
    <td><div id=four class=hide>
$card4</div></td>
    <td><div id=five class=hide>
$card5</div></td>
    <td><div id=six class=hide>
$card6</div></td>
    <td><div id=seven class=hide>
$card7</div></td>
    <td><div id=eight class=hide>
$card8</div></td>
  </tr>
</table>"

Reply With Quote
  #5 (permalink)  
Old 07-14-06, 04:56 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
When you supply an id to getElementById() you must put it in quotes, otherwise it will be treated as a variable. And the variables three, four, five.... are undefined.
You have a typo on all the lines, stlye should be style.

The button would call the javascript function called hit, not a php function.
I guess the next line in the js code would be
Code:
document.getElementById(whichone).style.classname="show"
Then you need some code to check the value of that card and compare it to the player's total, and also to the dealer's total to know who won.
The easiest way to do this would be to print the current scores of the dealer and the player in JS variables, along with the values of the next 6 cards for the player in an array.
Each time a card is shown, you take the next value from the array and adds it to the player total and do the comparison with the dealer.

However, I think it would be best to handle all of this serverside, since it's very easy to just modify the values by typing something like javascript:void(playerScore=21) (or whatever you name the player score variable) in the address field and always win.
You could avoid refreshing the page by using AJAX to request the php file again, this time with a "hitme" querystring or something else to let php know what it's supposed to do. Then you take the response and parse it to know which new card you got (and also its points, if you're bust, got BJ, or you're not allowed more cards).
That way the server would keep track of the game and a player has no access to the cards and score numbers.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #6 (permalink)  
Old 07-14-06, 11:15 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
hehe lol thanks but

now why isnt this working????

Code:
<script type="text/javascript">
document.write('<style type="text/css"> \\n');
document.write('.show{display: block;} \\n');
document.write('.hide{display: none;} \\n');
document.write('</style> \\n');

function hit(){
if(document.getElementById("three").className=="hide"){ var whichone = "three"; }
else if(document.getElementById("four").className=="hide"){ var whichone = "four"; }
else if(document.getElementById("five").className=="hide"){ var whichone = "five"; }
else if(document.getElementById("six").className=="hide"){ var whichone = "six"; }
else if(document.getElementById("seven").className=="hide"){ var whichone = "seven"; }
else if(document.getElementById("eight").className=="hide"){ var whichone = "eight"; }
var tag = document.getElementById(whichone);
	tag.className = "show"; 
}
</script>
but it gives me an error saying something like "unexpected end of file while searching for closing } of a ruleset" and another 3 of "Unexpected end of file while searching for ',' or ''{' Ruleset ignored due to bad selector. " ???
Reply With Quote
  #7 (permalink)  
Old 07-16-06, 12:51 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
I've had some problems with writing stylesheets to pages, they simply won't apply unless you write a character before the style tag. A blank character won't work, but you can put a div around it and change make it invisible.
Try
Code:
document.write('<div style=\'display:none;\'>.</div><style type="text/css"> \\n');
It might work...
But why use JS to write out the stylesheet in the first place?
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Server side variables Puno PHP 13 08-13-05 10:22 PM
using variable variables with multiple variables for parts of the name harlock PHP 7 07-26-05 07:35 PM
recording variables wings PHP 4 03-18-05 05:12 PM
help with displaying variables tisza PHP 9 12-22-03 05:08 PM
adding more variables to the mail() result spinicrus PHP 6 08-28-03 04:55 PM


All times are GMT -5. The time now is 11:14 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.