Created
November 6, 2012 21:19
-
-
Save benjamw/4027637 to your computer and use it in GitHub Desktop.
Get index of FEN string for given blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** function get_index | |
* | |
* Gets the FEN string index for a 2D location | |
* of a square array of blocks each containing | |
* a square array of elements within those blocks | |
* | |
* This was designed for use within foreach structures | |
* The first foreach ($i) is the outer blocks, and the | |
* second ($j) is for the inner elements. | |
* | |
* Example Structure: | |
* +----+----+----++----+----+----+ | |
* | 0 | 1 | 2 || 3 | 4 | 5 | | |
* +----+----+----++----+----+----+ | |
* | 6 | 7 | 8 || 9 | 10 | 11 | | |
* +----+----+----++----+----+----+ | |
* | 12 | 13 | 14 || 15 | 16 | 17 | | |
* +====+====+====++====+====+====+ | |
* | 18 | 19 | 20 || 21 | 22 | 23 | | |
* +----+----+----++----+----+----+ | |
* | 24 | 25 | 26 || 27 | 28 | 29 | | |
* +----+----+----++----+----+----+ | |
* | 30 | 31 | 32 || 33 | 34 | 35 | | |
* +----+----+----++----+----+----+ | |
* | |
* Where $i = 2 (bottom left big block) | |
* and $j = 5 (center element) | |
* $blocks = 2 (number of blocks per side) | |
* $elems = 3 (numer of elements per side in each block) | |
* will return 25 (the index of the string) | |
* | |
* @param int the current block number | |
* @param int the current element number | |
* @param int the number of blocks per side | |
* @param int the number of elements per side | |
* @return int the FEN string index | |
*/ | |
function get_index($i, $j, $blocks = 3, $elems = 3) { | |
$bits = array( | |
($j % $elems), // across within block (across elems) | |
((int) floor($j / $elems) * $blocks * $elems), // down within block (down elems) | |
(($i % $blocks) * $elems), // across blocks | |
((int) floor($i / $blocks) * $blocks * $elems * $elems), // down blocks | |
); | |
return array_sum($bits); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment