-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blackjack.php
80 lines (64 loc) · 1.81 KB
/
Blackjack.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
<?php
class Blackjack{
const CARD_ERROR = 'Error: error card Values, RETRY PLEASE';
/**
* give result for BlackJacks Game----- <21 loose and >21 win
*/
public static function overTwentyOne(array $_params)
{
$tmp_gamerScore = Blackjack::checkCardValues($_params);
$gamerResult = Blackjack::giveGamerScore($tmp_gamerScore);
var_dump($gamerResult);
return $gamerResult;
}
/**
* check the value of given array
*/
public static function checkCardValues(array $_values)
{
foreach($_values as $card){
if ( filter_var($card, FILTER_VALIDATE_INT) && $card<2){
echo self::CARD_ERROR;
return die();
// echo $card;
}elseif( filter_var($card, FILTER_VALIDATE_INT)){
$numberCard[] = $card;
}
else{
$numberCard[] = Blackjack::giveEquivalentCardNumber($card);
}
}
var_dump($numberCard);
die();
return array_sum($numberCard);
}
/**
* take card string value and return his exact int value
*/
public function giveEquivalentCardNumber(string $_stringCard)
{
if( $_stringCard == 'J' || $_stringCard == 'K'){
return 10;
}elseif($_stringCard == 'Q'){
return 1;
}elseif($_stringCard == 'A'){
return 1;
}
}
/**
* get the tmp_score and give if gamer win or loose
*/
public static function giveGamerScore($_score)
{
if( $_score >= 21 ){
echo 'winner';
return true;
}else{
echo 'looser';
return false;
}
}
}
// test the function
$cards = ["J", "Q", 1];
Blackjack::overTwentyOne($cards);