Page 3 of 3
Re: only want 2 numbers
Posted: Tue Feb 15, 2011 4:18 pm
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.
Re: only want 2 numbers
Posted: Tue Feb 15, 2011 4:26 pm
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.
Re: only want 2 numbers
Posted: Tue Feb 15, 2011 5:04 pm
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?
Re: only want 2 numbers
Posted: Tue Feb 15, 2011 5:17 pm
by pickle
That should do it - what error are you getting?
Re: only want 2 numbers
Posted: Tue Feb 15, 2011 5:26 pm
by danwguy
Warning: Illegal offset type in unset in C:\WEB_ROOT\maybe2.php on line 84
the exact code I am using is
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?
Re: only want 2 numbers
Posted: Tue Feb 15, 2011 5:28 pm
by pickle
That would mean that $fcards is not a scalar value - probably an array.
Re: only want 2 numbers
Posted: Wed Feb 16, 2011 11:06 am
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.
Re: only want 2 numbers
Posted: Thu Feb 17, 2011 11:18 am
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;
}
}
}
}
}
?>
Re: only want 2 numbers
Posted: Thu Feb 17, 2011 2:03 pm
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.
Re: only want 2 numbers
Posted: Thu Feb 17, 2011 2:17 pm
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.
Re: only want 2 numbers
Posted: Thu Feb 17, 2011 2:27 pm
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;
Re: only want 2 numbers
Posted: Thu Feb 17, 2011 2:33 pm
by danwguy
Ok, that makes sense, thank you so much for your help. I really learned a lot during this whole thing.