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.