Jump to content

random numbers and cards


hamburgerlove413

Recommended Posts

Hello,

 I wasn't sure if there was a "beginner php questions" area, so sorry if this is not put in the right spot, but I have a very basic "game" where it prints out a randomly generated low card and high card. The low card has to be of a lower value than the high card, and they cant equal each other or be within 1 of each other. I have this all working fine. The problem is, when I try to add in any kind of way to change numbers, say, 11-14 to Jack, Queen, King, Ace, the whole lower card being lower than the high card goes out the window. I feel like I'm maybe approaching this from the completely wrong direction or something, so if anyone could give me any kind of advice, I'd be very grateful.

<?php

function deal() {

	$card = rand(1,52);
	
	return $card;
	
}

$highCard = deal();

do  {
	
	$lowCard = deal();

}


while ($lowCard > $highCard || $highCard - $lowCard <= 1);


function cardValue($card) {
	if ($card % 14 === 0 || $card % 14 === 1) {
		return $card = "an Ace";
	} else if ($card % 14 === 13) {
		return $card = "a King";
	} else if ($card % 14 === 12) {
		return $card = "a Queen";
	} else if ($card % 14 === 11) {
		return $card = "a Jack";
	} else {
		return $card % 13;
	}
}

print "Your low card is " .cardValue($lowCard). " and your high card is " .cardValue($highCard);



?>
Link to comment
Share on other sites

Your card value function is incorrect. Just do a quick loop over your 1-52 range and you can see that:

foreach (range(1,52) as $card){
	echo cardValue($card).PHP_EOL;
}
/*-----
an Ace
2
3
4
5
6
7
8
9
10
a Jack
a Queen
a King
an Ace
an Ace
3
4
5
6
7
8
9
10
11
a Jack
a Queen
a King
an Ace
an Ace
4
5
6
7
8
9
10
11
12
a Jack
a Queen
a King
an Ace
an Ace
5
6
7
8
9
10
11
12
0
-----*/
If you're not going to have the suit in there, you may as well just do a range of 0-12 (your range should be zero-based) and test that way. If you want to do the suit, then use range 0-51 and first extract the suit by doing $card/13, and then check the value of $card%13 to get the face value.

function cardValue($card) {
	$suit = $card/13;
	$value = $card%13;
	
	if ($value == 0) $value = 'Ace';
	else if ($value == 10) $value = 'Jack';
	else if ($value == 11) $value = 'Queen';
	else if ($value == 12) $value = 'King';
	else $value++;

	$suitList=['Heart','Spade','Club','Diamond'];
	return $value.' of '.$suitList[$suit];
}

foreach (range(0,51) as $card){
	echo cardValue($card).PHP_EOL;
}
Link to comment
Share on other sites

To expand on kicken's post, you need to make some additional changes. You use a while() loop to find the low card. But, that is inefficient to simply do a loop looking for a random value that is lower. Plus, and more importantly, if the high card pulled was a 1, you would get caught in an infinite loop since you could never find a lower card! The high card must have a minimum value of 3 and the low card must have a maximum value of the high card - 2.  Your function to get a card needs to accept parameters so you can get a card you need the first time and avoid these problems.

 

Since the high and low cards MUST be different, you don't need any logic when determining the suit to prevent duplicates, since the value will never be the same. So, I'd just randomly pick a suite for each. Here's my take

<?php

function deal($min, $max)
{
    //Determine the suite
    $suits = array('Hearts', 'Spades', 'Clubs', 'Diamonds');
    $suit = $suits[array_rand($suits)];
    //Determine the value and 'face'
    $value = rand($min, $max);
    $faceCards = array(11 => 'Jack', 12 => 'Queen', 13 => 'King', 14 => 'Ace');
    $face = (array_key_exists($value, $faceCards)) ? $faceCards[$value] : $value;
    //Return the results as an array
    $result = array('value' => $value, 'face' => $face, 'suite' => $suit);
    return $result;
}

function cardValue($cardAry)
{
    return "{$cardAry['face']} of {$cardAry['suite']}";
}

$highCard = deal(3, 14); //Min of 3, Max of Ace
$lowCard = deal(1, $highCard['value']-2); //Min of 1, mMax of High card - 2
print "Your low card is " .cardValue($lowCard). " and your high card is " .cardValue($highCard);

?>
Edited by Psycho
Link to comment
Share on other sites

Ok, I'm trying to understand this. Is this correct?

 

you created a function called deal that passes the arguments min and max.

 

In this function, you create a variable called suits which is assigned the value of an array consisting of the four suits.

 

you then create a variable called suit which takes a random item from the suits array.

 

Then you create a variable called value which randomly gets a number between min and max.

 

You then create another array called faceCards which assigns the values to their corresponding strings.

 

Then you check to see if the random number from value is located in the faceCards array. If it is, replace that with the string that is associated with that number. else, use the original value from $value.

 

You then put the value, whether its a face card or not, and its suit in an array called result which is what the function returns.

 

A function is created called cardValue which passes the argument cardAry. It returns face and the suit.

 

A variable called highCard is created that calls the deal function and sets min and max to 3 and 14.

 

A variable called lowCard is created calls the deal function and sets the min to 1 and the max to the high card's value - 2.

 

These two come back with values that are arrays, which includes all of the information needed.

 

finally, you send the lowCard and highCard through cardAry, which separates out the array information into face and suit and then print the results.

Link to comment
Share on other sites

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.