Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $arr = [];
- $lines = 3;
- $els = 5;
- $combos = 15;
- for($i=0; $i < $lines; $i++){
- $arr[$i] = [];
- for ($j=0; $j < $els; $j++) {
- $arr[$i][$j] = rand(0,$combos-1);
- }
- }
- /*echo "<pre>";*/
- //draw($arr, []);
- //echo "<br><br>";
- $matches = [];
- $matches[0] = [];
- for ($i=0; $i < $lines; $i++) {
- for ($j=0; $j < $combos; $j++) {
- if ($arr[$i][0] == $j) {
- $arr2 = FindMatching($arr, $j, 0, $i, $matches);
- }
- }
- }
- $matches = RemoveUnwanted($matches);
- //print_r($matches);
- /*echo "<br><br>";
- draw($arr, $matches);
- echo "</pre>";
- */
- function FindMatching(&$arr, $match, $x, $y, &$matches){
- $e = count($matches)-1;
- array_push($matches[$e], $y.':'.$x);
- if ($y-1 > -1 && $x+1 < count($arr[0]) && $arr[$y-1][$x+1] == $match) {
- FindMatching($arr, $match, $x+1, $y-1, $matches);
- }
- if ($x+1 < count($arr[0]) && $arr[$y][$x+1] == $match) {
- FindMatching($arr, $match, $x+1, $y, $matches);
- }
- if ($y+1 < count($arr) && $x+1 < count($arr[0]) && $arr[$y+1][$x+1] == $match) {
- FindMatching($arr, $match, $x+1, $y+1, $matches);
- }
- if ($x != 0) {
- $matches[count($matches)] = array_slice($matches[count($matches)-1],0,$x);
- } else {
- $matches[count($matches)] = [];
- }
- }
- function RemoveUnwanted($matches){
- $matches = MultiUnique($matches);
- $len = count($matches);
- $newmatches = [];
- foreach ($matches as $key => $value) {
- if($value != [] && count($value) >= 3 && !IsSubset($value,$matches)) {
- $newmatches[count($newmatches)] = $value;
- }
- }
- return $newmatches;
- }
- function MultiUnique($src){
- $output = array_map("unserialize",
- array_unique(array_map("serialize", $src)));
- return $output;
- }
- function IsSubset($arr, $matches) {
- foreach ($matches as $key => $match) {
- if(count($arr) < count($match) && strpos(implode("",$match), implode("",$arr)) !== false) {
- return true;
- }
- }
- return false;
- }
- function draw($arr, $match){
- foreach ($match as $key => $match2) {
- foreach ($match2 as $key2 => $value) {
- $index = explode(':',$value);
- $arr[$index[0]][$index[1]] = "<b>".$arr[$index[0]][$index[1]]."</b>";
- }
- }
- for ($i=0; $i < count($arr); $i++) {
- for ($j=0; $j < count($arr[0]); $j++) {
- echo $arr[$i][$j]." ";
- }
- echo "<br>";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement