Skip to content

Instantly share code, notes, and snippets.

@Soben
Created November 11, 2017 23:36
Show Gist options
  • Save Soben/779156a0102d22e11e37f7bbc12aeec5 to your computer and use it in GitHub Desktop.
Save Soben/779156a0102d22e11e37f7bbc12aeec5 to your computer and use it in GitHub Desktop.
Finds combinations of real words for a WordLock (requires pspell)
<?php
class WordGenerator {
private $attemptedWords = [];
private $foundWords = [];
private $totalPermutations = 0;
private $comboLock = [];
private $dictionary = null;
private $totalAttempts = 0;
public function __construct(array $comboLock) {
$this->comboLock = $comboLock;
$this->dictionary = pspell_new("en");
$permutations = 1;
foreach ($comboLock as $comboLine) {
$permutations = $permutations * strlen($comboLine);
}
$this->totalPermutations = $permutations;
$this->findWords();
var_dump($this->foundWords);
}
private function findWords() {
while ($this->totalAttempts < $this->totalPermutations) {
$word = $this->generateWord();
if ($this->isAWord($word)) {
$this->foundWords[] = $word;
}
$this->attemptedWords[] = $word;
$this->totalAttempts++;
}
}
private function isAWord($word) {
if (pspell_check($this->dictionary, $word)) {
return true;
}
return false;
}
private function generateWord () {
$word = '';
foreach ($this->comboLock as $options) {
$word .= $this->getLetter($options);
}
if (in_array($word, $this->attemptedWords)) {
return generateWord();
}
return $word;
}
private function getLetter($options) {
$rand = rand(0, count($options) - 1);
return substr($options, $rand, 1);
}
}
$comboLock = array(
"pbwslatdmf",
"porilcetna",
"rilanutose",
"knrteldaos",
"nthyderls",
);
$generator = new WordGenerator($comboLock);
print "PROCESS COMPLETE" . PHP_EOL;
// $comboLock = array(
// "pbwslatdmf",
// "porilcetna",
// "rilanutose",
// "knrteldaos",
// );
// $generator = new WordGenerator($comboLock);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment