function isTension($moveString) { //detect is a major piece is under attack //Input: A string of moves in coordinate notation (e2e4) // And if to limit results to major cpatures (i.e. not pawn captures) $moves = explode(' ', $moveString); $position = array(array('r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'), array('p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'), array(0, 0, 0, 0, 0, 0, 0, 0), array(0, 0, 0, 0, 0, 0, 0, 0), array(0, 0, 0, 0, 0, 0, 0, 0), array(0, 0, 0, 0, 0, 0, 0, 0), array('P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'), array('R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R')); // indexed [number][letter] foreach ($moves as $key => $move) { $position = updatePosition($position, $move); $isCheck = FALSE; foreach ($position as $number => $column) { //echo "1\n"; foreach ($column as $letter => $square) { //echo "2\n"; if ($square !== 0) { // Now to work out if the locus of each piece hits an opposite side king if ($square === 'q') { $collided = array('+x' => FALSE, '+y' => FALSE, '-x' => FALSE, '-y' => FALSE, '+x+y' => FALSE, '+x-y' => FALSE, '-x+y' => FALSE, '-x-y' => FALSE); // straights // +x for ($x = $number + 1; $x < 8; $x++) { //echo "$x 3\n"; if ($collided['+x'] === FALSE && $position[$x][$letter] === 'Q') { $isCheck = TRUE; $collided['+x'] = TRUE; } else { if ($collided['+x'] === FALSE && $position[$x][$letter] !== 0) { $collided['+x'] = TRUE; } } } // -x for ($x = $number - 1; $x >= 0; $x--) { //echo "4\n"; if ($collided['-x'] === FALSE && $position[$x][$letter] === 'Q') { $isCheck = TRUE; $collided['-x'] = TRUE; } else { if ($collided['-x'] === FALSE && $position[$x][$letter] !== 0) { $collided['-x'] = TRUE; } } } // +y for ($x = $letter + 1; $x < 8; $x++) { //echo "5\n"; if ($collided['+y'] === FALSE && $position[$number][$x] === 'Q') { $isCheck = TRUE; $collided['+y'] = TRUE; } else { if ($collided['+y'] === FALSE && $position[$number][$x] !== 0) { $collided['+y'] = TRUE; } } } // -y for ($x = $letter - 1; $x >= 0; $x--) { //echo "6\n"; if ($collided['-y'] === FALSE && $position[$number][$x] === 'Q') { $isCheck = TRUE; $collided['-y'] = TRUE; } else { if ($collided['-y'] === FALSE && $position[$number][$x] !== 0) { $collided['-y'] = TRUE; } } } // diagonals // +x +y $x = $letter + 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "7\n"; if ($collided['+x+y'] == FALSE && $position[$y][$x] === 'Q') { $isCheck = TRUE; $collided['+x+y'] = TRUE; } else { if ($collided['+x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x+y'] = TRUE; } } $x++; $y++; } // +x -y $x = $letter + 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "8\n"; if ($collided['+x-y'] == FALSE && $position[$y][$x] === 'Q') { $isCheck = TRUE; $collided['+x-y'] = TRUE; } else { if ($collided['+x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x-y'] = TRUE; } } $x++; $y--; } // -x +y $x = $letter - 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "9\n"; if ($collided['-x+y'] == FALSE && $position[$y][$x] === 'Q') { $isCheck = TRUE; $collided['-x+y'] = TRUE; } else { if ($collided['-x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x+y'] = TRUE; } } $x--; $y++; } // -x -y $x = $letter - 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "10\n"; if ($collided['-x-y'] == FALSE && $position[$y][$x] === 'Q') { $isCheck = TRUE; $collided['-x-y'] = TRUE; } else { if ($collided['-x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x-y'] = TRUE; } } $x--; $y--; } } else { if ($square === 'r') { $collided = array('+x' => FALSE, '+y' => FALSE, '-x' => FALSE, '-y' => FALSE); // straights // +x for ($x = $number + 1; $x < 8; $x++) { //echo "11\n"; if ($collided['+x'] === FALSE && $position[$x][$letter] === 'Q') { $isCheck = TRUE; $collided['+x'] = TRUE; } else { if ($collided['+x'] === FALSE && $position[$x][$letter] !== 0) { $collided['+x'] = TRUE; } } } // -x for ($x = $number - 1; $x >= 0; $x--) { //echo "12\n"; if ($collided['-x'] === FALSE && $position[$x][$letter] === 'Q') { $isCheck = TRUE; $collided['-x'] = TRUE; } else { if ($collided['-x'] === FALSE && $position[$x][$letter] !== 0) { $collided['-x'] = TRUE; } } } // +y for ($x = $letter + 1; $x < 8; $x++) { //echo "13\n"; if ($collided['+y'] === FALSE && $position[$number][$x] === 'Q') { $isCheck = TRUE; $collided['+y'] = TRUE; } else { if ($collided['+y'] === FALSE && $position[$number][$x] !== 0) { $collided['+y'] = TRUE; } } } // -y for ($x = $letter - 1; $x >= 0; $x--) { //echo "14\n"; if ($collided['-y'] === FALSE && $position[$number][$x] === 'Q') { $isCheck = TRUE; $collided['-y'] = TRUE; } else { if ($collided['-y'] === FALSE && $position[$number][$x] !== 0) { $collided['-y'] = TRUE; } } } } else { if ($square === 'b') { $collided = array('+x+y' => FALSE, '+x-y' => FALSE, '-x+y' => FALSE, '-x-y' => FALSE); // diagonals // +x +y $x = $letter + 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "15\n"; if ($collided['+x+y'] == FALSE && ($position[$y][$x] === 'Q' || $position[$y][$x] === 'R')) { $isCheck = TRUE; $collided['+x+y'] = TRUE; } else { if ($collided['+x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x+y'] = TRUE; } } $x++; $y++; } // +x -y $x = $letter + 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "16\n"; if ($collided['+x-y'] == FALSE && ($position[$y][$x] === 'Q' || $position[$y][$x] === 'R')) { $isCheck = TRUE; $collided['+x-y'] = TRUE; } else { if ($collided['+x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x-y'] = TRUE; } } $x++; $y--; } // -x +y $x = $letter - 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "17\n"; if ($collided['-x+y'] == FALSE && ($position[$y][$x] === 'Q' || $position[$y][$x] === 'R')) { $isCheck = TRUE; $collided['-x+y'] = TRUE; } else { if ($collided['-x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x+y'] = TRUE; } } $x--; $y++; } // -x -y $x = $letter - 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "18\n"; if ($collided['-x-y'] == FALSE && ($position[$y][$x] === 'Q' || $position[$y][$x] === 'R')) { $isCheck = TRUE; $collided['-x-y'] = TRUE; } else { if ($collided['-x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x-y'] = TRUE; } } $x--; $y--; } } else { if ($square === 'n') { //L shapes // ++x +y if ($letter + 2 < 8 && $number + 1 < 8) { if ($position[$number + 1][$letter + 2] === 'Q' || $position[$number + 1][$letter + 2] === 'R') { $isCheck = TRUE; } } // ++x -y if ($letter + 2 < 8 && $number - 1 >= 0) { if ($position[$number - 1][$letter + 2] === 'Q' || $position[$number - 1][$letter + 2] === 'R') { $isCheck = TRUE; } } // --x +y if ($letter - 2 >= 0 && $number + 1 < 8) { if ($position[$number + 1][$letter - 2] === 'Q' || $position[$number + 1][$letter - 2] === 'R') { $isCheck = TRUE; } } // --x -y if ($letter - 2 >= 0 && $number - 1 >= 0) { if ($position[$number - 1][$letter - 2] === 'Q' || $position[$number - 1][$letter - 2] === 'R') { $isCheck = TRUE; } } // ++y +x if ($letter + 1 < 8 && $number + 2 < 8) { if ($position[$number + 2][$letter + 1] === 'Q' || $position[$number + 2][$letter + 1] === 'R') { $isCheck = TRUE; } } // ++y -x if ($letter - 1 >= 0 && $number + 2 < 8) { if ($position[$number + 2][$letter - 1] === 'Q' || $position[$number + 2][$letter - 1] === 'R') { $isCheck = TRUE; } } // --y +x if ($letter + 1 < 8 && $number - 2 >= 0) { if ($position[$number - 2][$letter + 1] === 'Q' || $position[$number - 2][$letter + 1] === 'R') { $isCheck = TRUE; } } // --y -x if ($letter - 1 >= 0 && $number - 2 >= 0) { if ($position[$number - 2][$letter - 1] === 'Q' || $position[$number - 2][$letter - 1] === 'R') { $isCheck = TRUE; } } } else { if ($square === 'p') { // +y -x if ($letter - 1 >= 0 && $number + 1 < 8) { if ($position[$number + 1][$letter - 1] === 'Q' || $position[$number + 1][$letter - 1] === 'R' || $position[$number + 1][$letter - 1] === 'N' || $position[$number + 1][$letter - 1] === 'B') { $isCheck = TRUE; } } // +y +x if ($letter + 1 < 8 && $number + 1 < 8) { if ($position[$number + 1][$letter + 1] === 'Q' || $position[$number + 1][$letter + 1] === 'R' || $position[$number + 1][$letter + 1] === 'N' || $position[$number + 1][$letter + 1] === 'B') { $isCheck = TRUE; } } } else { if ($square === 'Q') { $collided = array('+x' => FALSE, '+y' => FALSE, '-x' => FALSE, '-y' => FALSE, '+x+y' => FALSE, '+x-y' => FALSE, '-x+y' => FALSE, '-x-y' => FALSE); // straights // +x for ($x = $number + 1; $x < 8; $x++) { //echo "19\n"; if ($collided['+x'] === FALSE && $position[$x][$letter] === 'q') { $isCheck = TRUE; $collided['+x'] = TRUE; } else { if ($collided['+x'] === FALSE && $position[$x][$letter] !== 0) { $collided['+x'] = TRUE; } } } // -x for ($x = $number - 1; $x >= 0; $x--) { //echo "20\n"; if ($collided['-x'] === FALSE && $position[$x][$letter] === 'q') { $isCheck = TRUE; $collided['-x'] = TRUE; } else { if ($collided['-x'] === FALSE && $position[$x][$letter] !== 0) { $collided['-x'] = TRUE; } } } // +y for ($x = $letter + 1; $x < 8; $x++) { //echo "21\n"; if ($collided['+y'] === FALSE && $position[$number][$x] === 'q') { $isCheck = TRUE; $collided['+y'] = TRUE; } else { if ($collided['+y'] === FALSE && $position[$number][$x] !== 0) { $collided['+y'] = TRUE; } } } // -y for ($x = $letter - 1; $x >= 0; $x--) { //echo "22\n"; if ($collided['-y'] === FALSE && $position[$number][$x] === 'q') { $isCheck = TRUE; $collided['-y'] = TRUE; } else { if ($collided['-y'] === FALSE && $position[$number][$x] !== 0) { $collided['-y'] = TRUE; } } } // diagonals // +x +y $x = $letter + 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "23\n"; if ($collided['+x+y'] == FALSE && $position[$y][$x] === 'q') { $isCheck = TRUE; $collided['+x+y'] = TRUE; } else { if ($collided['+x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x+y'] = TRUE; } } $x++; $y++; } // +x -y $x = $letter + 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "24\n"; if ($collided['+x-y'] == FALSE && $position[$y][$x] === 'q') { $isCheck = TRUE; $collided['+x-y'] = TRUE; } else { if ($collided['+x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x-y'] = TRUE; } } $x++; $y--; } // -x +y $x = $letter - 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "25\n"; if ($collided['-x+y'] == FALSE && $position[$y][$x] === 'q') { $isCheck = TRUE; $collided['-x+y'] = TRUE; } else { if ($collided['-x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x+y'] = TRUE; } } $x--; $y++; } // -x -y $x = $letter - 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "26\n"; if ($collided['-x-y'] == FALSE && $position[$y][$x] === 'q') { $isCheck = TRUE; $collided['-x-y'] = TRUE; } else { if ($collided['-x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x-y'] = TRUE; } } $x--; $y--; } } else { if ($square === 'R') { $collided = array('+x' => FALSE, '+y' => FALSE, '-x' => FALSE, '-y' => FALSE); // straights // +x for ($x = $number + 1; $x < 8; $x++) { //echo "27\n"; if ($collided['+x'] === FALSE && $position[$x][$letter] === 'q') { $isCheck = TRUE; $collided['+x'] = TRUE; } else { if ($collided['+x'] === FALSE && $position[$x][$letter] !== 0) { $collided['+x'] = TRUE; } } } // -x for ($x = $number - 1; $x >= 0; $x--) { //echo "28\n"; if ($collided['-x'] === FALSE && $position[$x][$letter] === 'q') { $isCheck = TRUE; $collided['-x'] = TRUE; } else { if ($collided['-x'] === FALSE && $position[$x][$letter] !== 0) { $collided['-x'] = TRUE; } } } // +y for ($x = $letter + 1; $x < 8; $x++) { //echo "29\n"; if ($collided['+y'] === FALSE && $position[$number][$x] === 'q') { $isCheck = TRUE; $collided['+y'] = TRUE; } else { if ($collided['+y'] === FALSE && $position[$number][$x] !== 0) { $collided['+y'] = TRUE; } } } // -y for ($x = $letter - 1; $x >= 0; $x--) { //echo "30\n"; if ($collided['-y'] === FALSE && $position[$number][$x] === 'q') { $isCheck = TRUE; $collided['-y'] = TRUE; } else { if ($collided['-y'] === FALSE && $position[$number][$x] !== 0) { $collided['-y'] = TRUE; } } } } else { if ($square === 'B') { $collided = array('+x+y' => FALSE, '+x-y' => FALSE, '-x+y' => FALSE, '-x-y' => FALSE); // diagonals // +x +y $x = $letter + 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "31\n"; if ($collided['+x+y'] == FALSE && ($position[$y][$x] === 'q' || $position[$y][$x] === 'r')) { $isCheck = TRUE; $collided['+x+y'] = TRUE; } else { if ($collided['+x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x+y'] = TRUE; } } $x++; $y++; } // +x -y $x = $letter + 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "32\n"; if ($collided['+x-y'] == FALSE && ($position[$y][$x] === 'q' || $position[$y][$x] === 'r')) { $isCheck = TRUE; $collided['+x-y'] = TRUE; } else { if ($collided['+x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['+x-y'] = TRUE; } } $x++; $y--; } // -x +y $x = $letter - 1; $y = $number + 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "33\n"; if ($collided['-x+y'] == FALSE && ($position[$y][$x] === 'q' || $position[$y][$x] === 'r')) { $isCheck = TRUE; $collided['-x+y'] = TRUE; } else { if ($collided['-x+y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x+y'] = TRUE; } } $x--; $y++; } // -x -y $x = $letter - 1; $y = $number - 1; while ($x < 8 && $y < 8 && $x >= 0 && $y >= 0) { //echo "34\n"; if ($collided['-x-y'] == FALSE && ($position[$y][$x] === 'q' || $position[$y][$x] === 'r')) { $isCheck = TRUE; $collided['-x-y'] = TRUE; } else { if ($collided['-x-y'] == FALSE && $position[$y][$x] !== 0) { $collided['-x-y'] = TRUE; } } $x--; $y--; } } else { if ($square === 'N') { //L shapes // ++x +y if ($letter + 2 < 8 && $number + 1 < 8) { if ($position[$number + 1][$letter + 2] === 'q' || $position[$number + 1][$letter + 2] === 'r') { $isCheck = TRUE; } } // ++x -y if ($letter + 2 < 8 && $number - 1 >= 0) { if ($position[$number - 1][$letter + 2] === 'q' || $position[$number - 1][$letter + 2] === 'r') { $isCheck = TRUE; } } // --x +y if ($letter - 2 >= 0 && $number + 1 < 8) { if ($position[$number + 1][$letter - 2] === 'q' || $position[$number + 1][$letter - 2] === 'r') { $isCheck = TRUE; } } // --x -y if ($letter - 2 >= 0 && $number - 1 >= 0) { if ($position[$number - 1][$letter - 2] === 'q' || $position[$number - 1][$letter - 2] === 'r') { $isCheck = TRUE; } } // ++y +x if ($letter + 1 < 8 && $number + 2 < 8) { if ($position[$number + 2][$letter + 1] === 'q' || $position[$number + 2][$letter + 1] === 'r') { $isCheck = TRUE; } } // ++y -x if ($letter - 1 >= 0 && $number + 2 < 8) { if ($position[$number + 2][$letter - 1] === 'q' || $position[$number + 2][$letter - 1] === 'r') { $isCheck = TRUE; } } // --y +x if ($letter + 1 < 8 && $number - 2 >= 0) { if ($position[$number - 2][$letter + 1] === 'q' || $position[$number - 2][$letter + 1] === 'r') { $isCheck = TRUE; } } // --y -x if ($letter - 1 >= 0 && $number - 2 >= 0) { if ($position[$number - 2][$letter - 1] === 'q' || $position[$number - 2][$letter - 1] === 'r') { $isCheck = TRUE; } } } else { if ($square === 'P') { // -y -x if ($letter - 1 >= 0 && $number - 1 >= 0) { if ($position[$number - 1][$letter - 1] === 'q' || $position[$number - 1][$letter - 1] === 'r' || $position[$number - 1][$letter - 1] === 'n' || $position[$number - 1][$letter - 1] === 'b') { $isCheck = TRUE; } } // -y +x if ($letter + 1 < 8 && $number - 1 >= 0) { if ($position[$number - 1][$letter + 1] === 'q' || $position[$number - 1][$letter + 1] === 'r' || $position[$number - 1][$letter + 1] === 'n' || $position[$number - 1][$letter + 1] === 'b') { $isCheck = TRUE; } } } } } } } } } } } } } } } } return $isCheck; }
function getPosition($points) { $score = getFinalScore(); $pos = 0; foreach ($score as $elemento) { $pos = $elemento['posizione']; if ($points > $elemento['punti']) { updatePosition($points); return $pos; } else { if ($points == $elemento['punti']) { return $pos; } } } return $pos + 1; }