/** * @param int $number */ public function addNumber($number) { $numberHalfScore = Math::numberToHalfScore($number); if (in_array($numberHalfScore, $this->halfScores)) { throw new RowException('Cannot add two numbers from the same halfscore'); } if (in_array($number, $this->cells)) { throw new RowException('Cannot add the same value twice'); } if ($number <= 0 || $number > 90) { throw new RowException('Out of range value'); } $added = false; foreach ($this->cells as $index => $cellTofill) { if (is_null($cellTofill)) { $this->cells[$index] = $number; $this->halfScores[] = $numberHalfScore; $added = true; break; } } if (!$added) { throw new RowException('Row is full'); } }
public function testFindTriples() { $array = [1, 1, 1, 2, 2, 3, 3, 2]; $triples = Math::findTriples($array); $this->assertContains(2, $triples); $this->assertCount(2, $triples); }
protected function checkNumbersHistory() { if (count($this->numbersHistory) !== 15) { return; } $halfScores = []; foreach ($this->numbersHistory as $number) { $halfScores[] = Math::numberToHalfScore($number); } $triples = Math::findTriples($halfScores); if (count($triples) > 0) { throw new FolderException('Folder can\'t contain same halfscore more than twice'); } }
/** * @param Folder $folder * @return array */ protected static function fillEmptySpaces(Folder $folder) { $formattedFolder = []; $rows = $folder->getRows(); $filledRows = []; foreach ($rows as $row) { $filledRow = array_fill(0, 9, null); $cells = $row->getCells(); foreach ($cells as $number) { $filledRow[Math::numberToHalfScore($number)] = $number; } $filledRows[] = $filledRow; } return $filledRows; }
protected function getUnaivalableHalfScoreIndexes() { $usedInRow = []; foreach ($this->extractionHistory->getRowHistory() as $number) { $usedInRow[] = Math::numberToHalfScore($number); } $usedInFolder = []; foreach ($this->extractionHistory->getFolderHistory() as $number) { $usedInFolder[] = Math::numberToHalfScore($number); } $usedTwiceInFolder = Math::findDoubles($usedInFolder); $consumed = []; foreach ($this->halfScores as $index => $halfScore) { if (count($halfScore) === 0) { $consumed[] = $index; } } return array_merge($consumed, $usedInRow, $usedTwiceInFolder); }