Ruby 196
This is an anonymous function that takes width and length as arguments and writes to stdout.
->m,n{r=rand(9)
t=""
128.times{|h|t<<"%02X"%((h+r)*17%256)}
(m*n*4-m).times{|k|i=k%m;j=k/m
s="\\ #{t[j/4]} / | #{t[i]} X #{t[i+1]} | / #{t[j/4+1]} \\ | ------+-"[j%4*8,8]
i==m-1&&s[5,3]=$/
$><<s}}
Explanation
Uniqueness of cells is guaranteed by having the contents of each cell correspond to its x,y coordinates, as below. As a result the pattern is always symmetrical about the NW-SE diagonal:
00,00 01,00 02,00 03,00 04,00 05,00 06,00 07,00 08,00 09,00 10,00 11,00...
00,01 01,01 02,01 03,01 04,01 05,01 06,01 07,01 08,01 09,01 10,00 11,01...
00,00 01,02 02,02 03,02 04,02 05,02 06,02 07,02 08,02 09,02 10,02 11,02...
.
etc
The x values appear on the left and right edges of the tile, and the y values on the top and bottom edges.
The astute will have noticed that we cannot simply write a 2-digit number across each tile, because the edges will not match. What is needed is a sequence of (at least) 255 digits where all combinations are unique. From this we can pick a pair of digits (e.g. 1 and 2) for the first tile , then follow naturally on to the next, where the last number of one tile becomes the first of the next (e.g. 2 and 3).
I use a sequence containing just the hexadecimal digits 0-9A-F because the hexadecimal number representation is more golfable than some other arbitrary base. Therefore I need a 256 digit sequence where every one of the possible 2-digit combinations appears exactly once. Such a sequence is known as a De Bruijn sequence.
I have discovered a very golfy way of generating De Bruijn sequences of subsequence length 2 with digits from even base numbers. We simply take all the numbers from 0
to b*b/2-1
, multiply them by b+1
,take the last 2 digits, and concatenate the results. Here is an illustration of the sequence used with a bit more explanation as to how it works for base b=16
. Basically each line contains all combinations with two of the possible differences between digits, which together add up to 1 or 17 (mod 16). It is necessary to think in modular base 16 arithmetic, where for example -1
= +F
.
00112233445566778899AABBCCDDEEFF Difference between digits is +1 or +0
102132435465768798A9BACBDCEDFE0F Difference between digits is +2 or +F
2031425364758697A8B9CADBECFD0E1F Difference between digits is +3 or +E
30415263748596A7B8C9DAEBFC0D1E2F . . . . .
405162738495A6B7C8D9EAFB0C1D2E3F . . . . .
5061728394A5B6C7D8E9FA0B1C2D3E4F . . . . .
60718293A4B5C6D7E8F90A1B2C3D4E5F Difference between digits is +7 or +A
708192A3B4C5D6E7F8091A2B3C4D5E6F Difference between digits is +8 or +9
Finally, to comply with the random requirement, the pattern can be shifted diagonally by a random number 0..8
so there are 9 possible boards that can be generated.
ungolfed in test program
f=->m,n{ #width,height
r=rand(9) #pick a random number
t="" #setup empty string and build a DeBruijn sequence
128.times{|h|t<<"%02X"%((h+r)*17%256)} #with all 2-digit combinations of 0-9A-F
(m*n*4-m).times{|k| #loop enough for rows*columns, 4 rows per tile, omitting last row.
i=k%m;j=k/m #i and j are the x and y coordinates
#lookup the 4 values for the current tile and select the appropriate 8-character segment
# from the 32-character string produced, according to the row of the tile we are on.
s="\\ #{t[j/4]} / | #{t[i]} X #{t[i+1]} | / #{t[j/4+1]} \\ | ------+-"[j%4*8,8]
i==m-1&&s[5,3]=$/ #if i is on the rightmost row, delete the last 3 charaters of s and replace with \n
$><<s}} #print the current horizontal segment of the current tile.
f[20,20]
Output for 20,20 where r=8
\ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \ | / 8 \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 / | \ 8 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \ | / 9 \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 / | \ 9 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \ | / A \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A / | \ A /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \ | / B \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B / | \ B /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \ | / C \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C / | \ C /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \ | / D \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D / | \ D /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \ | / E \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E / | \ E /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \ | / F \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F / | \ F /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \ | / 0 \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 / | \ 0 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \ | / 2 \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 / | \ 2 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \ | / 1 \
------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+------
\ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 / | \ 1 /
8 X 8 | 8 X 9 | 9 X 9 | 9 X A | A X A | A X B | B X B | B X C | C X C | C X D | D X D | D X E | E X E | E X F | F X F | F X 1 | 1 X 0 | 0 X 2 | 2 X 1 | 1 X 3
/ 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \ | / 3 \
Can I output a board where all the
#
s areA
? – Leaky Nun – 2016-05-18T13:43:02.883@Kenny: Each individual tile in the output must be unique. – Lynn – 2016-05-18T13:46:36.567
1.Are rotations and reflections of tiles considered unique? 2.Are rotations and reflections of boards considered unique (eg is it sufficient to generate one board deterministically and turn it upside down at random?) 3. Are relabellings of boards unique (if I change ALL the A's to Z's, is that the same board or a different one?) – Level River St – 2016-05-18T14:16:14.373
@LevelRiverSt asked about tiles, too. Are
"\\ A /\nB X C\n/ D \\"
and"\\ B /\nD X A\n/ C \\"
the same tile? – Lynn – 2016-05-18T15:52:45.877Grid is still guaranteed to be rectangular, though. So it can be reflected horizontally, vertically, or (most similarly) rotated 180 degrees. Are they considered the same? – Level River St – 2016-05-18T16:03:39.180
1@Lynn Tile rotations/reflections aren't a problem, since you're not allowed to rotate or flip the tiles when you're actually playing the game -- I'll clarify that. – AdmBorkBork – 2016-05-18T16:41:27.990
@LevelRiverSt I see what you're saying. Reconsidering, I'll allow the full reflection/rotation spectrum. – AdmBorkBork – 2016-05-18T16:42:08.630
r.tr!'ab','ba'if rand>.5
is perfectly legit you say? – John Dvorak – 2016-05-19T07:39:08.883@JanDvorak If you can still guarantee a solved board and that every tile is unique, go for it. – AdmBorkBork – 2016-05-19T12:19:30.810
Happy coincidence: My own question Output diagonal positions of me squared has the same pattern as example!
– sergiol – 2017-11-03T00:01:00.877