forked from sespinoza/cucumber_lab3
/
silverjack.php
113 lines (95 loc) · 3.55 KB
/
silverjack.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
//The array of file paths to cards in the entire deck
$deck = array();
//The array of player fo this game
$players = array('Richard Ciampa' => array('cards' => array(), 'handTotal' => '', 'pic' => './img/pl3.png', 'win' => 'false'),
'Andrew Richardson' => array('cards' => array(), 'handTotal' => '', 'pic' => './img/pl1.jpg', 'win' => 'false'),
'Susan Espnoza' => array('cards' => array(), 'handTotal' => '', 'pic' => './img/pl4.jpg', 'win' => 'false'),
'Brandon Saletta' => array('cards' => array(), 'handTotal' => '', 'pic' => './img/pl2.jpg', 'win' => 'false'));
//Get a reference to all the cards in the different directories
$cardDirs = array("./img/cards/clubs/*", "./img/cards/diamonds/*", "./img/cards/hearts/*", "./img/cards/spades/*");
//Collect the files in the $deck array
for ($i = 0; $i < 4; $i++) {
// Add the cards from each directory to the array
foreach (glob($cardDirs[$i]) as $file) {
array_push($deck, $file);
}
}
//Now lets shuffle the $deck array
shuffle($deck);
//Deal the cards for each user
function deal($players) {
//Call the global var for the deck array
global $deck;
foreach ($players as $player => $playerValues) {
$handTotal = 0;
//The hand card sum. The closest to 45 with going over wins
$handValuesDealt = array();
//We use a do loop becuase we need to do it once for sure
do {
//Get the card file path information to store if it is usable
$cardDealt = $deck[count($deck) - 1];
/*
* Get the value of the card to both compare and add to the
* total if it is not a duplicate value.
*
* The game rules state that each player can not recieve duplicate
* card values
*/
$cardValue = substr(substr($deck[count($deck) - 1], (strrpos($deck[count($deck) - 1], "/") + 1)), 0, -4);
//Check that the card value has not already been dealt from the deck
if (!isValueDealt($handValuesDealt, $cardValue)) {
//Add the card to the hand
array_push($handValuesDealt, $cardValue);
//Remove the card from the deck
array_pop($deck);
//Add the sum to the total
$handTotal += $cardValue;
$playerValues['handTotal'] += $cardValue;
//Add the card url to the array
array_push($playerValues['cards'], $cardDealt);
} else {//We will reshuffle the deck so we get no duplicates
shuffle($deck);
}
} while($handTotal < 42 && (count($handValuesDealt) < 6));
//Set the player information for the return
$players[$player] = $playerValues;
}
//Call findWinners() to set the Win boolean
$players = findWinners($players);
//return the $players array to the calling code
return $players;
}
//We need to see if the value of the card has been dealt
function isValueDealt($handValuesDealt, $cardValue) {
foreach ($handValuesDealt as $value) {
if ($cardValue == $value) {
return TRUE;
}
}
return FALSE;
}
/*
* Find all the winners in this round of Silverjack and set the
* boolean value to true for the UI. There can be more than one
* winner
*/
function findWinners($players){
//Hold the highest winning hand here
$highestHand = 0;
//Find the hightest hand at the table
foreach ($players as $hand) {
if(($highestHand < $hand["handTotal"]) && ($hand["handTotal"] <= 42)){
$highestHand = $hand["handTotal"];
}
}
//Set the boolean value of each winner to true
foreach ($players as $player => $hand) {
$players[$player]["win"] = ($hand["handTotal"] == $highestHand) ? "TRUE" : "FALSE";
}
//Return the array for players with the Win boolean set
return $players;
}
//Lets deal a round of cards
$players = deal($players);
?>