|
8 | 8 | * One of the standard 52 playing cards |
9 | 9 | * Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King |
10 | 10 | */ |
11 | | -class Card{ |
12 | | - |
13 | | - const ACE = 100; |
14 | | - const JACK = 101; |
15 | | - const QUEEN = 102; |
16 | | - const KING = 103; |
17 | | - |
18 | | - /** |
19 | | - * The suit of this card |
20 | | - * @var Suit |
21 | | - */ |
22 | | - protected $suit; |
23 | | - |
24 | | - /** |
25 | | - * Face value |
26 | | - * @var int |
27 | | - */ |
28 | | - protected $faceValue; |
29 | | - |
30 | | - /** |
31 | | - * A new playing card |
32 | | - * @param int $faceValue the face value of the card. |
33 | | - * @param Suit $suit the suit of the card. |
34 | | - * |
35 | | - * @throws \InvalidArgumentException |
36 | | - */ |
37 | | - public function __construct($faceValue, Suit $suit){ |
38 | | - |
39 | | - if($this->isValidFaceValue($faceValue)){ |
40 | | - |
41 | | - $this->faceValue = $faceValue; |
42 | | - $this->suit= $suit; |
43 | | - |
44 | | - }else{ |
45 | | - throw new \InvalidArgumentException("The face value in not valide: $faceValue"); |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - |
50 | | - protected function isValidFaceValue($value){ |
51 | | - |
52 | | - if($value >= 2 && $value <= 10){ |
53 | | - return true; |
54 | | - } |
55 | | - |
56 | | - if($this->isFaceCardOrAce($value)){ |
57 | | - return true; |
58 | | - } |
59 | | - |
60 | | - return false; |
61 | | - } |
62 | | - |
63 | | - protected function isFaceCardOrAce($value){ |
64 | | - |
65 | | - return ( |
66 | | - $value == static::ACE |
67 | | - || |
68 | | - $value == static::JACK |
69 | | - || |
70 | | - $value == static::QUEEN |
71 | | - || |
72 | | - $value == static::KING); |
73 | | - |
74 | | - } |
75 | | - |
76 | | - /** |
77 | | - * Get the Face value of the card |
78 | | - * |
79 | | - * @return int face value |
80 | | - */ |
81 | | - public function value(){ |
82 | | - |
83 | | - return $this->faceValue; |
84 | | - } |
85 | | - |
86 | | - /** |
87 | | - * Get the Suit of the card |
88 | | - * |
89 | | - * @return Suit |
90 | | - */ |
91 | | - public function suit(){ |
92 | | - |
93 | | - return $this->suit; |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Get the suit name the card |
98 | | - * |
99 | | - * @return string |
100 | | - */ |
101 | | - public function suitName(){ |
102 | | - |
103 | | - return $this->suit->name(); |
104 | | - } |
105 | | - |
106 | | - /* |
107 | | - * Is this an Ace |
108 | | - * |
109 | | - * @return bool |
110 | | - */ |
111 | | - public function isAce(){ |
112 | | - |
113 | | - return $this->faceValue == static::ACE; |
114 | | - } |
115 | | - |
116 | | - /** |
117 | | - * Is this a King |
118 | | - * |
119 | | - * @return bool |
120 | | - */ |
121 | | - public function isKing(){ |
122 | | - |
123 | | - return $this->faceValue == static::KING; |
124 | | - } |
125 | | - |
126 | | - /** |
127 | | - * Is this a Queen |
128 | | - * |
129 | | - * @return bool |
130 | | - */ |
131 | | - public function isQueen(){ |
132 | | - |
133 | | - return $this->faceValue == static::QUEEN; |
134 | | - } |
135 | | - |
136 | | - /** |
137 | | - * Is this a Jack |
138 | | - * |
139 | | - * @return bool |
140 | | - */ |
141 | | - public function isJack(){ |
142 | | - |
143 | | - return $this->faceValue == static::JACK; |
144 | | - } |
145 | | - |
146 | | - /** |
147 | | - * Is Face card. True if the card is King,Queen or Jack |
148 | | - * |
149 | | - * @return bool |
150 | | - */ |
151 | | - public function isFaceCard(){ |
152 | | - |
153 | | - $face = $this->isKing() || $this->isQueen() || $this->isJack(); |
154 | | - |
155 | | - return $face; |
156 | | - } |
| 11 | +class Card |
| 12 | +{ |
| 13 | + |
| 14 | + const ACE = 100; |
| 15 | + const JACK = 101; |
| 16 | + const QUEEN = 102; |
| 17 | + const KING = 103; |
| 18 | + |
| 19 | + /** |
| 20 | + * The suit of this card |
| 21 | + * @var Suit |
| 22 | + */ |
| 23 | + protected $suit; |
| 24 | + |
| 25 | + /** |
| 26 | + * Face value |
| 27 | + * @var int |
| 28 | + */ |
| 29 | + protected $faceValue; |
| 30 | + |
| 31 | + /** |
| 32 | + * A new playing card |
| 33 | + * @param int $faceValue the face value of the card. |
| 34 | + * @param Suit $suit the suit of the card. |
| 35 | + * |
| 36 | + * @throws \InvalidArgumentException |
| 37 | + */ |
| 38 | + public function __construct($faceValue, Suit $suit) |
| 39 | + { |
| 40 | + if ($this->isValidFaceValue($faceValue)) { |
| 41 | + $this->faceValue = $faceValue; |
| 42 | + $this->suit= $suit; |
| 43 | + } else { |
| 44 | + throw new \InvalidArgumentException("The face value in not valide: $faceValue"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + protected function isValidFaceValue($value) |
| 50 | + { |
| 51 | + if ($value >= 2 && $value <= 10) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + if ($this->isFaceCardOrAce($value)) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + protected function isFaceCardOrAce($value) |
| 63 | + { |
| 64 | + return ( |
| 65 | + $value == static::ACE |
| 66 | + || |
| 67 | + $value == static::JACK |
| 68 | + || |
| 69 | + $value == static::QUEEN |
| 70 | + || |
| 71 | + $value == static::KING); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get the Face value of the card |
| 76 | + * |
| 77 | + * @return int face value |
| 78 | + */ |
| 79 | + public function value() |
| 80 | + { |
| 81 | + return $this->faceValue; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the Suit of the card |
| 86 | + * |
| 87 | + * @return Suit |
| 88 | + */ |
| 89 | + public function suit() |
| 90 | + { |
| 91 | + return $this->suit; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get the suit name the card |
| 96 | + * |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + public function suitName() |
| 100 | + { |
| 101 | + return $this->suit->name(); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + * Is this an Ace |
| 106 | + * |
| 107 | + * @return bool |
| 108 | + */ |
| 109 | + public function isAce() |
| 110 | + { |
| 111 | + return $this->faceValue == static::ACE; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Is this a King |
| 116 | + * |
| 117 | + * @return bool |
| 118 | + */ |
| 119 | + public function isKing() |
| 120 | + { |
| 121 | + return $this->faceValue == static::KING; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Is this a Queen |
| 126 | + * |
| 127 | + * @return bool |
| 128 | + */ |
| 129 | + public function isQueen() |
| 130 | + { |
| 131 | + return $this->faceValue == static::QUEEN; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Is this a Jack |
| 136 | + * |
| 137 | + * @return bool |
| 138 | + */ |
| 139 | + public function isJack() |
| 140 | + { |
| 141 | + return $this->faceValue == static::JACK; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Is Face card. True if the card is King,Queen or Jack |
| 146 | + * |
| 147 | + * @return bool |
| 148 | + */ |
| 149 | + public function isFaceCard() |
| 150 | + { |
| 151 | + $face = $this->isKing() || $this->isQueen() || $this->isJack(); |
| 152 | + |
| 153 | + return $face; |
| 154 | + } |
157 | 155 | } |
158 | | - |
0 commit comments