11
\$\begingroup\$

TetraVex is a tile-based edge-matching game that was originally coded by Scott Ferguson for the Microsoft Entertainment Pack 3 back in 1990. The game consists of a grid upon which square tiles are placed. The tiles have numbers or letters on each edge, and the goal is to place all tiles in the grid, with each pair of edges matching, in the fastest time. Since solving TetraVex is known to be NP-Complete, we're instead just going to generate a solved board.

For our purposes, each TetraVex tile is 5 characters wide by 3 tall, and follows the pattern:

\ # /
# X #
/ # \

where each # is replaced with an alphanumeric symbol matching [A-Z0-9] (capitals, not lowercase).

When placed on the board, the tiles have additional spaces between them on the grid, in order to differentiate the tiles horizontally from each other. The grid is composed of |, -, and + symbols, as shown below. Note that there is no border on the outside of the gameplay area, and no trailing spaces to the right of the tiles.

\ # / | \ # /
# X # | # X #
/ # \ | / # \
------+------
\ # / | \ # /
# X # | # X #
/ # \ | / # \

Input

  • Two integers 1 < n,m <= 255 in any convenient format (tuple, array, function argument, etc.). The numbers can be taken in either order (please specify in your answer which order).
  • These represent a board of n tiles wide by m tiles tall.
    • The smallest board output will thus be 2x2 tiles, or 13 characters wide by 7 characters tall.
    • The largest is 255x255 tiles, or 2037 characters wide by 1019 characters tall.

Output

  • An ASCII representation of a TetraVex board of size n x m.
  • The board must follow the tile construction rules above, and must be a solved board (meaning the paired edges match). Non-paired edges (i.e., the border) can be any legal character.

Rules

  • Each individual tile in the output must be unique.
    • Reflected or rotated tiles count as distinct, since during actual gameplay you're not permitted to rotate or flip the tiles in any manner.
    • For example, a tile with 1 A on top and 3 Bs on the other edges is considered distinct from a tile with 1 A on the right and 3 Bs on the other edges.
  • The output must be somewhat non-deterministic, meaning different runs of the code with the same n and m input should yield different boards. However, in the interests of complexity, not all possible n x m boards need have non-zero probability in your output -- only at least two of the possible boards must have non-zero probability of actually being produced.
    • For example, a 3 x 3 board, with full alphanumeric edges, already has somewhere around 36^24 ~= 2.25e37 possible boards. Since that number grows exponentially as the size increases, ensuring equal probability for all boards may become infeasible for many languages.
    • This does mean you could have two separate deterministic code paths produce two separate boards and then simply flip a coin to determine which to output -- that's fine. (For example, starting with the first tile all A in one path and all B in the other, and then generate the rest of the board using the same algorithm from there will guarantee two unique boards and is allowed)
    • Any of our usual definitions for randomness will suffice for determining this non-zero probability.
    • Rotations or reflections of the board are considered "different" boards, but note that you're not guaranteed a square grid, only a rectangular one. ;-)
    • Generating a board and then replacing a certain character with another (e.g., replacing all A with Z) is allowed, provided that each tile in the output is still unique.
  • Trailing spaces are not allowed but trailing newline(s) are OK.
  • Standard loopholes are forbidden.
  • This is , so all usual golfing rules apply, and the shortest code wins.

Examples

1) Input: 3 3

\ A / | \ B / | \ C /
D X E | E X G | G X I
/ J \ | / K \ | / L \
------+-------+------
\ J / | \ K / | \ L /
M X N | N X O | O X P
/ Q \ | / R \ | / S \
------+-------+------
\ Q / | \ R / | \ S /
T X U | U X V | V X W
/ X \ | / Y \ | / Z \

2) Input: 5 3

\ 0 / | \ B / | \ C / | \ A / | \ Z /
D X E | E X F | F X I | I X A | A X Q
/ J \ | / 9 \ | / L \ | / L \ | / 6 \
------+-------+-------+-------+------
\ J / | \ 9 / | \ L / | \ L / | \ 6 /
M X N | N X 4 | 4 X P | P X P | P X T
/ R \ | / R \ | / S \ | / B \ | / K \
------+-------+-------+-------+------
\ R / | \ R / | \ S / | \ B / | \ K /
T X Z | Z X V | V X W | W X J | J X G
/ X \ | / Y \ | / Z \ | / X \ | / A \
\$\endgroup\$
10
  • \$\begingroup\$ Can I output a board where all the #s are A? \$\endgroup\$
    – Leaky Nun
    May 18, 2016 at 13:43
  • \$\begingroup\$ @Kenny: Each individual tile in the output must be unique. \$\endgroup\$
    – Lynn
    May 18, 2016 at 13:46
  • \$\begingroup\$ 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?) \$\endgroup\$ May 18, 2016 at 14:16
  • \$\begingroup\$ @LevelRiverSt asked about tiles, too. Are "\\ A /\nB X C\n/ D \\" and "\\ B /\nD X A\n/ C \\" the same tile? \$\endgroup\$
    – Lynn
    May 18, 2016 at 15:52
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ May 18, 2016 at 16:41

4 Answers 4

3
\$\begingroup\$

CJam, 116

26mr:D;q~:B;){[7B),f#10222@#f*2G#)f%_2ew:+23f%\W<26f%]{D+26%'A+}f%~"\/"f*_:$@2/'Xf*[@]'|f*Sf*~_'{f>"-+"f=\@}%2>-3<N*

Try it online

Uses only letters.

Explanation:

The first observation is that any rectangular piece of a solved board is a solved board. Since the program needs to work up to 255×255, it is enough to solve it for that case and use a piece of that board with the requested dimensions.

I am using a fixed 255×255 board that I determined to be correct. I actually did 256×256 tiles first, choosing 2 numbers per tile (top and left), then copied the other numbers from adjacent tiles, discarding some edge numbers from the last row and last column.

Each tile is first given an associated number, and then I calculate the remainders of that number modulo two other numbers (I used 23 and 26) to get the 2 numbers chosen for that tile.

In order to avoid patterns which lead to duplication, I decided to make the associated numbers pseudo-random and unique. I used my favorite distinct not-very-random number generator, which is based on primitive roots: take a prime number slightly bigger than the desired range (65537) and a small prime number (7), then use the powers of the small number modulo the big number. After a few trials, I found that the combination of 7, 23 and 26 works well, giving 65025 (255²) unique tiles.

For the random factor, I am just applying a random cyclic permutation to the value of each number. Then the numbers (0-25) are converted to letters (A-Z).

Code explanation to follow.

\$\endgroup\$
3
\$\begingroup\$

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 \
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice explanation, and that would certainly make for a confusing actual game with so many tiles that are almost-duplicates. \$\endgroup\$ May 20, 2016 at 12:28
2
+200
\$\begingroup\$

APL (Dyalog Unicode), 112 bytes

⎕CY'dfns'
{⍉¯2↓2↓⍉¯1↓1↓(1,⍨3 7×⍳¨⍵)box⊃⍪/,/⍵↑∘.{(7⍴0 1)\3 3⍴⍺@2 8⊢⍵@4 6,⍨'\//\X'}⍨2,/⎕A[(?5)+16|+\17|⊢∘-\32/⍳8]}

Try it online!

An inline function that takes a single argument, a length-2 vector n m.

The essential idea is taken from Level River St's Ruby solution, i.e. build a De Bruijn sequence of order 2 on a size-16 alphabet.

The rest of the algorithm is just extracting alphabets (with random offset to the alphabet index), arranging into boxes, taking the top left corner of size of the input, and formatting to match the output format.

A bit of work is offloaded to the dfns.box library function, which inserts horizontal and vertical dividers at given indexes using either box-drawing Unicode chars or ASCII.

How it works

⍝ Load library to use the function `box`
⎕CY'dfns'

f←{
  ⍝ Generate the De Bruijn sequence using the observation by Level River St
  idxs←16|+\17|⊢∘-\32/⍳8
                   32/⍳8  ⍝ 1..8, each number copied 32 times
               ⊢∘-\       ⍝ Negate the even indexes (1-based)
            17|           ⍝ Modulo 17; gives 1 16 1 16 ... 2 15 2 15 ... 8 9 8 9
       16|+\              ⍝ Cumul.sum mod 16

  ⍝ Generate tile fragments in one dimension
  axis←2,/⎕A[idxs+?5]
             idxs+?5   ⍝ Add one of 1..5 (random) to all of idxs
          ⎕A[       ]  ⍝ Index into the vector of uppercase letters
       2,/             ⍝ Join adjacent pairs

  ⍝ Generate the matrix of tiles
  mat←⍵↑∘.{(7⍴0 1)\3 3⍴⍺@2 8⊢⍵@4 6,⍨'\//\X'}⍨axis
        ∘.{                                }⍨axis  ⍝ Self outer product over axis
                                  ,⍨'\//\X'  ⍝ '\//\X\//\X'
                   3 3⍴             ⍝ Reshape to 3×3 so the ^ chars form X-frame
                       ⍺@2 8⊢⍵@4 6  ⍝ Place chars of ⍺ vertically, ⍵ horizontally
           (7⍴0 1)\                 ⍝ Insert blanks between columns and outside
      ⍵↑  ⍝ Take n rows and m columns of the entire matrix

  ⍝ Output formatting
  ⍉¯2↓2↓⍉¯1↓1↓(1,⍨3 7×⍳¨⍵)box⊃⍪/,/mat
                             ⊃⍪/,/mat  ⍝ Flatten chars to single matrix
              (          )box  ⍝ Apply box drawing with the options of
                  3 7×⍳¨⍵      ⍝   inserting lines after every 3rd row and 7th col
               1,⍨             ⍝   using ASCII
  ⍉¯2↓2↓⍉¯1↓1↓  ⍝ Fix formatting: drop 1 rows and 2 columns from the outside
}
\$\endgroup\$
1
\$\begingroup\$

PowerShell 6, 244 254 bytes

De Bruijn sequence is a good idea. Thanks to Level River St

param($w,$h)for(;$z='A'..'Z'|?{"Z$x"-notmatch-join"$x$_"[-2..-1]}){$x+=''+$z[0]}$v=random 9
1..$h|%{$y=$_+$v
,(($r=1..$w)|%{"\ $($x[$y+$_-1]) /"})+,($r|%{$x[$_-1]+' X '+$x[$_]})+,($r|%{"/ $($x[$y+$_]) \"})|%{$_-join' | '}
,'-----'*$w*($_-ne$h)-join'-+-'}

Try it online!

  1. I've adapted my answer from the topic Generate the shortest De Bruijn for the case {String:'A'..'Z', n:2}.
  2. The script loop by this sequence twice: each tail take two chars by row and by column
  3. The shortest De Bruijn sequence contains 676 chars. That's enough for 255 rows and for 255 columns
  4. The script add random 9 to row numbers to comply with the random requirement
  5. The last line fixed. Output contains extra trailing newline

No-duplicate test generates #### strings instead ASCII tails and is looking for duplicates.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.