Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to make some assumptions here: The board is stored as a 64 bit number, each 8 byte block represents a row. Each bit in the row represents a column (a..h). You have start and end position as zero-based coordinates. i.e: <em><code>start = "C4" = [2,3]; end = "F4" = [5,3]</code></em></p> <p>For horizontal moves with increasing columns, you can calculate the distance moved: <em><code>d = (F4-C4 = 3)</code></em>. Subtract 1 to exclude the destination, then the "trail" t of d-1 bits is <em><code>t = (1&lt;&lt;(d-1))-1</code></em>. Shift the trail adjacent to the source piece to get the mask M: <em><code>M = t&lt;&lt;(start.row*8 + start.column+1)</code>.</em></p> <p>This is equivalent to <code>M = ((1&lt;&lt;d)-2)&lt;&lt;(start.row*8 + start.column)</code></p> <p>For horizontal moves the other way: </p> <pre><code> d = (C4-F4 = -3) t = (1&lt;&lt;(-d-1))-1 M = (t&lt;&lt;dest.column+1) //-or- M = ((1&lt;&lt;-d)-2)&lt;&lt;(dest.row*8 + dest.column) </code></pre> <p>For vertically increasing moves: </p> <pre><code> d = (C7-C4 = 3) t=(1&lt;&lt;8) (d-1) times: { t |= (t&lt;&lt;8)} M = t &lt;&lt; (start.row*8 + start.column) </code></pre> <p>For vertically decreasing moves: </p> <pre><code> d = (C4-C7 = 3) t=(1&lt;&lt;8) (d-1) times: { t |= (t&lt;&lt;8)} M = t &lt;&lt; (dest.row*8 + start.column) </code></pre> <p>For the vertical moves, you can replace the loop over d by storing the maximum "vertical trail" <code>VT = 0x0101010101010101 = 72340172838076673</code>. Then mask out the right number of bits for the actual move. </p> <p>This reduces the caclulation to <code>M = (VT &amp; ((1&lt;&lt;(d*8)) - 2)) &lt;&lt; (row*8+column)</code>.</p> <p>You could probably do something similar for the diagonal moves. Start with the max diagonal trail DT = <code>0x0102040810204080</code>, apply a mask to reduce it to <code>d</code> set bits, and shift to the start or end location, depending on which is closer to the edge. This would need careful testing to make sure there were no edge cases which wrapped around into the wrong row.</p> <p><em>Edited to exclude both source and destination, and fix one-off errors</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload