-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fix (drunkness)
- Loading branch information
Showing
4 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Hands; | ||
|
||
use Card\HandInterface; | ||
use Hands\Search\HandSearchInterface; | ||
|
||
class OnePair implements HandMatcherInterface | ||
{ | ||
const MULTIPLIER = 10; | ||
|
||
/** | ||
* @var HandSearchInterface | ||
*/ | ||
protected $twoOfAKindFinder; | ||
|
||
/** | ||
* @param HandSearchInterface $twoOfAKindFinder | ||
*/ | ||
public function __construct(HandSearchInterface $twoOfAKindFinder) | ||
{ | ||
$this->twoOfAKindFinder = $twoOfAKindFinder; | ||
} | ||
|
||
/** | ||
* @param HandInterface $hand | ||
* | ||
* @return bool|void | ||
*/ | ||
public function match(HandInterface $hand) | ||
{ | ||
$match = $this->twoOfAKindFinder->find($hand); | ||
|
||
if (!$match) { | ||
return; | ||
} | ||
|
||
if (count($match->getCards()) !== 2) { | ||
throw new \LogicException('A pair should be an Hand of size 2. Found: '.count($match->getCards())); | ||
} | ||
|
||
return new Score( | ||
$hand->discard($match)->keep(3)->insert($match), | ||
$match->getFaceValue() * self::MULTIPLIER | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Hands\Search; | ||
|
||
use Card\Card; | ||
use Card\Hand; | ||
use Card\HandInterface; | ||
|
||
class HandSearch | ||
{ | ||
/** | ||
* @param Card $cardToFind | ||
* @param HandInterface $hand | ||
* | ||
* @return Hand | ||
*/ | ||
public function search(Card $cardToFind, HandInterface $hand) | ||
{ | ||
$matches = 0; | ||
|
||
$cards = []; | ||
|
||
foreach ($hand->getCards() as $key => $card) { | ||
if ($card->getFaceValue() === $cardToFind->getFaceValue()) { | ||
++$matches; | ||
$cards[] = $card; | ||
} | ||
} | ||
|
||
return new Hand(...$cards); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Hands\Search; | ||
|
||
use Card\HandInterface; | ||
|
||
class TwoOfAKindFinder implements HandSearchInterface | ||
{ | ||
/** | ||
* @var HandSearch | ||
*/ | ||
protected $handSearch; | ||
|
||
/** | ||
* @param HandSearch $handSearch | ||
*/ | ||
public function __construct(HandSearch $handSearch) | ||
{ | ||
$this->handSearch = $handSearch; | ||
} | ||
|
||
/** | ||
* @param HandInterface $hand | ||
* | ||
* @return HandInterface|null | ||
*/ | ||
public function find(HandInterface $hand) | ||
{ | ||
if (count($hand->getCards()) < 2) { | ||
return; | ||
} | ||
|
||
$cards = $hand->getCards(); | ||
|
||
foreach ($cards as $card) { | ||
$matchingHand = $this->handSearch->search($card, $hand); | ||
|
||
if (count($matchingHand->getCards()) === 2) { | ||
return $matchingHand; | ||
} | ||
} | ||
|
||
return; | ||
} | ||
} |