JavaScript (ES6), 303 315
W większości oparte na tej odpowiedzi
Edytuj 1
- Po komentarzu OP, ignorując obudowę i zmuszając wszystko do małych liter.
- Naprawiono argumenty, teraz tylko jeden argument łańcuchowy, który się dzieli
Uwaga: przy użyciu ciągów szablonów w kodzie znajdują się 3 nowe wiersze, które są znaczące i zawierają się w liczbie bajtów
l=>(l=l.toLowerCase().split`
`).shift(r=l[1].length*4).split` `.map(v=>[2,-2,r,-r,r+2,-r-2,r-2,2-r].map((d,k)=>o.map((t,p)=>[...v].some((c,i)=>z[i?(z[i=t+d/2]=z[i]>' '?'X':'-|\\/'[k>>1],t+=d):t=p]!=c,z=[...o])?0:o=z)),o=[...l.map(s=>' '.repeat(r/2-1)+`
${[...s].join` `}
`).join``.slice(r/2)])&&o.join``
Wyjaśnione (nieaktualne)
// Note a pattern : in golfed code (but not in the following explanation),
// simple operations that have to done before calling .map or .every
// are inserted as parameter 2,3,etc... to the same function
// as each parameter is evaluated before calling a function
//
F=( l, // word list as a space separated string
o, // character grid as a newline separated string - each row is the same length
// default parameters used as local variables
r = 4 * o.search`\n` // offset from a line to next nonblank line in resulting output
)=>(
w = ' ', // shortcut for blank
// given the input string in o, build the result as a single dimension array filled with blanks
// between characters and runs of blank chars betweem each row
// .map used as a shorter .forEach
z = w, // init z to a single blank
[...o].map( c=> // for each char of o execute the following
c > w // check if c is a letter (>' ') or a newline
? z+=w+c // if c is a letter, add ' '+c to z
: ( // if c is a newline add chars to array o
z = z.slice(2), // remove the first 2 char of z (' ' or '\n ')
z = [...z], // convert to array
o.push(...z, // push one by one the chars of z
c, // then c (a newline)
...z.fill(w), // the same number of char as z, but all blanks
z = c // a newline again, meanwhile reset z
)
)
, o=[] // o is reset to an empty array just in the map call
// reusing o just to avoid another global variable (no real need in fact)
), // end of .map call
l.split` `.map( // split l into words and exec the following for each word
v => [2,-2,r,-r,r+2,-r-2,r-2,2-r].map( // for each scan direction
(d,k) => // d is the move offset , k is the index 0 to 7
o.map( // for each char in (including fill blanks and newlines, the scan will fail for them)
(t,p) => // t is the first character but NOT USED (var t reused), p is the start position
(
z=[...o], // make a copy of o in z
t = p-d, // current position-d goes in t, it will be incremented before using
[...v].every( // check the following conditions for every char of word v
(c,i) => // c: current char of word, i: index used to differentiate the first check when i==0
(
// while scanning I already modify the result working copy in z
i ? // if i != 0
// fill the intermediate position with the right fiil character
// based on direction index in k, or X if the position is already full
z[i = t+d/2] = z[i] > w ? 'X' : '--||\\\\//'[k]
: 0, // else do nothing
// case insensitive comparison, if not a letter parseInt returns NaN that is != from any value
// this is the condition retured to the .every function
parseInt(c,36)==parseInt(o[t+=d],36) // meanwhile increment position in T
)
) ? // check the result of .every
o = z // if true, update o from the working copy
: 0 // if false do nothing
) // end of o.map operations
) // end of o.map function call
) // end of direction list map function call
) // end of l.split.map function call
, o.join``
)
TEST
console.log=(...x)=>O.textContent+=x.join` `+`\n`;
F=l=>
(l=l.toLowerCase().split`\n`)
.shift(r=l[1].length*4).split` `.map(v=>[2,-2,r,-r,r+2,-r-2,r-2,2-r].map((d,k)=>o.map((t,p)=>
[...v].some((c,i)=>z[i?(z[i=t+d/2]=z[i]>' '?'X':'-|\\/'[k>>1],t+=d):t=p]!=c
,z=[...o])?0:o=z)
),o=[...l.map(s=>' '.repeat(r/2-1)+`\n${[...s].join` `}\n`).join``.slice(r/2)])
&&o.join``
console.log(F('Atomic chess is cool\nachess\nbtoikm\nbloosi\nnowmlp\nhewiir\nasdfec'))
console.log(F("RANDOM VERTICAL HORIZONTAL WIKIPEDIA Tail WordSearch CODEGOLF UNICORN\nWVERTICALL\nROOAFFLSAB\nACRILIATOA\nNDODKONWDC\nDRKESOODDK\nOEEPZEGLIW\nMSIIHOAERA\nALRKRRIRER\nKODIDEDRCD\nHELWSLEUTH"))
<pre id=O></pre>