Commit 9dc7be09 authored by Sylvain Hatier's avatar Sylvain Hatier Committed by Arnaud Bey
Browse files

WIP Maximize wors number for an orphan square

parent 42b61795
......@@ -29,17 +29,25 @@ class LetterLanguageManager
// où chaque bigramme apparait autant de fois que son poids
$bigrams = [];
$lines = file($fileBigrams);
$lettersFromBigrams=[];
array_shift($lines);
foreach ($lines as $line) {
$tab = explode("\t", $line);
$string = $tab[0];
$poids = $tab[3];
$bigramLetters = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);
foreach ($bigramLetters as $bigramLetter) {
$lettersFromBigrams[$bigramLetter] = 1;
}
for ($j = 0; $j < $poids; $j++) {
$bigrams[] = $string;
}
}
// On initialise une grille vide.
$countsBigram = array_count_values($bigrams);
// var_dump($countsBigram);
// die();
// // On initialise une grille vide.
$grid = [];
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
......@@ -48,6 +56,7 @@ class LetterLanguageManager
}
$emptySquares = $this->getEmptySquares($grid, $side);
//tant qu'il reste des cases vides
while (!empty($emptySquares)) {
$bigram = $bigrams[array_rand($bigrams)];
$emptySquare = $emptySquares[array_rand($emptySquares)];
......@@ -58,10 +67,11 @@ class LetterLanguageManager
$firstLetter = $chars[0];
$secondLetter = $chars[1];
$flagBigram = false;
// on regarde si une lettre du bigram pioché est déjà présente et qu'elle a des adjacent vide.
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
if ($grid[$i][$j] == $firstLetter) {
$ajdacentsLetterBigram = $this->getEmptyAdjacentSquares($grid, $side, [$i, $j]);
$ajdacentsLetterBigram = $this->getAdjacentSquares($grid, $side, [$i, $j], true);
if (!empty($ajdacentsLetterBigram)) {
$ajdacentLetterBigram = $ajdacentsLetterBigram[array_rand($ajdacentsLetterBigram)];
$grid[$ajdacentLetterBigram[0]][$ajdacentLetterBigram[1]] = $secondLetter;
......@@ -69,7 +79,7 @@ class LetterLanguageManager
break 2;
}
} elseif ($grid[$i][$j] == $secondLetter) {
$ajdacentsLetterBigram = $this->getEmptyAdjacentSquares($grid, $side, [$i, $j]);
$ajdacentsLetterBigram = $this->getAdjacentSquares($grid, $side, [$i, $j], true);
if (!empty($ajdacentsLetterBigram)) {
$ajdacentLetterBigram = $ajdacentsLetterBigram[array_rand($ajdacentsLetterBigram)];
$grid[$ajdacentLetterBigram[0]][$ajdacentLetterBigram[1]] = $firstLetter;
......@@ -80,11 +90,32 @@ class LetterLanguageManager
}
}
if (!$flagBigram) {
$ajdacentEmptySquares = $this->getEmptyAdjacentSquares($grid, $side, $emptySquare);
$randomAdjacent = (!empty($ajdacentEmptySquares)) ? $ajdacentEmptySquares[array_rand($ajdacentEmptySquares)] : null;
$grid[$emptySquareI][$emptySquareJ] = $firstLetter;
$grid[$randomAdjacent[0]][$randomAdjacent[1]] = ($randomAdjacent !== null) ? $secondLetter : null;
$ajdacentEmptySquares = $this->getAdjacentSquares($grid, $side, $emptySquare, true);
if (empty($ajdacentEmptySquares)) {
$ajdacentSquares = $this->getAdjacentSquares($grid, $side, $emptySquare, false);
$letterToScore=[];
foreach ($lettersFromBigrams as $key => $value) {
$score=0;
foreach ($ajdacentSquares as $ajdacentSquare) {
$letter = $grid[$ajdacentSquare[0]][$ajdacentSquare[1]];
if (isset($countsBigram[$letter.$key])) {
$score += $countsBigram[$letter.$key];
} elseif (isset($countsBigram[$key.$letter])) {
$score += $countsBigram[$key.$letter];
}
}
$letterToScore[$key] = $score;
}
arsort($letterToScore);
reset($letterToScore);
$bestLetter = key($letterToScore);
$grid[$emptySquareI][$emptySquareJ] = $bestLetter;
} else {
$randomAdjacent = $ajdacentEmptySquares[array_rand($ajdacentEmptySquares)];
$grid[$emptySquareI][$emptySquareJ] = $firstLetter;
$grid[$randomAdjacent[0]][$randomAdjacent[1]] = $secondLetter;
}
}
$emptySquares = $this->getEmptySquares($grid, $side);
}
......@@ -95,11 +126,10 @@ class LetterLanguageManager
$letters[] = $grid[$i][$j];
}
}
return $letters;
}
private function getEmptyAdjacentSquares($grid, $side, $square)
private function getAdjacentSquares($grid, $side, $square, $returnEmpty = true)
{
$emptyAdjacentSquares = [];
$i = $square[0];
......@@ -118,38 +148,54 @@ class LetterLanguageManager
if ($lookTop) {
if ($lookLeft && $grid[$top][$left] === null) {
$emptyAdjacentSquares[] = [$top, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$top, $left];
}
if ($grid[$top][$j] === null) {
$emptyAdjacentSquares[] = [$top, $j];
}
$adjacentSquares[] = [$top, $j];
if ($lookRight && $grid[$top][$right] === null) {
$emptyAdjacentSquares[] = [$top, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$top, $right];
}
}
if ($lookRight && $grid[$i][$right] === null) {
$emptyAdjacentSquares[] = [$i, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$i, $right];
}
if ($lookBottom) {
if ($lookRight && $grid[$bottom][$right] === null) {
$emptyAdjacentSquares[] = [$bottom, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$bottom, $right];
}
$adjacentSquares[] = [$bottom, $j];
if ($grid[$bottom][$j] === null) {
$emptyAdjacentSquares[] = [$bottom, $j];
}
if ($lookLeft && $grid[$bottom][$left] === null) {
$emptyAdjacentSquares[] = [$bottom, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$bottom, $left];
}
}
if ($lookLeft && $grid[$i][$left] === null) {
$emptyAdjacentSquares[] = [$i, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$i, $left];
}
return $emptyAdjacentSquares;
return ($returnEmpty) ? $emptyAdjacentSquares : $adjacentSquares;
}
private function getEmptySquares($grid, $side)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment