kaartendelen
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?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/');
?>
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/');
?>