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

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: only want 2 numbers

Post by John Cartwright »

danwguy wrote:So after reading a little, it seems like I could say "global $face, $suit;" then I should be able to echo those two anywhere else on the page or in any included pages right? Or would I have to define them as global each time, like " case 'J': global $face = "Jack of "; break;" and so on.
Do not use globals, ever. They will result in nasty code smells, and hard to track bugs.
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 would need to define them as global anytime you want to refer to the global scope. The simplest approach would be to define them outside, in the "global" scope, then declare them with the "global" keyword in the deal() function.

That said, you should almost never use global variables - it's bad form because they can make it really difficult to determine where the global variable is getting initially defined & modified.

If you just want to output the name of the cards, the best solution would be to have deal() return an array with each element being the name of one card. In your output code, iterate through that array and echo the name of each card. You shouldn't need globals for this.
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 »

Ok, so I have pretty much everything working, it's showing me exactly what I want and working great. I am using globals right now, I totally understand it's a bad idea, but really it's not going anywhere but my desk and I'll refine it and polish it up and get rid of the globals once it's done and actually working. The thing I am stuck on now is how to remove an item from an array using a variable, i.e.

Code: Select all

$array = array('1D', '2D', '3D');
$first = array_flip($array);
$number = array_rand($first);
$value = substr($number,0,1);
$suit = substr($number,1,1);
switch($value) {
   case '1':
      $cvalue = "One of ";
      break;
    //etc...
}
switch($suit) {
   case 'D':
      $suits = "Diamonds";
      break;
     //etc...
}
echo "You have a " . $value . " " . $suit'
I tried using unset($array[$number]); but that gives an error, how can I remover $number from $array properly?
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 »

That should do it - what error are you getting?
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 »

Warning: Illegal offset type in unset in C:\WEB_ROOT\maybe2.php on line 84
the exact code I am using is

Code: Select all

unset($this->working[$fcards]);
Do you think it could be because I am using $fcards = array_rand($this->working, $number); so it's getting back more than one result?
Last edited by danwguy on Tue Feb 15, 2011 5:32 pm, 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 »

That would mean that $fcards is not a scalar value - probably an 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 »

ok so I did some testing and now more confused than ever. I was able to make this work...

Code: Select all

$array = array('1', '2', '3', '4'); $rand = array_rand($array); unset($array[$rand]); echo $rand; print_r($array);
and it properly got rid of whatever number was in the $rand variable, but for some reason the following code will not get rid of the number in the array, any ideas?

Code: Select all

$array = array('1D', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', '10D', 'JD', 'QD', 'KD', '1H', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', '10H', 'JH', 'QH', 'KH', '1C', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '10C', 'JC', 'QC', 'KC', '1S', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', '10S', 'JS', 'QS', 'KS');
$flip = array_flip($array);
$card = array_rand($flip);
$flipback = array_flip($array);
unset($array[$card]);
I have tried it without the $flipback part as well, whatever $card picks will not unset from that array, I am a little confused. I think it has something to do with the array_flip command but not sure, any insight would be greatly appreciated.

EDIT: Nevermind, I realized I'm changing the array name with the $flip command so now my array is called $flip, so I need to unset my $card from $flip, not from $working.
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 everything works great, I can select to deal one card or multiple cards at a time, they all get removed from the array as they are picked and echo'd out to the screen. My only, and last question is... What is the point of having the class object? I get that you can call to it multiple times and multiple ways and all that jazz, but if you have a function in the class and that function outputs a variable, you can't access that variable anywhere. So I don't quite understand the usability of a class unless you echo things from the function within the class or define globals from within the function. Can anyone shed some light on this for me please? oh and here's the final Deck-class.php script, let me know what you think. Thanks again.

Code: Select all

<?php
class Deck
{
	private $original = array('1D', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', '10D', 'JD', 'QD', 'KD', '1H', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', '10H', 'JH', 'QH', 'KH', '1C', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '10C', 'JC', 'QC', 'KC', '1S', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', '10S', 'JS', 'QS', 'KS');
	private $working = array();
	
	public function __construct()
	{
		$this->working = array_flip($this->original);
	}
	
	public function deal($number)
	{
		if($number <= '1')
		{
			$fcards = array_rand($this->working);
			unset($this->working[$fcards]);
			$face = substr($fcards,0,1);
			$suit = substr($fcards,1,1);
			if($suit == '0') {
				$face = substr($fcards,0,2);
				$suit = substr($fcards,2,1);
			}
			switch($face)
			{
				case '1':
					echo "Ace of ";
					break;
				case '2':
					echo "2 of ";
					break;
				case '3':
					echo "3 of ";
					break;
				case '4':
					echo "4 of ";
					break;
				case '5':
					echo "5 of ";
					break;
				case '6':
					echo "6 of ";
					break;
				case '7':
					echo "7 of ";
					break;
				case '8':
					echo "8 of ";
					break;
				case '9':
					echo "9 of ";
					break;
				case '10':
					echo "10 of ";
					break;
				case 'J':
					echo "Jack of ";
					break;
				case 'Q':
					echo "Queen of ";
					break;
				case 'K':
					echo "King of ";
					break;
		}
		switch ($suit)
		{
			case 'D':
				echo "Diamonds <br />";
				break;
			case 'H':
				echo "Hearts <br />";
				break;
			case 'C':
				echo "Clubs <br />";
				break;
			case 'S':
				echo "Spades <br />";
				break;
		}
		}else{
			for($i=0; $i<$number; $i++) 
			{
				$fcards = array_rand($this->working);
			unset($this->working[$fcards]);
			$face = substr($fcards,0,1);
			$suit = substr($fcards,1,1);
			if($suit == '0') {
				$face = substr($fcards,0,2);
				$suit = substr($fcards,2,1);
			}
			switch($face)
			{
				case '1':
					echo "Ace of ";
					break;
				case '2':
					echo "2 of ";
					break;
				case '3':
					echo "3 of ";
					break;
				case '4':
					echo "4 of ";
					break;
				case '5':
					echo "5 of ";
					break;
				case '6':
					echo "6 of ";
					break;
				case '7':
					echo "7 of ";
					break;
				case '8':
					echo "8 of ";
					break;
				case '9':
					echo "9 of ";
					break;
				case '10':
					echo "10 of ";
					break;
				case 'J':
					echo "Jack of ";
					break;
				case 'Q':
					echo "Queen of ";
					break;
				case 'K':
					echo "King of ";
					break;
		}
		switch ($suit)
		{
			case 'D':
				echo "Diamonds <br />";
				break;
			case 'H':
				echo "Hearts <br />";
				break;
			case 'C':
				echo "Clubs <br />";
				break;
			case 'S':
				echo "Spades <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 »

The class helps to encapsulate functionality, and facilitates variable passing in an easier way. The $working variable is an example of that. You don't need to create that variable & have it returned by deal() in order to have the dealt cards removed - you can do all that intra-class.

I'm not sure what you mean by "output a variable". Your deal() function can be updated to return an array of the dealt cards. Finding tutorials & articles about OOP will shed some light on its benefits.
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 »

in simplest term I guess something like this shows what I mean...

Code: Select all

Class Number
{
   private $array = range(1, 52);
  
   public function newnumber($number)
   {
      $ynumber = array_rand($array, $number)
      foreach($ynumber as $tnumber) {
      switch($ynumber)
      {
          case '1':
             $fnumber = "something I want on the screen here";
             break;
          //etc...
      }
    }
}
Then I have a form that says how many numbers do you want and passes newnumber(their input number here); but after all that coding the $fnumber variable is useless to me, I cannot call it to say whatever I wanted it to say. so the whole thing is useless, because I can't know what the newnumber function output unless I make the output global. At least that's how I see it now, I have read a few pages about that term you posted "Variable Scope" and the only conclusion I can come up with is I have to use global to retrieve anything that $fnumber variable. I hope that makes sense.
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 »

2 ways you can do that. The important thing to note is that generally you wouldn't want the class to say anything. You'd want the class to do some work, then your display code should then interpret that. So if your code determines 1 or 0, but you want to output the english equivalent, don't have your class output "One" or "Zero", have it return 1 or 0, and have your display code interpret that to mean "One" or Zero".

Anyway, there are 2 ways to modify your code to get what you want.

Just have your function return the value

Code: Select all

Class Number
{
   private $array = range(1, 52);
 
   public function newnumber($number)
   {
      $ynumber = array_rand($array, $number)
      foreach($ynumber as $tnumber) {
      switch($ynumber)
      {
          case '1':
             $fnumber = "something I want on the screen here";
             break;
          //etc...
      }
      return $fnumber;
    }
}

$Number = new Number();
echo $Number->newNumber(3); 
Or, have your class set an object property:

Code: Select all

Class Number
{
   private $array = range(1, 52);
   public $fnumber;
 
   public function newnumber($number)
   {
      $ynumber = array_rand($array, $number)
      foreach($ynumber as $tnumber) {
      switch($ynumber)
      {
          case '1':
             $this->fnumber = "something I want on the screen here";
             break;
          //etc...
      }
    }
}

$Number = new Number();
$Number->newNumber(3);
echo $Number->fnumber; 
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 »

Ok, that makes sense, thank you so much for your help. I really learned a lot during this whole thing.
Post Reply