Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. <?php
  2. $arr = [];
  3. $lines = 3;
  4. $els = 5;
  5. $combos = 15;
  6. for($i=0; $i < $lines; $i++){
  7. $arr[$i] = [];
  8. for ($j=0; $j < $els; $j++) {
  9. $arr[$i][$j] = rand(0,$combos-1);
  10. }
  11. }
  12.  
  13. /*echo "<pre>";*/
  14. //draw($arr, []);
  15.  
  16. //echo "<br><br>";
  17.  
  18. $matches = [];
  19. $matches[0] = [];
  20.  
  21. for ($i=0; $i < $lines; $i++) {
  22. for ($j=0; $j < $combos; $j++) {
  23. if ($arr[$i][0] == $j) {
  24. $arr2 = FindMatching($arr, $j, 0, $i, $matches);
  25. }
  26. }
  27. }
  28.  
  29. $matches = RemoveUnwanted($matches);
  30.  
  31. //print_r($matches);
  32.  
  33.  
  34.  
  35. /*echo "<br><br>";
  36.  
  37. draw($arr, $matches);
  38. echo "</pre>";
  39. */
  40.  
  41. function FindMatching(&$arr, $match, $x, $y, &$matches){
  42. $e = count($matches)-1;
  43. array_push($matches[$e], $y.':'.$x);
  44.  
  45.  
  46. if ($y-1 > -1 && $x+1 < count($arr[0]) && $arr[$y-1][$x+1] == $match) {
  47. FindMatching($arr, $match, $x+1, $y-1, $matches);
  48. }
  49.  
  50. if ($x+1 < count($arr[0]) && $arr[$y][$x+1] == $match) {
  51. FindMatching($arr, $match, $x+1, $y, $matches);
  52. }
  53.  
  54. if ($y+1 < count($arr) && $x+1 < count($arr[0]) && $arr[$y+1][$x+1] == $match) {
  55. FindMatching($arr, $match, $x+1, $y+1, $matches);
  56. }
  57.  
  58. if ($x != 0) {
  59. $matches[count($matches)] = array_slice($matches[count($matches)-1],0,$x);
  60. } else {
  61. $matches[count($matches)] = [];
  62. }
  63. }
  64.  
  65. function RemoveUnwanted($matches){
  66. $matches = MultiUnique($matches);
  67. $len = count($matches);
  68. $newmatches = [];
  69. foreach ($matches as $key => $value) {
  70. if($value != [] && count($value) >= 3 && !IsSubset($value,$matches)) {
  71. $newmatches[count($newmatches)] = $value;
  72. }
  73. }
  74. return $newmatches;
  75. }
  76.  
  77. function MultiUnique($src){
  78. $output = array_map("unserialize",
  79. array_unique(array_map("serialize", $src)));
  80. return $output;
  81. }
  82.  
  83. function IsSubset($arr, $matches) {
  84. foreach ($matches as $key => $match) {
  85. if(count($arr) < count($match) && strpos(implode("",$match), implode("",$arr)) !== false) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91.  
  92.  
  93. function draw($arr, $match){
  94. foreach ($match as $key => $match2) {
  95. foreach ($match2 as $key2 => $value) {
  96. $index = explode(':',$value);
  97. $arr[$index[0]][$index[1]] = "<b>".$arr[$index[0]][$index[1]]."</b>";
  98. }
  99. }
  100.  
  101.  
  102. for ($i=0; $i < count($arr); $i++) {
  103. for ($j=0; $j < count($arr[0]); $j++) {
  104. echo $arr[$i][$j]." ";
  105. }
  106. echo "<br>";
  107. }
  108. }
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement