Jump to content

Recommended Posts

Hello,

 

I'm a beginner programmer, and for my first "php-script" im trying to deal myself two holecards. It's easy to get dealt the first card, but when the second card arrives I have to remove the first card from the array. How to do that?

 

Or I could do like i have tried here: if ($y != $x) { echo $y; }. Problem is when holecard number one = holecard number two it wont echo anything. So i need to say if holecard one = holecard two { do a new random }. But how do i do that?

 

<?php
    function poker() {
        $cards = array("Ah", "Ac", "Ad", "As",
                       "2h", "2c", "2d", "2s",
                       "3h", "3c", "3d", "3s",
                       "4h", "4c", "4d", "4s",
                       "5h", "5c", "5d", "5s",
                       "6h", "6c", "6d", "6s",
                       "7h", "7c", "7d", "7s",
                       "8h", "8c", "8d", "8s",
                       "9h", "9c", "9d", "9s",
                       "Th", "Tc", "Td", "Ts",
                       "Jh", "Jc", "Jd", "Js",
                       "Qh", "Qc", "Qd", "Qs",
                       "Kh", "Kc", "Kd", "Ks");


   $x = $cards[rand(0,51)];
   echo "$x ";
   $y = $cards[rand(0,51)];
   if ($y != $x) {
       echo $y;
   }
}
   
   echo poker() . "<br>";
?>

 

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/226312-beginner-help-poker-hole-card-function/
Share on other sites

function poker() {
        $cards = array("Ah", "Ac", "Ad", "As",
                       "2h", "2c", "2d", "2s",
                       "3h", "3c", "3d", "3s",
                       "4h", "4c", "4d", "4s",
                       "5h", "5c", "5d", "5s",
                       "6h", "6c", "6d", "6s",
                       "7h", "7c", "7d", "7s",
                       "8h", "8c", "8d", "8s",
                       "9h", "9c", "9d", "9s",
                       "Th", "Tc", "Td", "Ts",
                       "Jh", "Jc", "Jd", "Js",
                       "Qh", "Qc", "Qd", "Qs",
                       "Kh", "Kc", "Kd", "Ks");


   $x = $cards[rand(0,51)];
   echo "$x ";
   while(1){
   $y = $cards[rand(0,51)];
   if ($y != $x) {
	   echo $y;
	   break;
   }
   }
}
   echo poker() . "<br>";

 

So what I have added above is a while loop. The purpose is to continually try to grab a random number for $y until it doesn't equal $x.

So, if $y == $x, then the if statement won't be satisfied, therefore it will skip it, and then loop back to the start of the while, where $y will get a new value using the rand() function. If they're not equal (therefore satisfying the if condition), then $y is printed, and the while loop is broken.

 

Hope that helps.

 

Denno

Thank you very much for the quick and enlightening answer. I knew i needed a while loop, but wasn't sure how to fit the "if" into it.

 

Lets say i would expand this to dealing other players hole cards, this would be kind of a hassle in the big picture, so is there a way to just remove the given cards completely from the array?

 

Thanks

Ok so I've been trying to figure this out, how to change the contents of the array, but it doesn't seem to be working for me.

The first one will change fine, however when getting the second or further cards, it seems to pass the original array back through.

I reckon the reason for this is because the array that I pass to the getRandom() function is the one that's edited. This array isn't returned back to the original function to change the original array.

I'll give you the code I have now, so you can see where I was headed with it, however the problem is being able to edit the original array still.. Once that is figured out, I reckon using this kind of layout will make it super simple to add as many players as you want. And should make the code nice and neat too.

<?php
function poker() {
$cards = array("Ah", "Ac", "Ad", "As",
                       "2h", "2c", "2d", "2s",
                       "3h", "3c", "3d", "3s",
                       "4h", "4c", "4d", "4s",
                       "5h", "5c", "5d", "5s",
                       "6h", "6c", "6d", "6s",
                       "7h", "7c", "7d", "7s",
                       "8h", "8c", "8d", "8s",
                       "9h", "9c", "9d", "9s",
                       "Th", "Tc", "Td", "Ts",
                       "Jh", "Jc", "Jd", "Js",
                       "Qh", "Qc", "Qd", "Qs",
                       "Kh", "Kc", "Kd", "Ks");
$x = getRandom($cards);
echo "-" . $x . " ";
 $y = getRandom($cards);
 echo "<br/>-" . $y;
$z = getRandom($cards);
echo "<br/>-" . $z;

echo "<br/>";
for ($i=0 ; $i<52 ; $i++){
	echo $cards[$i] . " | ";
}
}

function getRandom($cards){

while(1){
	$rand = rand(0,51);
	$player = $cards[$rand];
	if($player != 0){
		$cards[$rand] = 0;
		break;
	}
}

return $player;
}
poker();
   ?>

 

Thinking about your original question, I have probably provided you with no useful information at all, as I'm stuck with how to unset an array too lol.

 

Sorry mate.. I'll continue to look into it myself, for my own benefits. If I come up with something, I'll let you know, but don't hold your breath.

 

Denno

 

after a quick read through, i have a couple thoughts/options:

 

1. instead of removing elements from the array, have a value to indicate whether they have been 'taken from the deck' or not.

 

maybe initialize the array like so:

 

cards = array("Ah"=>0, "Ac"=>0, etc...

 

then update value to 1 when card is 'taken'.

 

2. or randomize the deck array and use array_pop() or array_shift() to pull a card off of the end of the array.

 

/end 2 pennies.

The first one will change fine, however when getting the second or further cards, it seems to pass the original array back through.

 

this is because each time the function is called, it is 'reborn' with the reset array. this could be avoided by setting the array outside of the function and making it global inside the function... sort of making the function pointless.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.