Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
BlackJack (21) - Random Unused Card Function..
Old 03-25-2009, 12:06 PM BlackJack (21) - Random Unused Card Function..
Average Talker

Posts: 15
Name: Declan Dowling
Trades: 0
Hello,

I'm creating a php blackjack (21) game, and need some help creating a function that will select a random card, that is not already in use.

Ultimately I want to be able to define a new, unused card as simply as going:

$new_card = newCard();

I've looked at a few ways of going about this, and really just need someone to help me put it together.

PHP Code:
function newCard(){

$all_cards    range(152);
$used_cards    = array();
$card        rand($all_cards[0], $all_cards[51]);

while    (
in_array($card$all_cards)){
// not sure what to do here to here.
}

$used_cards array_push($used_cards$card);
// I want this to add whatever card to the used cards array, so it can't be used again.
return $card;

I'd be over the moon if someone could help me with this !
Declan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-25-2009, 01:08 PM Re: BlackJack (21) - Random Unused Card Function..
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
The way I would do it would be to build an array of integers, where each item corresponds to the point value of a card, and then shuffle that array.
PHP Code:
$cards = array();
$value 2;

//generate non-face cards
for($i 1$i <= 36$i++)
{
    
$cards[] = $value;
    if((
$i 4) == 0)
    {
          
$value++;
    }
}

//generate jacks queens kings
for($i 0$i 12$i++)
{
     
$cards[] = 10;
}

//generate aces
for($i 0$i 4$i++)
{
     
$cards[] = 11;
}

shuffle($cards); 
I haven't tested this but I think it should do the trick. Use array_pop to get items from the array.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-25-2009 at 01:11 PM.. Reason: Fixed bug
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-25-2009, 02:09 PM Re: BlackJack (21) - Random Unused Card Function..
Average Talker

Posts: 15
Name: Declan Dowling
Trades: 0
Thats a very nice script, i tested it out + it only produced 4 of each number.
After that it just stopped echoing. All working well.

So if I were to put that into a function, and each time I wanted a new card, simply do. $var = function(); it would work?
Declan is offline
Reply With Quote
View Public Profile
 
Old 03-25-2009, 02:17 PM Re: BlackJack (21) - Random Unused Card Function..
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You would probably have to use it in an object, since it is not static.
PHP Code:
class Dealer
{
     private 
$cards;
     function 
__construct()
     {
          
$cards = array(); 
          
$value 2

           
//generate non-face cards 
           
for($i 1$i <= 36$i++) 
           { 
                
$cards[] = $value
                if((
$i 4) == 0
                { 
                     
$value++; 
                } 
           } 

           
//generate jacks queens kings 
          
for($i 0$i 12$i++) 
          { 
               
$cards[] = 10
          } 

          
//generate aces 
          
for($i 0$i 4$i++) 
          { 
              
$cards[] = 11
          } 

         
shuffle($cards);
         
$this->cards $cards;
     }

     function 
deal()
     {
          return 
array_pop($this->cards);
     }
}

$dealer = new Dealer();
$myCard $dealer->deal(); 
Something like that should work.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-25-2009, 02:26 PM Re: BlackJack (21) - Random Unused Card Function..
Average Talker

Posts: 15
Name: Declan Dowling
Trades: 0
Works great! Thanks alot

Last edited by Declan; 03-25-2009 at 06:38 PM..
Declan is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to BlackJack (21) - Random Unused Card Function..
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45586 seconds with 12 queries