PHP generates random colors, and neighboring elements have different colors

Source: Internet
Author: User
PHP random PHP random

Title, is this layout form
1 2 3
4 5 6
7 8 9
The color of the neighboring elements is different, there is, for example, 5, and 1 3 7 9 color also to be different, how this implementation, generate random colors can be, but how to achieve the color difference. The alternative color is about 6 kinds. Thank you


Reply to discussion (solution)

Keep the rules clear.

Are the so-called neighboring elements different colors 4 and 5 are adjacent? If so, only 6 colors are not possible. Re-describe your needs ... If not, then you define the concept of "neighboring elements" here.

Keep the rules clear.

Are the so-called neighboring elements different colors 4 and 5 are adjacent? If so, only 6 colors are not possible. Re-describe your needs ... If not, then you define the concept of "neighboring elements" here.
Well, 4,5 is adjacent. 6 kinds of things that cannot be achieved can be increased.

1 2 3
4 5 1
2 3 6

Does this meet the requirements?


Keep the rules clear.

Are the so-called neighboring elements different colors 4 and 5 are adjacent? If so, only 6 colors are not possible. Re-describe your needs ... If not, then you define the concept of "neighboring elements" here.
Well, 4,5 is adjacent. 6 kinds of things that cannot be achieved can be increased.

Limiting the use of a limited number of colors and not qualifying the color of the implementation should be (can) not the same as the former is a finite species can be calculated that the latter can be directly randomly taken and then judged if not again random

1 2 1
3 4 3
1 2 1

Four-color map?
Algorithm I'm weak.

My idea is to go from left to right, from top to bottom, to fill in the numbers (1~n), as long as the front adjacent (diagonal) the lattice has been skipped, did not appear to select the number
For example, the upper right corner, because not yet to the second row, adjacent to the left only a lattice 2, so 1~n, 1 directly meet, fill in the upper right corner

1 2 3
4 5 1
2 3 6

Does this meet the requirements?
This fits, but this style is loaded through the waterfall stream. Height is not fixed, so very tangled. It's possible that the Middle 2 5 3 are close to 1!

1 2 3
4 5 1
2 3 6

Does this meet the requirements?
The page uses masonry and infinitescroll, each row three blocks, the width is fixed, the height is uncertain. Then to achieve the adjacent background color is different, do not know how to implement it?

If the # # also meets the requirements, then you can say: the singular line is 1 2 1, the even-numbered line is 3 4 3 ... Just the loop.

$d = Array (1,2,3,4,5,6), $a = Array ($d [rand (0, 5)]); $loop = 300;  Used to prevent a dead loop while (count ($a) < && $loop-) {$p = count ($a);  $n = $d [0];  $d [] = Array_shift ($d);      Switch ($p% 3) {case 1:if ($a [$p-1] = = $n) continue;  if (isset ($a [$p-3]) && $a [$p-3] = = $n) continue;      $a [] = $n;    Break      Case 2:if ($a [$p-1] = = $n) continue;      if (isset ($a [$p-2]) && $a [$p-2] = = $n) continue;      if (isset ($a [$p-3]) && $a [$p-3] = = $n) continue;      if (isset ($a [$p-6]) && $a [$p-6] = = $n) continue;      if (isset ($a [$p-8]) && $a [$p-8] = = $n) continue;      $a [] = $n;    Break      Case 0:if (isset ($a [$p-3]) && $a [$p-3] = = $n) continue;      if (isset ($a [$p-4]) && $a [$p-4] = = $n) continue;      if (isset ($a [$p-6]) && $a [$p-6] = = $n) continue;      $a [] = $n;  Break }//$a [] = $n; If you move from case to here, it will result in incorrect results! BUG} $t = Array_map (function ($r) {return join (', ', $r);}, Array_chunk ($a, 3));p Rint_r ($t);
Array
(
[0] =
[1] = 4,5,6
[2] = 2,3,4
[3] = 5,6,1
[4] = 3,4,5
[5] = 6,1,2
[6] = 4,5,6
[7] =
[8] = 5,6,1
[9] = 2,3,4
[Ten] = 6,1,2
[One] = 3,4,5
[+] =
[+] = 4,5,6
[+] = 2,3,4
[[] = 5,6,1
[+] = 3,4,5
[+] = 6,1,2
[+] = 4,5,6
[+] =
[+] = 5,6,1
[+] = 2,3,4
[] = 6,1,2
[All] = 3,4,5
[+] =
[+] = 4,5,6
[+] = 2,3,4
[+] = 5,6,1
[+] = 3,4,5
[+] = 6,1,2
[+] = 4,5,6
[+] =
[+] = 5,6,1
[+] = 2,3,4
[+] = 6,1,2
[+] = 3,4,5
[Approx.]
[PNS] = 4,5,6
[+] = 2,3,4
[All] = 5,6,1
[+] = 3,4,5
[+] = 6,1,2
[All] = 4,5,6
[+] =
[[] = 5,6,1
[2,3,4] =
[+] = 6,1,2
[+] = 3,4,5
[+] =
[+] = 4,5,6
[[] = 2,3,4
[Wuyi] = 5,6,1
[3,4,5] = =
[+] = 6,1,2
[Wu] = 4,5,6
[+] =
[+] = 5,6,1
[[+] = 2,3,4
[+] = 6,1,2
[3,4,5] = +
[Up] =
[+] = 4,5,6
[+] = 2,3,4
[+] = 5,6,1
[+] = 3,4,5
[All] = 6,1,2
[[+] = 4,5,6
[+] =
[5,6,1] = +
[[+] = 2,3,4
)
Any three consecutive lines will meet your needs.

Got a few days, and finally found a bug in PHP
It's still quite satisfactory.

If there is a problem with the basic grammatical structure, it's time for PHP to go to the grave!
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.