nems Posted February 10, 2010 Share Posted February 10, 2010 Hey, I'm trying to create a simple Black Jack game that doesn't involve a dealer and consist of two buttons; deal, and hit. What I have so far is that when a user clicks deal they receive two cards and then those cards are removed from the deck (array). Next what I want to do is when the user clicks hit they see their two cards that they were dealt before plus a given card. However the problem I'm having is that when the "my_hit" function is called the "$hand" array is empty. Below is my code, help and suggestions are more then welcomed. <html> <body> <form action="BlackJack.php" method="get" enctype="application/x-www-form-urlencoded"> <input type="submit" name="deal" value="Deal" /> <input type="submit" name="hit" value="Hit" /> </form> <?php //DECLARING ARRAYS $cards = array(); $hand = array(); //DECLARING FILE VARIABLES $array_file = "hand.txt"; $fp = fopen($array_file, 'w+') or die("I could not open $array_file."); //GETTING FORM INFORMATION extract($_GET); if (isset($deal)) my_deal(&$hand, &$cards, &$fp); else if (isset($hi)) my_hit(&$hand); function my_deal(&$hand, &$cards, &$fp) { $cards = array( "ah", "ac", "ad", "as", "2h" => 2, "2c" => 2, "2d" => 2, "2s" => 2, "3h" => 3, "3c" => 3, "3d" => 3, "3s" => 3, "4h" => 4, "4c" => 4, "4d" => 4, "4s" => 4, "5h" => 5, "5c" => 5, "5d" => 5, "5s" => 5, "6h" => 6, "6c" => 6, "6d" => 6, "6s" => 6, "7h" => 7, "7c" => 7, "7d" => 7, "7s" => 7, "8h" => 8, "8c" => 8, "8d" => 8, "8s" => 8, "9h" => 9, "9c" => 9, "9d" => 9, "9s" => 9, "th" => 10, "tc" => 10, "td" => 10, "ts" => 10, "jh" => 10, "jc" => 10, "jd" => 10, "js" => 10, "qh" => 10, "qc" => 10, "qd" => 10, "qs" => 10, "kh" => 10, "kc" => 10, "kd" => 10, "ks" => 10 ); shuffle($cards); for ($i = 1; $i <= 2; $i++) { $hand[$i] = $cards[$i]; unset($cards[$i]); echo $hand[$i], " "; } fwrite($fp, serialize($cards)); fclose($fp); } function my_hit(&$hand) { echo $hand[1]; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/191567-forms-and-passing-by-reference/ Share on other sites More sharing options...
alexjb Posted February 10, 2010 Share Posted February 10, 2010 Sessions should be able to achieve what you want. session_start(); $_SESSION['hand'] = array('your cards here'); Link to comment https://forums.phpfreaks.com/topic/191567-forms-and-passing-by-reference/#findComment-1009840 Share on other sites More sharing options...
teamatomic Posted February 10, 2010 Share Posted February 10, 2010 You not only have to put the hands in session vars but the deck also or how else do you know whats left to deal the hits from. In blackjack you also need to keep counting the shoe so you know when to reshuffle. Link to comment https://forums.phpfreaks.com/topic/191567-forms-and-passing-by-reference/#findComment-1009860 Share on other sites More sharing options...
nems Posted February 10, 2010 Author Share Posted February 10, 2010 You not only have to put the hands in session vars but the deck also or how else do you know whats left to deal the hits from. In blackjack you also need to keep counting the shoe so you know when to reshuffle. Good point, but this blackjack program for what I'm doing with it, has no dealer and one player thus they will never run through the entire shoe. (The deck is also shuffled again after each bust, not a practical game I know haha) I don't want to use a session due to the simple fact that it is dependent on cookies so instead I'm writing the arrays to a text file but serializing them first. Then in my second function I'm getting the contents of the file and putting them to a string variable which I then unserialize. However when the second function is called the contents of cards_hand.txt is deleted so nothing happens. I currently have the program seperated into two pages; BlackJack.php, and BlackJack.inc.php. BlackJack.php. <html> <body> <form action="BlackJack.php" method="post" enctype="application/x-www-form-urlencoded"> <input type="submit" name="deal" value="Deal" /> <input type="submit" name="hit" value="Hit" /> </form> <?php require "BlackJack.inc.php"; $cards = array(); $hand = array(); $fp = fopen('cards_hand.txt', 'w'); extract($_POST); if (isset($deal)) my_deal(&$hand, &$cards, &$fp); else if (isset($hit)) my_hit(&$hand); ?> </body> </html> BlackJack.inc.php <?php function my_deal(&$hand, &$cards, &$fp) { $cards = array( "ah", "ac", "ad", "as", "2h" => 2, "2c" => 2, "2d" => 2, "2s" => 2, "3h" => 3, "3c" => 3, "3d" => 3, "3s" => 3, "4h" => 4, "4c" => 4, "4d" => 4, "4s" => 4, "5h" => 5, "5c" => 5, "5d" => 5, "5s" => 5, "6h" => 6, "6c" => 6, "6d" => 6, "6s" => 6, "7h" => 7, "7c" => 7, "7d" => 7, "7s" => 7, "8h" => 8, "8c" => 8, "8d" => 8, "8s" => 8, "9h" => 9, "9c" => 9, "9d" => 9, "9s" => 9, "th" => 10, "tc" => 10, "td" => 10, "ts" => 10, "jh" => 10, "jc" => 10, "jd" => 10, "js" => 10, "qh" => 10, "qc" => 10, "qd" => 10, "qs" => 10, "kh" => 10, "kc" => 10, "kd" => 10, "ks" => 10 ); shuffle($cards); for ($i = 1; $i <= 2; $i++) { $hand[$i] = $cards[$i]; unset($cards[$i]); echo $hand[$i], " "; } fwrite($fp, serialize($cards)); fwrite($fp, serialize($hand)); fclose($fp); } function my_hit(&$hand) { $temp = file_get_contents('cards_hand.txt'); $new_hand = unserialize($temp); echo $new_hand[1]; echo $new_hand[2]; } ?> The problem is similar to if you open a file then forget to close it, causing the data to be lost. What am I missing? Link to comment https://forums.phpfreaks.com/topic/191567-forms-and-passing-by-reference/#findComment-1010405 Share on other sites More sharing options...
nems Posted February 10, 2010 Author Share Posted February 10, 2010 Sessions should be able to achieve what you want. session_start(); $_SESSION['hand'] = array('your cards here'); I dont know why I was thinking sessions were dependent on cookies, so I'm open to using sessions again. However I get this error when trying to use a session: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\BlackJack.php:3) in C:\xampp\htdocs\BlackJack.php on line 3 I have <?php session_start(); ?> at the start of my file but still recieve the same error. Link to comment https://forums.phpfreaks.com/topic/191567-forms-and-passing-by-reference/#findComment-1010428 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.