Skip to content

Commit

Permalink
Dev And Ci
Browse files Browse the repository at this point in the history
 - Fix (drunkness)
  • Loading branch information
ranpafin committed Aug 17, 2015
1 parent 7cf880d commit 864eff9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ allow_failures:
fast_finish: true

before_script:
- /home/travis/build/facile-it/paraunit/.travis/sigsegv-extension/build-for-travis.sh
- if [[ $TRAVIS_PHP_VERSION = '5.6' ]]; then PHPUNIT_FLAGS="--coverage-clover ./build/logs/clover.xml"; else PHPUNIT_FLAGS=""; fi
- composer install -vvv


script:
- phpunit -v $PHPUNIT_FLAGS
- ./bin/phpspec run

after_script:
- if [[ $TRAVIS_PHP_VERSION = '5.6' ]]; then php bin/coveralls -v; fi
47 changes: 47 additions & 0 deletions src/Hands/OnePair.php
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
);
}
}
32 changes: 32 additions & 0 deletions src/Hands/Search/HandSearch.php
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);
}
}
45 changes: 45 additions & 0 deletions src/Hands/Search/TwoOfAKindFinder.php
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;
}
}

0 comments on commit 864eff9

Please sign in to comment.