php - Pulling random numbers from an array -


i'm working on "card game" in cards randomly dealt 4 suites 4 players. created 2 separate arrays, 1 13 cards, , 1 4 suites. below code have 1 player.

arrays:

<?php   $suite=array("clubs", "diamonds", "hearts", "spades");   $random_suite=array_rand($suite, 4); ?>  <?php   $card=array("ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king");   $random_card=array_rand($card,5); ?> 

if statements (don't believe problem exists here):

<?php  if (!is_numeric($_get['players'])) { echo "you did not enter value number of players; click 'return' button , enter number."; } elseif ($_get['players'] < 1) { echo "you entered number less 1 minimum allowed; click 'return' button enter number."; } elseif ($_get['players'] > 4) { echo "you entered number greater 4 maximum allowed; click 'return' button enter number."; } else {   echo "<br>dealing cards 4 players   <br>hand player number 1: 

combining arrays:

  <br>{$card[$random_card[0]]} of {$suite[$random_suite[0]]}   <br>{$card[$random_card[1]]} of {$suite[$random_suite[1]]}   <br>{$card[$random_card[2]]} of {$suite[$random_suite[2]]}   <br>{$card[$random_card[3]]} of {$suite[$random_suite[3]]}   <br>{$card[$random_card[4]]} of {$suite[$random_suite[4]]}"; } ?> 

using <?php $suite=array("clubs", "diamonds", "hearts", "spades"); $random_suite=array_rand($suite, 4); ?> gives me this, makes sense number being 4. however, if change 5 how many cards need dealt, this, because there's 4 suites.

so, what's wrong code?

bonus: need figure out how make cards dealt unique. can't have 2 ace of spades. if me well, i'd grateful.

if want given number of cards randomised deck of cards, better idea create array every card in deck of cards.

<?php $suits = array("clubs", "diamonds", "hearts", "spades"); $cards = array("ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king");  $deck = array(); foreach($suits $suit) {     foreach($cards $card) {         $deck[] = $card . ' of ' . $suit;     } } 

now can shuffle array , specific amount of cards need it

$num = 5; shuffle($deck); $cards = array_slice($deck, 0, $num); 

here's ideone example: https://ideone.com/wqe677

or can use array rand again

$cards = array_rand($deck, 5); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -