only want 2 numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

only want 2 numbers

Post by danwguy »

So here is my problem, I am messing around with a script, just trying to learn things, so I figured I would make a quick desk of card so to speak and show 2 random cards from it. Now this is not really anything just random number if its 11 echo Jack something stupid like that. The problem is when the 2 numbers being echo'd to the screen are between 0-10 it echos 2 numbers, but if one of them is 11 or above it echos 2 numbers plus the "jack" or "queen" or whatever the third number is. I only want 2 numbers to show up regardless of what they are. So here's the script, leet me know what you think please, and thank you in advance for any help.

Code: Select all

<?php
$n = 2;

$data = range(1, 14);
$rand = array_rand($data, $n);

for($i=0; $i<$n; $i++)
{
if($rand[$i] == 13) {
echo "jack<br />";
}
if($rand[$i] == 14) {
echo "Ace<br />";
}
if($rand[$i] == 12) {
echo "Queen<br />";
}
if($rand[$i] == 11) {
echo "Jack<br />";
}
echo $rand[$i]."<br>";
}
?> 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: only want 2 numbers

Post by pickle »

You're missing an "else" condition.

You do all those ifs() to determine whether to output a face card or not, but regardless of the result of that, you always output the number as well. If you put an else in there, then the number will only be output if it's not a face card.

Some constructs that would clean up your code are foreach() and switch():

Code: Select all

<?php
$n = 2;

$data = range(1, 14);
$rand = array_rand($data, $n);

foreach($rand as $number)
{
  switch($number)
  {
    case 11:
      echo 'Jack';
      break;
    case 12:
      echo 'Queen';
      break;
    case 13:
      echo 'King';
      break;
    case 14:
      echo 'Ace';
      break;
    default:
      echo $number;
      break;
  }
}
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

Re: only want 2 numbers

Post by anantha »

Code: Select all

$n = 2;

$data = range(1, 14);
$rand = array_rand($data, $n);

for($i=0; $i<$n; $i++)
{
if($rand[$i] == 13) {
echo "jack<br />";
}elseif($rand[$i] == 14) {
echo "Ace<br />";
}elseif($rand[$i] == 12) {
echo "Queen<br />";
}elseif($rand[$i] == 11) {
echo "Jack<br />";
}else
echo $rand[$i]."<br>";
}



danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: only want 2 numbers

Post by danwguy »

pickle wrote:You're missing an "else" condition.

You do all those ifs() to determine whether to output a face card or not, but regardless of the result of that, you always output the number as well. If you put an else in there, then the number will only be output if it's not a face card.

Some constructs that would clean up your code are foreach() and switch():

Code: Select all

<?php
$n = 2;

$data = range(1, 14);
$rand = array_rand($data, $n);

foreach($rand as $number)
{
  switch($number)
  {
    case 11:
      echo 'Jack';
      break;
    case 12:
      echo 'Queen';
      break;
    case 13:
      echo 'King';
      break;
    case 14:
      echo 'Ace';
      break;
    default:
      echo $number;
      break;
  }
}
?>
That worked great, only problem is if I try to add a

Code: Select all

<br /> 
to the default switch it breaks the whole thing and won't work. I.e.

Code: Select all

 default:
  echo $number '<br />';
  break;
How would I add a break between the numbers? it works fine if I add it to the Jack Queen King and Ace parts, but everytime I add to the default one it breaks the whole file. Thank you again, I have always wanted to learn the whole switch and case way of coding things, just never got a chance to and this has helped greatly.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: only want 2 numbers

Post by danwguy »

Nevermind, I'm an idiot... forgot to put a period after the $number variable so it looks like this now...

Code: Select all

default:
   echo $number . '<br />':
   break;
Thank you again. Now I just have to figure out how to add suits and make sure I don't echo the same card twice in the same round.... plus all that other coding stuff to make the whole game work, lol.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: only want 2 numbers

Post by danwguy »

ok so 2 questions left... for now... is there a way to get rid of 0 as being one of the numbers? I figured using the range(1, 14); would force a number between 1 and 14 but every now and again a 0 pops up, how do I get rid of that as an option? And question 2, I added the suits as an array and did a random number thing just like for the cards but the only suit that shows up is Spades, I have done it 30 times now and its always $number . ' of Spades, any insight or help please. Thank you, btw, here is the code now...

Code: Select all

<html><head>
<title>Basic Texas Hold 'em game</title>
<link rel="stylesheet" href="style.css" media="screen" />
</head>
<body>
<div id="header">
	<h1 style="color: #00ff00";>TEXAS HOLD 'EM</h1><br />
<p>pathetic and basic, but hopefully it works, good luck and have fun</p>
</div>
<div id="mycards">
<h3><center>Your Cards</canter></h3>
<?php
$n = 2;
$suits = range(1, 4);
$randsuit = array_rand($suits);
foreach($suits as $suit)
{
	switch($suit)
	{
		case 1:
			$finalsuit = "Diamonds";
			break;
		case 2:
			$finalsuit = "Hearts";
			break;
		case 3:
			$finalsuit = "Clubs";
			break;
		case 4:
			$finalsuit = "Spades";
			break;
	}
}

$data = range(1, 14);
$rand = array_rand($data, $n);

foreach($rand as $number)
{
	switch($number)
	{
		case 11:
			echo 'Jack of ' . $finalsuit . '<br />';
			break;
		case 12:
			echo 'Queen of ' . $finalsuit . '<br />';
			break;
		case 13:
			echo 'King of ' . $finalsuit . '<br />';
			break;
		case 14:
			echo 'Ace of ' . $finalsuit . '<br />';
			break;
		default:
			echo $number . ' of ' . $finalsuit .  '<br />';
			break;
	}
}
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: only want 2 numbers

Post by pickle »

Why a zero is popping up I have no idea, nor do I know why Spades always comes up.

To be honest, if you're trying to do something with a deck of cards, your easiest solution would be to manually code a 52 element array - one element for each card. That'll greatly reduce the complexity of a) finding if you're returning a valid card and b) determining if you're returning the same card twice.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: only want 2 numbers

Post by danwguy »

now I am thouroughly confused. Here is the script...

Code: Select all

<html><head>
<title>Basic Texas Hold 'em game</title>
<link rel="stylesheet" href="style.css" media="screen" />
</head>
<body>
<div id="header">
	<h1 style="color: #00ff00";>TEXAS HOLD 'EM</h1><br />
<p>pathetic and basic, but hopefully it works, good luck and have fun</p>
</div>
<div id="mycards">
<h3><center>Your Cards</canter></h3>
<?php
$n = 2;
$c = 1;
$suits = range(1, 4);
$random = array_rand($suits, $c);

foreach($random as $numbers)
{
	switch($numbers)
	{
		case 1:
			$finalsuit = "Diamonds";
			break;
		case 2:
			$finalsuit = "Hearts";
			break;
		case 3:
			$finalsuit = "Clubs";
			break;
		case 4:
			$finalsuit = "Spades";
			break;
		default:
			$finalsuit = "none";
			break;
	}
}

$data = range(1, 14);
$rand = array_rand($data, $n);

foreach($rand as $number)
{
	switch($number)
	{
		case 11:
			echo 'Jack of ' . $finalsuit . '<br />';
			break;
		case 12:
			echo 'Queen of ' . $finalsuit . '<br />';
			break;
		case 13:
			echo 'King of ' . $finalsuit . '<br />';
			break;
		case 14:
			echo 'Ace of ' . $finalsuit . '<br />';
			break;
		default:
			echo $number . ' of ' . $finalsuit .  '<br />';
			break;
	}
}
?>
I get these errors when I run it...
Warning: main() [function.main]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for '-8.0/no DST' instead in C:\WEB_ROOT\randomnumbertest2.php on line 18

Warning: Invalid argument supplied for foreach() in C:\WEB_ROOT\randomnumbertest2.php on line 18

Warning: main() [function.main]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for '-8.0/no DST' instead in C:\WEB_ROOT\randomnumbertest2.php on line 60

Notice: Undefined variable: finalsuit in C:\WEB_ROOT\randomnumbertest2.php on line 60
5 of

Warning: main() [function.main]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for '-8.0/no DST' instead in C:\WEB_ROOT\randomnumbertest2.php on line 60

Notice: Undefined variable: finalsuit in C:\WEB_ROOT\randomnumbertest2.php on line 60
8 of
That is the output on the page when I run that script. I am so freaking lost now, it makes no sense at all. Please help. :dubious:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: only want 2 numbers

Post by pickle »

I don't know what's going on with those timezone errors - you're doing nothing with datetime in this file.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: only want 2 numbers

Post by danwguy »

I just figured out pretty much all the stuff, it's running right now. Only question left is, I created an array with all 52 cards and have it showing 2 cards for me, showing but hiding 2 cards for dealer and 2 cards for another player, is there a way I can "deal" more cards from the array and make sure that it can't show "cards" that are already in play? The way it's written now the cards array and method of showing them is stored as a funtion called deal() so essentially I just call that function and it "deals" 2 cards with suits. I am going to make another function to "deal" the rest of the cards, but again, is there a way to ensure I don't show the same card twice?

EDIT: I have an idea but it depends on the answer to this question... is there a way to store the output of the function as a variable after it's echo'd onto the screen? i.e. I say

Code: Select all

div id="mcards" style="width: 306px; height: 100%; float: left; border: 1px solid #2a2a2a; padding: 1px;">
<strong><u style="text-align: center; margin-left: 100px;">Your Cards</u></strong><br />
<?php deal(); ?>
</div>
but right after calling the function, store whatever it outputs, as a variable so I can pass it to different pages and check against it before "dealing" anymore cards.
Last edited by danwguy on Fri Feb 11, 2011 11:31 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: only want 2 numbers

Post by pickle »

Make a copy of your original "deck" array when you deal. After a card is dealt, remove it from the copy array.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: only want 2 numbers

Post by danwguy »

But I won't know what cards are dealt until the function runs, and then it's all random so it would have to know what the function outputs, then remove that from the array, or just check the next function output for that number and re-deal accordingly.

EDIT: Could I say something like

Code: Select all

$yourcards = deal();
echo $yourcards;
then on my next deal do something like...

Code: Select all

if(deal() == $yourcards) { deal(); }
then continue on in that manner until all the cards are dealt?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: only want 2 numbers

Post by pickle »

This is where separation of display & "business" logic becomes important.

Your deal() function shouldn't echo anything. It should only determine the cards to deal. You should have separate code to take that information and display it. Separating the two types of logic makes the code overall, much easier to work with.

In this case, a class might be useful:

Code: Select all

class Deck
{
	private $original = array('1D','2D','3D'...etc);
	private $working = array();
	
	public function __construct()
	{
		$this->working = $this->original;//from this point on, any dealing & modification of the deck will be done on $this->working
	}
	
	public function deal($number)
	{
		//have this function return an array with $number elements
		//this function can also remove those returned cards from $working
	}
}
If you're just starting out classes may be a bit much, but I mention it anyway, as it's probably the ideal solution.

Another option would be to pass your copied deck to your function as a reference. That way, you can work on the array inside your function, and have those changes persist between different calls to deal()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: only want 2 numbers

Post by danwguy »

So I may sound dumb, but I have never really done OO php programming before so I have no idea how to do what you are saying. That's kind of this whoole thing is learning to code OO style. I can understand the functions and calling the functions but I am pretty lost on the class thing, and how you were saying "//have this function return an array with $number elements
//this function can also remove those returned cards from $working"
also I have absolutely no idea what "$this->working = $this->original;" means how to call/edit that after that... if that makes any sense.
is there any possible chance you would be kind enough to give me a little example? I am not asking to be spoon fed the code, I really want to learn this, but a little help would be greatly appreciated. Thank you for all your help so far.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: only want 2 numbers

Post by pickle »

If that all escapes you, then your best bet is to find OOP tutorials online. The subject matter is too vast to explain here - I wouldn't know where to begin.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply