Help with my code - Blackjack

Discussion in 'PHP' started by D_C, Jan 26, 2008.

  1. #1
    I'm pretty new to php. I can create, edit, and do stuff with MYSQL but I want to create a TBS game and am throwing some challenge practices at myself to get my ready.

    So I thought blackjack is a good place to go.

    I have it set up in probably a more complicated way than it needs to be but I'm learning so just give me a hand here.

    Right now I'm just makeing sure the vars and commands obey the rules of blackjack. So far I'm working on one card cant be the same as the others. To practice this, I have it so it finds random 1-4 values of two cards. 1 = two of something, 2 = two of something else ect ext. So all I have is four two cards to practice with. I'm making it so one card cant be 'two of diamonds' and the other cant be 'two of diamonds'.

    Problems were this. The array_rand() i was using occasionally returned null. Makeing no card appear. So I made some loops so that if it found that it was null, it should try again untill it wasnt.

    That worked just fine. So I made a loop to check if one equals the other, and if it does it should set them to null which would start it over again. It worked...

    Kinda...

    They wont equal each other anymore, but sometimes the browser just keeps looping and looping, wont ever load.

    So now I need help XD How can I make this work?

    I'm not hideing the code, if people want to learn with me go ahead and take a look.

    
    <?
    
    $one=array('1', '2', '3', '4');
    
    //Makes it so the name cant be null or equal each other
    $card_one_name = null;
    while ($card_one_name == null)
    {
    	$card_one_name = array_rand($one);
    }
    
    $card_two_name = null;
    while ($card_two_name == null)
    {
    	$card_two_name = array_rand($one);
    }
    
    while ($card_one_name == $card_two_name)
    {
    	$card_one_name = null;
    	$card_two_name = null;
    }
    
    //other vars
    $name_one = null;
    $name_two = null;
    $card_one_amount = null;
    $card_two_amount = null;
    
    
    /*
    -----------------------------------------------------------
    */
    
    
    /*
    Card One name to amount
    */
    
    //2
    if ($card_one_name == "1")
    {
    	$name_one = "2 of diamonds";
    	$card_one_amount = "2";
    }
    
    if ($card_one_name == "2")
    {
    	$name_one = "2 of hearts";
    	$card_one_amount = "2";
    }
    
    if ($card_one_name == "3")
    {
    	$name_one = "2 of spaids";
    	$card_one_amount = "2";
    }
    
    if ($card_one_name == "4")
    {
    	$name_one = "2 of clubs";
    	$card_one_amount = "2";
    }
    
    /*
    Card Two name to amount
    */
    
    //2
    if ($card_two_name == "1")
    {
    	$name_two = "2 of diamonds";
    	$card_two_amount = "2";
    }
    
    if ($card_two_name == "2")
    {
    	$name_two = "2 of hearts";
    	$card_two_amount = "2";
    }
    
    if ($card_two_name == "3")
    {
    	$name_two = "2 of spaids";
    	$card_two_amount = "2";
    }
    
    if ($card_two_name == "4")
    {
    	$name_two = "2 of clubs";
    	$card_two_amount = "2";
    }
    
    /*
    -----------------------------------------------------------
    */
    
    
    $total=$card_one_amount + $card_two_amount;
    
    
    echo "Dealer Has:" .$name_one ." and " .$name_two;
    echo "<br>Total: " .$total;
    
    echo "<br><hr><br> " .$card_one_name ." " .$card_two_name ." " .$card_one_amount ." " .$card_two_amount;
    
    ?>
    
    PHP:
    Anyone?
     
    D_C, Jan 26, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    
    while ($card_one_name == $card_two_name)
    {
        $card_one_name = null;
        $card_two_name = null;
    }
    
    PHP:
    This is probably where your infinite loop is coming from. If the first card is exactly equal to the second card, then set them both to null. BUT, now they are both null, and both equal, therefore the loop will continue ... forever.

    Jay
     
    jayshah, Jan 26, 2008 IP
  3. D_C

    D_C Well-Known Member

    Messages:
    1,107
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    160
    #3
    Well heck, I didnt think about that. I'll fidle with that for a bit. Thanks a bunch.
     
    D_C, Jan 26, 2008 IP
  4. D_C

    D_C Well-Known Member

    Messages:
    1,107
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    160
    #4
    Got it, I just changed it to
    
    while ($card_one_name == $card_two_name && $card_one_name < 4)
    {
       $card_one_name ++;
    }
    while ($card_one_name == $card_two_name && $card_one_name == 4)
    {
      $card_one_name --;
    }
    
    PHP:
     
    D_C, Jan 26, 2008 IP