Ok, I'm creating working on a black jack game. A very simplified version that doesn't have a dealer. Just the user and they either hit black jack or bust basically.

I have the code written for a deck of cards, then shuffle it and place it into a text file. I then give the user the first two cards they are dealt. No issues there.

My question is how would I go about assigning a value to each of the cards without making a huge block of code to do so.

Here is the code:

<!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 the deck of cards

$DeckStore = fopen("blackjack.txt", "w"); //Opens blackjack.txt
fwrite($DeckStore, var_export($CardDeck,1)); //Writes the Deck of shuffled cards into blackjack.txt on the first line
fclose($DeckStore); //Closes blackjack.txt

echo "<p>You have been dealt the " . ($CardDeck[0]) . " and the " . ($CardDeck[1]) . "</p>";
?>
</body>
</html>

Is there a way to do it with the array I currently have? I tried to use two arrays. One for cards and one for suits and have them randomized into each other but I'm having quite a few issues with even getting it to work at all. Any tips and help is much appreciated.

Is it possible to switch the array around to an associative array such as 2=>"2H" , 3=>"3H" .... , 10=>"KH", 10=>"QH" even though you'de essentially have 16 values of 10 with different keys associated with it?

Arrays have always thrown me for a loop for whatever reason.

Member Avatar for diafol

I'm not really sure, but having 52 array items seems logical. However, you may need to have nested / multidimensional arrays - where the cards are thus:

$cards = array(
   array('label' => '1D', 'cvalue' => '11', 'altvalue' => '1', 'img' => 'd_ace.gif'),
   array('label' => '2D', 'cvalue' => '2', 'altvalue' => '', 'img' => 'd_deuce.gif'),
  ...etc...
)

$cards[0] gives "1D".
$cards[1] gives "d2.gif".

You could even have another array (0-51) that gets shuffled, stored and then when 'dealt' gets the values from the $cards array. Obviously this could also be done with a DB.

Ok, that's what I was wondering. I'll play with it some more and see what I can come up with. I'll most likely be back to bug ya some more :).

Member Avatar for diafol

Welcome.

Have another question.

Now, I don't completely understand the use of strpos() function. However, would it work something like this in this instance?

if (strpos($Card[0], "2") !== FALSE)

      $CardValue += 2;

else if (strpos($Card[0], "3" !== FALSE)

      $CardValue += 3;

........

The problem I'm having is setting the $CardValue variable. Why you may ask? I have no idea thats why I'm asking :).

Ok, it worked. Finally was able to make cardvalue work, not sure what I had wrong the first time, but rewrote the entire code and it worked this time around. Back to the drawing board on the rest of the program.

Ok, now I have a question.

Say I wanted to have a hit me button that took the first two cards off of the array, then save them in the second line of the text file. How would I go about getting that done.

I understand that I have to open the file, then it gets hairy from there. I can't get it to read the first two cards, take them out of the array on line 1 then make a new array on line 2 of the text file. Php newbness at its finest :).

Should probably give more details.

Ok. So I have a form with a hitme and deal link

Here is the form:

<!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>
<h1>Black Jack</h1>
<p><a href="Deal.php">Shuffle up and Deal!</a>
<p><a href="HitMe.php">Hit Me!</a></p>
</body>
</html>

When You hit the deal link, it creates the deck of cards, shuffles it, then saves it to the first line in the text file. Nothing special and works good.

<!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 the deck of cards

$DeckStore = fopen("blackjack.txt", "w+"); //Opens blackjack.txt
fwrite($DeckStore, var_export($CardDeck,1)); //Writes the Deck of shuffled cards into blackjack.txt on the first line
fclose($DeckStore); //Closes blackjack.txt
?>
</body>
</html>

This is what the text file looks like:

array (0 => '9H', 1 => 'AH', 2 => '5D', 3 => '7D', 4 =>'7H', ..etc

Then I want the person to be able to hit the hit me link which will :
1)open the file
2)take the first two cards off of the deck, store them in the second line
3)Calculate the value of the hand, then show the 2 dealt cards to the user.
4)They can then choose to hit the hitme link again and either bust or hit blackjack, (preety simple)

However, when I try to read the array from hitme.php it reads completely wrong. What I will get is array( for $handarray[0] and if i shift the array and do something like this:

$CurHand = $HandArray[0];
array_shift($HandArray);
$CurHand = $HandArray[0];

I'll get something like this: array(array => 3h

This is all I have so far because I've delete most of the code I had in hitme.php

<!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
if (!file_exists("blackjack.txt") || filesize("blackjack.txt") == 0)
	echo "<p>There is not a deck to play with!.</p>";
else {
	$HandArray = file("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 += 10;
else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}
?>
</body>
</html>

Not sure where I'm going wrong here. If I want to show the hand from the deal link it writes it just fine. The only problem there that I'm having is how to sum the value of the cards one I retrieve it. Any suggestions on that will be much appreciated aswell. However, I think it would be alot easier to get the first two cards on the second line and into their own array then just array_sum that array.

Ok, figured out a bit of my problem. I was writing to the file completely wrong. Once I was able to hash all that out, I can finally reread the array again and get the values as needed.

I have a quick question though. Once I assign the values to the first cards. How would I go about getting the second, third, forth card value. Would pop()ing it off work or how would I add the values. Think I've been staring at this too long.

Also, how would I go about grabbing the first two elements in the array, reassigning them to a new array and writing just those values to the text file. I don't quite understand the array_slice function, can't get it to work correctly at all.

Creates the deck, shuffles it and stores it in the text file.

<!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 the deck of cards

$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
?>
</body>
</html>

Opens the textfile, gets the value of the first two cards, assigns a value to the first card. (In it's current state)

<!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
if (!file_exists("blackjack.txt") || filesize("blackjack.txt") == 0)
	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]);
			echo "<p>Your current hand is: </p><br />";
			echo $Cards[0] . " and the " . $Cards[1];
		} 
}
$DeckStore = fopen("blackjack.txt", "a+"); //Open blackjack.txt
foreach($HandArray 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($Cards[0], "2") !== FALSE)
	$CardValue += 2;
else if (strpos($Cards[0], "3") !== FALSE)
	$CardValue += 3;
else if (strpos($Cards[0], "4") !== FALSE)
	$CardValue += 4;
else if (strpos($Cards[0], "5") !== FALSE)
	$CardValue += 5;
else if (strpos($Cards[0], "6") !== FALSE)
	$CardValue += 6;
else if (strpos($Cards[0], "7") !== FALSE)
	$CardValue += 7;
else if (strpos($Cards[0], "8") !== FALSE)
	$CardValue += 8;
else if (strpos($Cards[0], "9") !== FALSE)
	$CardValue += 9;
else if (strpos($Cards[0], "10") !== FALSE)
	$CardValue += 10;
else if (strpos($Cards[0], "J") !== FALSE)
	$CardValue += 10;
else if (strpos($Cards[0], "Q") !== FALSE)
	$CardValue += 10;
else if (strpos($Cards[0], "K") !== FALSE)
	$CardValue += 10;
else if (strpos($Cards[0], "A") !== FALSE)
	$CardValue += 10;
else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}
echo "Your current hand values is :" . $CardValue;
?>
</body>
</html>
Member Avatar for diafol

OK, as you're using php, each action will require a page refresh. However if you use javascript or preferably Ajax - it can be a lot smoother. This is how I'd do it. I'll just give an idea for now:

1. You can set up a set of arrays and variables:
A. randomized card order (or 'deck') array (0 - 51).
B. an empty hand array - which will hold a list of cards in the hand.
C. multidimensional array(0-51 with values;altvalues;labels;images) as mentioned in my original post.
D. running total (Ace = 11) value
E. running total (Ace = 1) value
F. current position (in deck) value

You can save these in $_SESSION variables to that they're easily available to Ajax calls - so that you can do away with files altogether.

A. $_SESSION
B. $_SESSION
C. $_SESSION
D. $_SESSION
E. $_SESSION
F. $_SESSION

You can have the following links (AJAX: javascript calls to php functions) on your page :

1. New Game - resets the arrays and shows the deal link
2. Deal - deals 2 cards (add values to arrays and move position pointer), show Hit and Stick links, Hide Deal link, check for Black Jack
3. Hit - add card value to total etc, move position pointer - check for BUST
4. Stick - store total - although this is pointless without a dealer. Could add this functionality later on - just have duplicates of B,D,E for the dealer.

In addition you could keep a running total of your performance in a cookie, file or DB. Furthermore, you could add a betting aspect. Sorry, getting beyond myself now.

Unfortunatly, your talking alien to me when it comes to AJAX etc. I'm new to php and grabbed a book and going through it step by step trying to learn it. So that is out of my comprehension atm.

Member Avatar for diafol

Sorry - like I said got away from myself.

Ok, trying to simplify this down for myself as much as possible.

This is what I've rewrote so far. I have a main page that says welcome, then a shuffle up and deal link. It then goes to the deal page which shows the first two cards (the users hand) and their hand total.

The form then has a hit me link and a stand link. The stand link brings them to a page they automatically win, then can hit the deal link again.

The hit me link is what I'm having a problem with. How would I go about adding a card and a card value up to 5 cards (1 at a time) until they bust, hit blackjack, or stand?

Main form : Blackjack.php

<!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>
<h2>Welcome!</h2>
<h2>Let's Play Black Jack</h2>
<hr />
<body>
<p><a href="Deal.php">Shuffle up and Deal!</a>
</body>
</html>

Page that deals the first two cards and gives the hand total:
Deal.php

<!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);

$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 1st 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 += 11;
else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}

$HandTotal = ($CardValue + $CardValue1);

echo "<p>Your hand: " . $CardDeck[0] . " , " . $CardDeck[1] . "</p>";
echo "<p>Hand Value: " . $HandTotal . "</p>";
?>
<p><a href="HitMe.php">Hit Me!</a></p>
<p><a href="Stand.php">Stand!</a></p>
</body>
</html>

HitMe.php is where I'm lost. I can get the current hand, but how would I go about adding one card at a time, everytime they click the link?

Member Avatar for diafol

OK, here's some logic (mine!):

Variables to keep note of:

minimum_total (for hands with aces)
maximum_total
card_number (will = 2 after deal)

The card number increments after each hit_me, so that you cannot draw another card beyond card no. 5.

The minimum_total keeps a check on your score calculations for BUST. Count all aces as 1. The max obviously counts them as 11. This isn't perfect - a more complicated algorithm is required is you have more than one ace in your hand.

HIT_ME:
if card_number < 5 - draw card
if card (A = 1) added to total > 21 -> BUST; show deal link; hide hitme/stand links; reset totals
if card added to total < 22 -> carry on playing - add card value to totals (A = 1 and A = 11); increment the card_number

I suggest using $_SESSION variables to hold these variables as you can then use them across php files without having to write them to text file or losing them to the aether.


BTW - if you'r looking for some visuals - a great image here:

http://math.hws.edu/eck/cs124/f05/labs/lab11/cards.png

You can use CSS to serve up some cards (using the background-position property). Methinks this is called spriting. Width of each card is 79px and height is 123px.

I used it in a bj project myself:

Member Avatar for diafol

Sorry SM - forgot to include the url of my project. It's not great, but it works after a fashion. Few niggles (blackjack appears when 21 achieved at any time/ soft stops score increment), but I abonded it to concentrate on other things. Still, should give you an idea of what can be achieved with just session variables (and some js) - no DB, no files. Perhaps I'll have a look at improving it.

http://tinyurl.com/yfhdedz

I understand the concept and what I have to do, that is what is frustrating me the most. I can not figure out the hitme page to be completely honest.

Like I said, I can get the hand dealt and it figures in their initial card value. How do I get it to add 1 card from the deck every time they hit the hit me link on the page. Been staring at this daily for hours and go back through my book page by page and I just can't get it to work.

Member Avatar for diafol

OK, if you want to use an external file (hit_me.php) to add value. You must have a way of propagating the data for total and cards from the deal page. This can be done simply with a bunch of $_SESSION variables.

OK here we go:

In your deal.php page, right at the top, before the doctype declaration, do this:

<?php
session_start();
if(isset($_SESSION['carddeck']))unset($_SESSION['carddeck']);
if(isset($_SESSION['handtotal']))unset($_SESSION['handtotal']);
?>

Then at the bottom of your file:

[B]$_SESSION['carddeck'] = $CardDeck;
$_SESSION['handtotal'] = $HandTotal;[/B]
echo "<p>Your hand: " . $CardDeck[0] . " , " . $CardDeck[1] . "</p>";
echo "<p>Hand Value: " . $HandTotal . "</p>";

In your hit_me.php file:

At the top:

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

Then, your hit_me page has all you need to carry on totalling:

//your next card from the deck can be deduced from the number of array items in the $CardDeck. Get this from: count($CardDeck)-1 .

check to see it's less than 5 (if your max hand can have 5 cards)

run some of the deal code again to get the value and add to the total.

ensure this is also at the bottom of the hit_me page:

[B]$_SESSION['carddeck'] = $CardDeck;
$_SESSION['handtotal'] = $HandTotal;[/B]
echo "<p>Your hand: " . $CardDeck[0] . " , " . $CardDeck[1] . "</p>";
echo "<p>Hand Value: " . $HandTotal . "</p>";

Your hitme link (ensure this is present in the hitme.php page as well) should refer to itself (href="hitme.php).

All this code could be tidied up considerably with functions and include files, but that can be done later if you decide to optimise your code.

ok, thanks again. Gonna give this a whirl and see where it goes from there. Thanks much.

Scratch that it's working now not sure what that was about. ok, back to getting the next card to get onto the hand and the total.

//your next card from the deck can be deduced from the number of array items in the $CardDeck. Get this from: count($CardDeck)-1 .

This line here. Now, I'm having a little trouble getting this correct. Am I supposed to use a for loop to count through and remove an item? it just remove an item. I don't know why this is confusing me so much to be completely honest because it shouldn't be this hard.

Member Avatar for diafol

//your next card from the deck can be deduced from the number of array items in the $CardDeck. Get this from: count($CardDeck)-1 .

This line here. Now, I'm having a little trouble getting this correct. Am I supposed to use a for loop to count through and remove an item? it just remove an item. I don't know why this is confusing me so much to be completely honest because it shouldn't be this hard.

[PERHAPS you should just use count($CardDeck)]

You don't need to delete anything.

say last card drawn from deck has key = 1 (since first card in deck = 0). SO the next card drawn on hit_me wil be card key = 2.

Your hitme.php page needs to know which card position you're at. This comes from the $_SESSION variable (originally set in the deal page).

count($CardDeck) will give 2 immediately after deal, so count($CardDeck) - 1 will give '1' - which will equal the item key of the last card drawn in your text file.

So, count($CardDeck) [without the '-1'] can be used to retrieve the next card from the text file.

Say the value = "4H", just get the numerical value and add it to the running total (as before) and add the whole card value to your $CardDeck array.

Remember to transfer all this data to the session equivalents by the end of the file so that any subsequent hit_me requests will add the correct card from the text file.

[PERHAPS you should just use count($CardDeck)]

You don't need to delete anything.

say last card drawn from deck has key = 1 (since first card in deck = 0). SO the next card drawn on hit_me wil be card key = 2.

Your hitme.php page needs to know which card position you're at. This comes from the $_SESSION variable (originally set in the deal page).

count($CardDeck) will give 2 immediately after deal, so count($CardDeck) - 1 will give '1' - which will equal the item key of the last card drawn in your text file.

So, count($CardDeck) [without the '-1'] can be used to retrieve the next card from the text file.

Say the value = "4H", just get the numerical value and add it to the running total (as before) and add the whole card value to your $CardDeck array.

Remember to transfer all this data to the session equivalents by the end of the file so that any subsequent hit_me requests will add the correct card from the text file.

This is what I have so far. Still can't get the next card to add then the next. Sitting here shaking my head at myself.

This is what I have going so far. Hit me will produce the current hand, but won't add the next card because I don't have the code to add it obviously and I've tried a few dozen different things to no avail.

<!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
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)
	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]);
			count($CardDeck);
		}
			
	} 

$_SESSION['carddeck'] = $CardDeck;
$_SESSION['handtotal'] = $HandTotal;
echo "<p>Your hand: " . $CardDeck[0] . " , " . $CardDeck[1] . "</p>";
echo "<p>Hand Value: " . $HandTotal . "</p>";
?>
<p><a href="HitMe.php">Hit Me!</a></p> <!-- Link to HitMe.php -->
</body>
</html>

I'm not exactly sure how to update the hand to 3 cards keep them reloop so on and so forth, same goes for adding the value of the card hand, keeping it, then looping through it again and again.

Member Avatar for diafol
<?php
session_start();
//========= retrieve details from deal or previous hit me ====//
if(isset($_SESSION['carddeck']))$CardDeck = $_SESSION['carddeck'];
if(isset($_SESSION['handtotal']))$HandTotal = $_SESSION['handtotal'];

//========= get deck info =========================//
if (!file_exists("blackjack.txt") || filesize("blackjack.txt") == 0)
	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]);
			
		}
			
	} 
//========= draw new card from deck
$card_pos = count($CardDeck); //get card position to draw from deck
if($card_pos < 6){ //check that no. cards is 5 or less
  array_push($CardDeck, $Cards[$card_pos]); //add drawn card to drawn cards array

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

//========= make a list of cards for echo output =========//
foreach($CardDeck as $card){
  $cardstring .= "," . $card; //make a list of all cards
}
//========= overwrite SESSION data with new data ======//
$_SESSION['carddeck'] = $CardDeck; //array of drawn cards
$_SESSION['handtotal'] = $HandTotal; //running total
?>

<!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
//========== your output ========================//
echo "<p>Your hand: " . substr($cardstring,1) . "</p>";
echo "<p>Hand Value: " . $HandTotal . "</p>";
?>
<p><a href="HitMe.php">Hit Me!</a></p> <!-- Link to HitMe.php -->
</body>
</html>

Sorry if it's long winded. Tried to comment as best as I can. Hope it makes sense.

Not at all. Makes sense. There is an error in the code that I'm trying to work out now.

Notice: Undefined variable: Cardstring in C:\wamp\www\HitMe.php  on line 53

which gives me the output of :

Your hand: , 5H , QD , 4D , KD , KS , 5C , 2S , 10S , QS , QH , 5S , AD , JD , AS , 9S , 4C , 7D , 8C , 3H , 10C , 7C , 3D , 2D , 10D , 2C , 4H , JS , KC , 9D , 3C , JC , 6S , 9H , AC , 3S , 7S , 4S , 7H , 8S , 9C , 2H , KH , 8H , 8D , 6D , AH , QC , 6H , 10H , 5D , JH , 6C

Hand Value: 15

Hit Me!

Instead of 3 cards then 4 and doesn't update the hand total correctly.

If I take the . out of this line:

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

It returns the last card in the array but thats it so the output would change to :

Your hand: , 8C

without updating the new handtotal.

If I do anything else to it I just get parse errors, and If i revert back it prints out the entire deck for their hand.

Member Avatar for diafol

OK - my fault - I forgot that the $CardDeck array on the deal.php page held ALL cards in the deck. Do this in the deal.php:

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

<strong>$CardDeck = array($CardDeck[0],$CardDeck[1]);</strong>


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

We simply overwrite the $CardDeck array. I get this:

Thanks a ton again and again :). I'm still getting this error though and it's not making any sense, I don't get it why it is undefined:

Notice: Undefined variable: Cardstring in C:\wamp\www\HitMe.php on line 54

Member Avatar for diafol

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);
 
$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 1st 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 += 11;
else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}
 
$HandTotal = ($CardValue + $CardValue1);
 
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;


?>
<p><a href="HitMe.php">Hit Me!</a></p>
<p><a href="Stand.php">Stand!</a></p>
</body>
</html>

HitMe.php
=======

<?php
session_start();
//========= retrieve details from deal or previous hit me ====//
if(isset($_SESSION['carddeck']))$CardDeck = $_SESSION['carddeck'];
if(isset($_SESSION['handtotal']))$HandTotal = $_SESSION['handtotal'];

//========= get deck info =========================//
if (!file_exists("blackjack.txt") || filesize("blackjack.txt") == 0)
	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]);
 
		}
 
	} 
//========= draw new card from deck
$card_pos = count($CardDeck); //get card position to draw from deck



if($card_pos < 6){ //check that no. cards is 5 or less
  array_push($CardDeck, $Cards[$card_pos]); //add drawn card to drawn cards array
 
//========= add card value to running total ============//  
$CardValue = 0; //if...else.if statement to set the value of the 1st card in the deck of the array
if (strpos($CardDeck[$card_pos], "2") !== FALSE)
	$CardValue = 2;
else if (strpos($CardDeck[$card_pos], "3") !== FALSE)
	$CardValue = 3;
else if (strpos($CardDeck[$card_pos], "4") !== FALSE)
	$CardValue = 4;
else if (strpos($CardDeck[$card_pos], "5") !== FALSE)
	$CardValue = 5;
else if (strpos($CardDeck[$card_pos], "6") !== FALSE)
	$CardValue = 6;
else if (strpos($CardDeck[$card_pos], "7") !== FALSE)
	$CardValue = 7;
else if (strpos($CardDeck[$card_pos], "8") !== FALSE)
	$CardValue = 8;
else if (strpos($CardDeck[$card_pos], "9") !== FALSE)
	$CardValue = 9;
else if (strpos($CardDeck[$card_pos], "10") !== FALSE)
	$CardValue = 10;
else if (strpos($CardDeck[$card_pos], "J") !== FALSE)
	$CardValue = 10;
else if (strpos($CardDeck[$card_pos], "Q") !== FALSE)
	$CardValue = 10;
else if (strpos($CardDeck[$card_pos], "K") !== FALSE)
	$CardValue = 10;
else if (strpos($CardDeck[$card_pos], "A") !== FALSE)
	$CardValue = 11;
else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}
$HandTotal += $CardValue; //keep running total by adding value of drawn card
}
 
//========= make a list of cards for echo output =========//
foreach($CardDeck as $card){
  $cardstring .= "," . $card; //make a list of all cards
}
//========= overwrite SESSION data with new data ======//
$_SESSION['carddeck'] = $CardDeck; //array of drawn cards
$_SESSION['handtotal'] = $HandTotal; //running total
?>
 
<!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
//========== your output ========================//
echo "<p>Your hand: " . substr($cardstring,1) . "</p>";
echo "<p>Hand Value: " . $HandTotal . "</p>";
?>
<p><a href="HitMe.php">Hit Me!</a></p> <!-- Link to HitMe.php -->
</body>
</html>

Works perfectly for me.

I'll take a look at it some more when I get out of work. Not sure why I'm getting that error. The code works even though it pops the error up which is just strange.

Thanks again, you've been a ton of help.

Member Avatar for diafol
if (strpos($CardDeck[$card_pos], "2") !== FALSE){
	$CardValue = 2;
}else if (strpos($CardDeck[$card_pos], "3") !== FALSE){
	$CardValue = 3;
}else if (strpos($CardDeck[$card_pos], "4") !== FALSE){
	$CardValue = 4;
}else if (strpos($CardDeck[$card_pos], "5") !== FALSE){
	$CardValue = 5;
}else if (strpos($CardDeck[$card_pos], "6") !== FALSE){
	$CardValue = 6;
}else if (strpos($CardDeck[$card_pos], "7") !== FALSE){
	$CardValue = 7;
}else if (strpos($CardDeck[$card_pos], "8") !== FALSE){
	$CardValue = 8;
}else if (strpos($CardDeck[$card_pos], "9") !== FALSE){
	$CardValue = 9;
}else if (strpos($CardDeck[$card_pos], "10") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "J") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "Q") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "K") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "A") !== FALSE){
	$CardValue = 11;
}else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}

Your original code didn't have braces around the conditionals, perhaps this would help. If you get it to work, you may find that putting common pieces of code from both files into functions would be of help. These functions could then be placed into a separate 'include' file and placed at the head of each file:

include('functions.php');

e.g.
Place the conditonals into a function and call thus. THe include should be placed after where $CardDeck and position of next card are declared or they will receive 'blank' parameters.

$cv = getCardValue($pos,$CardDeck);

in the functions.php
==============

function getCardValue($card_pos,$CardDeck){
  if (strpos($CardDeck[$card_pos], "2") !== FALSE){
	$CardValue = 2;
}else if (strpos($CardDeck[$card_pos], "3") !== FALSE){
	$CardValue = 3;
}else if (strpos($CardDeck[$card_pos], "4") !== FALSE){
	$CardValue = 4;
}else if (strpos($CardDeck[$card_pos], "5") !== FALSE){
	$CardValue = 5;
}else if (strpos($CardDeck[$card_pos], "6") !== FALSE){
	$CardValue = 6;
}else if (strpos($CardDeck[$card_pos], "7") !== FALSE){
	$CardValue = 7;
}else if (strpos($CardDeck[$card_pos], "8") !== FALSE){
	$CardValue = 8;
}else if (strpos($CardDeck[$card_pos], "9") !== FALSE){
	$CardValue = 9;
}else if (strpos($CardDeck[$card_pos], "10") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "J") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "Q") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "K") !== FALSE){
	$CardValue = 10;
}else if (strpos($CardDeck[$card_pos], "A") !== FALSE){
	$CardValue = 11;
}else {
	echo "The deck is empty"; //Error statement if the textfile is empty
}
  return $CardValue;
}
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.