Ok, gonna paste my code because I don't see what I'm missing here.
I'm still getting the error from this line:

$cardstring .= ", " . $card;

It Gives me this output after hitting the hit me button. The deal link works fine.

Notice: Undefined variable: cardstring in C:\wamp\www\HitMe.php on line 55

You Busted: GAME OVER!

Your hand: 3S, 8C, AH

Hand Value: 22

Hit Me!

New Game!

Deal.php

<?php
	session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Black Jack</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$CardDeck = array("2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH", //Deck of cards
				  "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
				  "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS",
				  "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD");

shuffle($CardDeck); //shuffles card deck

$DeckStore = fopen("blackjack.txt", "w"); //Open blackjack.txt
foreach($CardDeck as $key => $value) { 
fwrite($DeckStore, $value.", ");//Writes the Deck of shuffled cards into blackjack.txt on the first line in the format (card, card)
} 
fclose($DeckStore); //Closes blackjack.txt

$CardValue = 0; //if...else.if statement to set the value of the 1st card in the deck of the array
if (strpos($CardDeck[0], "2") !== FALSE)
	$CardValue += 2;
else if (strpos($CardDeck[0], "3") !== FALSE)
	$CardValue += 3;
else if (strpos($CardDeck[0], "4") !== FALSE)
	$CardValue += 4;
else if (strpos($CardDeck[0], "5") !== FALSE)
	$CardValue += 5;
else if (strpos($CardDeck[0], "6") !== FALSE)
	$CardValue += 6;
else if (strpos($CardDeck[0], "7") !== FALSE)
	$CardValue += 7;
else if (strpos($CardDeck[0], "8") !== FALSE)
	$CardValue += 8;
else if (strpos($CardDeck[0], "9") !== FALSE)
	$CardValue += 9;
else if (strpos($CardDeck[0], "10") !== FALSE)
	$CardValue += 10;
else if (strpos($CardDeck[0], "J") !== FALSE)
	$CardValue += 10;
else if (strpos($CardDeck[0], "Q") !== FALSE)
	$CardValue += 10;
else if (strpos($CardDeck[0], "K") !== FALSE)
	$CardValue += 10;
else if (strpos($CardDeck[0], "A") !== FALSE)
	$CardValue += 11;
else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}

$CardValue1 = 0; //if...else.if statement to set the value of the 2nd card in the deck of the array
if (strpos($CardDeck[1], "2") !== FALSE)
	$CardValue1 += 2;
else if (strpos($CardDeck[1], "3") !== FALSE)
	$CardValue1 += 3;
else if (strpos($CardDeck[1], "4") !== FALSE)
	$CardValue1 += 4;
else if (strpos($CardDeck[1], "5") !== FALSE)
	$CardValue1 += 5;
else if (strpos($CardDeck[1], "6") !== FALSE)
	$CardValue1 += 6;
else if (strpos($CardDeck[1], "7") !== FALSE)
	$CardValue1 += 7;
else if (strpos($CardDeck[1], "8") !== FALSE)
	$CardValue1 += 8;
else if (strpos($CardDeck[1], "9") !== FALSE)
	$CardValue1 += 9;
else if (strpos($CardDeck[1], "10") !== FALSE)
	$CardValue1 += 10;
else if (strpos($CardDeck[1], "J") !== FALSE)
	$CardValue1 += 10;
else if (strpos($CardDeck[1], "Q") !== FALSE)
	$CardValue1 += 10;
else if (strpos($CardDeck[1], "K") !== FALSE)
	$CardValue1 += 10;
else if (strpos($CardDeck[1], "A") !== FALSE)
	$CardValue1 += 1 || 11;
else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}

$HandTotal = ($CardValue + $CardValue1); //Calculates value of the hand

echo "<p>Your hand: " . $CardDeck[0] . " , " . $CardDeck[1] . "</p>";
echo "<p>Hand Value: " . $HandTotal . "</p>";

$CardDeck = array($CardDeck[0],$CardDeck[1]);

$_SESSION['carddeck'] = $CardDeck;
$_SESSION['handtotal'] = $HandTotal;

if($HandTotal == 21)
	echo "<strong>You were dealt: BLACK JACK!</strong>"; //message if the hand == 21 upon being dealt
?>
<p><a href="HitMe.php">Hit Me!</a></p> <!-- Link to HitMe.php -->
<p><a href="Stand.php">Stand!</a></p> <!-- Link to Stand.php -->
<hr />
<p><a href="Deal.php">New Game!</a></p> <!-- Link to Deal.php -->
</body>
</html>

HitMe.php

<?php
session_start();
if(isset($_SESSION['carddeck']))$CardDeck = $_SESSION['carddeck'];
if(isset($_SESSION['handtotal']))$HandTotal = $_SESSION['handtotal'];

if (!file_exists("blackjack.txt") || filesize("blackjack.txt") == 0) //check if the file exists if not, error message, if so gets the information and explodes into an array
	echo "<p>There is not a deck of cards to play with!.</p>";
else {
	$HandArray = file("blackjack.txt");
		for ($i=0; $i<count($HandArray); ++$i) {
			$Cards = explode(", ", $HandArray[$i]);
		}
}

$Card_position = count($CardDeck);

if($Card_position < 5) { //Sets maximum draw to 5 (BlackJack rules)
	array_push($CardDeck, $Cards[$Card_position]);
	
	$CardValue = 0; //if...else.if statement to set the value of the next cards in the deck of the array

	if (strpos($CardDeck[$Card_position], "2") !== FALSE) {
		$CardValue += 2;
	}else if (strpos($CardDeck[$Card_position], "3") !== FALSE){
		$CardValue += 3;
	}else if (strpos($CardDeck[$Card_position], "4") !== FALSE){
		$CardValue += 4;
	}else if (strpos($CardDeck[$Card_position], "5") !== FALSE){
		$CardValue += 5;
	}else if (strpos($CardDeck[$Card_position], "6") !== FALSE){
		$CardValue += 6;
	}else if (strpos($CardDeck[$Card_position], "7") !== FALSE){
		$CardValue += 7;
	}else if (strpos($CardDeck[$Card_position], "8") !== FALSE){
		$CardValue += 8;
	}else if (strpos($CardDeck[$Card_position], "9") !== FALSE){
		$CardValue += 9;
	}else if (strpos($CardDeck[$Card_position], "10") !== FALSE){
		$CardValue += 10;
	}else if (strpos($CardDeck[$Card_position], "J") !== FALSE){
		$CardValue += 10;
	}else if (strpos($CardDeck[$Card_position], "Q") !== FALSE){
		$CardValue += 10;
	}else if (strpos($CardDeck[$Card_position], "K") !== FALSE){
		$CardValue += 10;
	}else if (strpos($CardDeck[$Card_position], "A") !== FALSE){
		$CardValue += 11;
	}else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}
$HandTotal += $CardValue; //Adds Total Hand value
}

foreach($CardDeck as $card) {
	$cardstring .= ", " . $card;
}

if ($HandTotal > 21) //If the hand goes above 21 output statement to let user know the game is over
	echo "<p><strong>You Busted: GAME OVER!</strong></p>";
elseif ($HandTotal == 21)  //If the hand == 21 output statemetn to let user know they hit blackjack
	echo "<p><strong>You Hit BLACKJACK!</strong></p>";

$_SESSION['carddeck'] = $CardDeck; //overwrite $CardDeck session 
$_SESSION['handtotal'] = $HandTotal; //overwrite $HandTotal session
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Black Jack</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
echo "<p>Your hand: " . substr($cardstring,1) . "</p>"; //output the users hand
echo "<p>Hand Value: " . $HandTotal . "</p>"; //output hand total
?>
<p><a href="HitMe.php">Hit Me!</a></p> <!-- Link to HitMe.php -->
<hr />
<p><a href="Deal.php">New Game!</a></p> <!-- Link to Deal.php -->
</body>
</html>

Not sure where It's coming from, and I'm obviously still working on the code for the Aces to be determined correctly if they should be a 1 or an 11.

Member Avatar for diafol

OK - an unforgiving undeclared variable!

place this before the loop:

$cardstring = "";

ahh, the simplicity of it, lol. Sometimes the easiest things just go woosh right past your head. Thanks again ardav. Ok, onto work on my Ace code to get them calculated correctly. I'm sure I'll be back, hopefully I haven't given you too much of a headache already.

Member Avatar for diafol

Nah, feel free. Mind you - you better mark this thread solved once it's done!

Ofcourse

Still having a problem getting the Aces to work correctly.

Here's the issue. If the Ace is the first card, it works great and declares itself as 11, if it is the second card it declares itself as a 1.

Where I run into the problem is if I am dealt. AC QC I hit blackjack.
If I am dealt QC AC it gives me a hand total of 11.

I've tried the code SEVERAL different ways from just using a simple if..else statement to counting the aces etc and just can't get it to work correctly.

Here's what I currently have in HitMe.php

if($HandTotal > 21){
	if(strpos($CardDeck[$Card_position], "A") !== FALSE) {
		$CardValue += 11;
	 }else{
		$CardValue += 1;
	}
}

Which is causing the problem above. If I try and count the aces and return a value I get the same problem so not sure where I'm going wrong at this point. PhP noobness at it's finest.

Member Avatar for diafol

Still having a problem getting the Aces to work correctly.

Here's the issue. If the Ace is the first card, it works great and declares itself as 11, if it is the second card it declares itself as a 1.

Where I run into the problem is if I am dealt. AC QC I hit blackjack.
If I am dealt QC AC it gives me a hand total of 11.

I've tried the code SEVERAL different ways from just using a simple if..else statement to counting the aces etc and just can't get it to work correctly.

Here's what I currently have in HitMe.php

if($HandTotal > 21){
	if(strpos($CardDeck[$Card_position], "A") !== FALSE) {
		$CardValue += 11;
	 }else{
		$CardValue += 1;
	}
}

Which is causing the problem above. If I try and count the aces and return a value I get the same problem so not sure where I'm going wrong at this point. PhP noobness at it's finest.

Hi sm, on my blackberry so cant give a full sol.
If handtotal +11 < 22 ok else handtotal = ht + 1. Hope it makes sense

Yup, got it thanks. Took some working but it works now. Thanks for all the help, gonna go ahead and marked this as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.