/** * @param HandInterface $hand * * @return HandInterface|null */ public function find(HandInterface $hand) { if (count($hand->getCards()) < $this->requiredMatches) { return null; } $cards = $hand->getCards(); foreach ($cards as $card) { $matchingHand = $this->handSearch->search($card, $hand); if ($matchingHand && count($matchingHand->getCards()) === $this->requiredMatches) { return $matchingHand; } } return null; }
/** * @param HandInterface $hand * * @return HandInterface */ public function find(HandInterface $hand) { if (count($hand->getCards()) < 5) { return null; } $cards = $hand->getCards(); foreach ($cards as $card) { $start = max($card->getFaceValue() - 4, 1); $end = min($card->getFaceValue() + 4, 14); $flushCards = []; for ($faceValueCounter = $start; $faceValueCounter <= $end; ++$faceValueCounter) { $matchingHand = $this->handSearch->search(new Card($faceValueCounter == 1 ? 14 : $faceValueCounter, Suit::diamonds()), $hand); if ($matchingHand && count($matchingHand->getCards())) { $flushCards[] = $matchingHand->getCards()[0]; } if (count($flushCards) == 5) { return new Hand(...$flushCards); } } } return null; }
/** * @dataProvider hands * * @param Card $cardToFind * @param HandInterface $handToSearch * @param $expectedHand * * @internal param $hand */ public function test_it_will_return_an_hand_that_matches_the_search_null_otherwise(Card $cardToFind, HandInterface $handToSearch, $expectedHand) { $handSearch = new HandSearch(); $actualHand = $handSearch->search($cardToFind, $handToSearch); $this->assertEquals($expectedHand, $actualHand); }