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...
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.
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...
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
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.
<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. " ???
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