jpbellavance Posted October 3, 2009 Share Posted October 3, 2009 Hello, I am learning PHP by creating a small project to play the Card game WAR. On the page I have the user click DRAW. The program than pulls a function from another PHP file that picks a random number from a deck and then uses that number within a SWITCH statement to assign to a name card such as 4C = four of clubs. I am able to two cards returned back to an array on my main page in display it. One for the computer and one for the user. Later I will write the code for who wins. The first draw is successful but a repeated draw does not seem to do anything. I have included the .php code. Any information or hints will be greatly appreciated with a virtual Guinness. Thanks in advance. website: http://www.battlescapes.com/CardGame/frontpage.php <?php include ('header.html'); include('cardfunctions.php'); if (isset($_POST['submitted'])){//User did click. $twocards = create_newdeck(); echo ' <table width = "400" align="center"> <tr> <td width="200" align="center"><b>Computers Cards</b></td> <td width="200" align="center"><b>Users Cards</b></td> </tr> <tr> <td width="200" align="center">' . $twocards[0] . '</td> <td width="200" align="center">' . $twocards[2] . '</td> </tr> </table>'; }else{ // USER DID NOT CLICK. echo '<p align="center"><b>Please draw a card.</b></p>'; }//END OF if (isset($_POST['submitted'])) statement ?> <form action = "war.php" method="post"> <table width="770" align="center" cellpadding="5" cellspacing="5"> <tr> <td align="center"><input type="submit" name="submit" value="Draw"/></td> </tr> </table> <input type="hidden" name="submitted" value="TRUE"/> </form> <?php include('footer.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/176401-new-to-php-question-about-forms-after-submissions/ Share on other sites More sharing options...
sKunKbad Posted October 3, 2009 Share Posted October 3, 2009 It's hard to see what is going on without seeing cardfunctions.php Quote Link to comment https://forums.phpfreaks.com/topic/176401-new-to-php-question-about-forms-after-submissions/#findComment-929819 Share on other sites More sharing options...
jpbellavance Posted October 4, 2009 Author Share Posted October 4, 2009 Sorry. cardfunctions.php <?php function create_newdeck(){ $randomcard1 = rand(0,55); $randomcard2 = rand(0,55); $deck = array(1=> '1C', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '10C', 'JC', 'QC', 'KC', 'AC', '1S', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', '10S', 'JS', 'QS', 'KS', 'AS', '1H', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', '10H', 'JH', 'QH', 'KH', 'AH', '1D', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', '10D', 'JD', 'QD', 'KD', 'AD'); //MAKE SURE THE SAME CARD CANNOT BE DRAWN. if ($randomcard1 == $randomcard2){//GET NEW CARD $randomcard2 = rand(0,55); if ($randomcard1 == $randomcard2){//DO AGAIN IF CARD IS AGAIN THE SAME. $randomcard2 = rand(0,55); }//END OF SECOND RANDOM DRAWING //END OF FIRST RANDOM DRAWING }else{ $usercard = $deck[$randomcard1]; $usercardvalue = getcardvalue($usercard); $computercard = $deck[$randomcard2]; $computercardvalue = getcardvalue($computercard); $cardarray = array($usercard, $usercardvalue, $computercard, $computercardvalue); return $cardarray; }//END OF FIRST IF. } function getcardvalue($card) { //ASSIGN VALUE TO CARD switch ($card) { case '1C': $value = 1; return($value); break; case '1S': $value = 1; return($value); break; case '1H': $value = 1; return($value); break; case '1D': $value = 1; return($value); break; case '2C': $value = 2; return($value); break; case '2S': $value = 2; return($value); break; case '2H': $value = 2; return($value); break; case '2D': $value = 2; return($value); break; case '3C': $value =3; return($value); break; case '3S': $value = 3; return($value); break; case '3H': $value = 3; return($value); break; case '3D': $value = 3; return($value); break; case '4C': $value =4; return($value); break; case '4S': $value = 4; return($value); break; case '4H': $value = 4; return($value); break; case '4D': $value = 4; return($value); break; case '5C': $value =5; return($value); break; case '5S': $value = 5; return($value); break; case '5H': $value = 5; return($value); break; case '5D': $value = 5; return($value); break; case '6C': $value =6; return($value); break; case '6S': $value = 6; return($value); break; case '6H': $value = 6; return($value); break; case '6D': $value = 6; return($value); break; case '7C': $value =7; return($value); break; case '7S': $value = 7; return($value); break; case '7H': $value = 7; return($value); break; case '7D': $value = 7; return($value); break; case '8C': $value =8; return($value); break; case '8S': $value = 8; return($value); break; case '8H': $value = 8; return($value); break; case '8D': $value = 8; return($value); break; case '9C': $value =9; return($value); break; case '9S': $value = 9; return($value); break; case '9H': $value = 9; return($value); break; case '9D': $value = 9; return($value); break; case '10C': $value =10; return($value); break; case '10S': $value = 10; return($value); break; case '10H': $value = 10; return($value); break; case '10D': $value = 10; return($value); break; case 'JC': $value =11; return($value); break; case 'JS': $value = 11; return($value); break; case 'JH': $value = 11; return($value); break; case 'JD': $value = 11; return($value); break; case 'QC': $value =12; return($value); break; case 'QS': $value = 12; return($value); break; case 'QH': $value = 12; return($value); break; case 'QD': $value = 12; return($value); break; case 'KC': $value =13; return($value); break; case 'KS': $value = 13; return($value); break; case 'KH': $value = 13; return($value); break; case 'KD': $value = 13; return($value); break; case 'AC': $value =14; return($value); break; case 'AS': $value = 14; return($value); break; case 'AH': $value = 14; return($value); break; case 'AD': $value = 14; return($value); break; }//END OF SWITCH }//END OF FUNCTION ?> Quote Link to comment https://forums.phpfreaks.com/topic/176401-new-to-php-question-about-forms-after-submissions/#findComment-929962 Share on other sites More sharing options...
sKunKbad Posted October 4, 2009 Share Posted October 4, 2009 I think you might be going about this all wrong... or at least that is my opinion. When playing war, player one and player two are both dealt a hand of cards, or 1/2 the deck. You might represent these cards as arrays that are held in a cookie or session array. These arrays would be added to (if the player wins), or subtracted from (if the player loses), but in your game the cards come from a sizeless deck that doesn't ever ends. Also, the cards in each players hand do not get played randomly in a real game of war. The card that is played comes off the top of the players dealt hand (which is face down in front of them). So, I think you need to go back to the basics of how to play the game of war, and then think about using cookies or the session array to hold your cards. There's only 52 cards in a deck, and none of them are the same (with the exception of jokers). Again, consider using cookies or the session array. Quote Link to comment https://forums.phpfreaks.com/topic/176401-new-to-php-question-about-forms-after-submissions/#findComment-930255 Share on other sites More sharing options...
jon23d Posted October 4, 2009 Share Posted October 4, 2009 I would consider using something more efficient to determine the value of the cards too, the big switch is unnecessary. If every value ends with H,C,S, or D, you could just strip that and then move forward, for example: function getValue($card) { $value = substr($card, 0, strlen($card) - 1); if ($value == "J") $value = 11; if ($value == "Q") $value = 12; if ($value == "K") $value = 13; return (int) $value; } Quote Link to comment https://forums.phpfreaks.com/topic/176401-new-to-php-question-about-forms-after-submissions/#findComment-930272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.