Scripts
Kaarten-delen
Deze 2 samengepakte functies kan je gebruiken om een kaartspel te maken. Je kan aangeven hoeveel spelers er zijn, hoeveel kaarten een speler moet krijgen, hoeveel pakjes kaarten er zijn en de map aangeven waar de kaarten staan. De kaarten moeten dan wel genummerd zijn van 1 t/m 52 (spel zonder jokers dus), bijvoorbeeld voor pokeren.
kaartendelen
<?php
error_reporting(E_ALL);
ini_set('error_reporting',1);
/**
* @author Robert Deiman
* @copyright 2008
* @param $iPlayer_number integer Used for setting the number of players in the game
* @param $iCard_number integer Used for setting the number of cards each player gets at the start
* @param $iDeck_number integer Used for setting the number of card decks used
*/
function game_start_cards($iPlayer_number = 4, $iCard_number = 2, $iDeck_number = 1){
// create an array with the cards
$aCards = range(1,52*$iDeck_number);
// shake the cards
shuffle($aCards);
//get every player his set of cards
for($kaartaantal = 1; $kaartaantal <= $iCard_number; $kaartaantal++){
for($player=1;$player<=$iPlayer_number;$player++){
// get the next card from stack and give it to the player, the card will be gone from the stack
$aPlayercards[$player][] = array_shift($aCards);
}
}
//add stack_item to the array containing the cards from all players
$aPlayercards['stack'] = $aCards;
return $aPlayercards;
}
/**
* @author Robert Deiman
* @copyright 2008
* @param $aCards_input integer The input array from the function above. Contains players and their cards.
* @param $sCards_folder string Location or folder where pictures of the cards can be found.
*/
function showcards($aCards_input, $sCards_folder = 'cards/'){
//set empty return value for preventing an error from occuring.
$sReturn_value = '';
//first get all players from the array. The player contains an array with their cards.
foreach($aCards_input as $iPlayer => $value){
$sReturn_value .= 'Player'.$iPlayer.': ';
// count the number of cards the player has
$iCount_cards = count($aCards_input[$iPlayer]);
// set counter variable, that makes the different items to be devided by a comma.
$iCount_times = 1;
// set the player cards in the returnvalue.
foreach($aCards_input[$iPlayer] as $sCardkey => $iCardvalue){
if($iPlayer != 'stack'){
$sCardvalue = $sCardvalue % 52;
$sReturn_value .= '<img src="'.$sCards_folder.$sCardvalue.'.jpg" style="border: 1px solid black;" />';
}
else{
$sReturn_value .= $sCardvalue;
}
if($iCount_cards > $iCount_times){
$sReturn_value .= ',';
}
$iCount_times++;
}
$sReturn_value .= '<br /><br />';
}
//return the created value
return $sReturn_value;
}
echo showcards(game_start_cards(),'kaarten/img/');
?>
Reacties
0