try { if (false !== strpos($_POST['to'], 'split')) { // splitting obelisk $to = substr($_POST['to'], 0, 2); call($to); $from = Pharaoh::index_to_target($_POST['from']); $to = Pharaoh::index_to_target($to); call($from . '.' . $to); $Game->do_move($from . '.' . $to); } elseif ((string) $_POST['to'] === (string) (int) $_POST['to']) { // moving $to = $_POST['to']; call($to); $from = Pharaoh::index_to_target($_POST['from']); $to = Pharaoh::index_to_target($to); call($from . ':' . $to); $Game->do_move($from . ':' . $to); } else { // rotating $target = Pharaoh::index_to_target($_POST['from']); $dir = (int) ('r' == strtolower($_POST['to'])); call($target . '-' . $dir); $Game->do_move($target . '-' . $dir); } $return['action'] = 'RELOAD'; } catch (MyException $e) { $return['error'] = 'ERROR: ' . $e->outputMessage(); } echo json_encode($return); exit; }
/** static public function test_reflection * Tests the validity of the given reflection * for the given setup * * @param string setup * @param string [optional] reflection type (Origin, Long, Short) * @return bool if the reflection is valid */ public static function is_valid_reflection($setup, $type = 'Origin') { call(__METHOD__); call($setup); // expand the board FEN $xFEN = expandFEN($setup); // validate the setup try { self::is_valid_setup($xFEN); } catch (MyException $e) { throw $e; } // make sure the given xFEN has all the pieces reflected properly switch ($type) { case 'Origin': $reflect = array('A' => 'c', 'C' => 'a', 'B' => 'd', 'D' => 'b', 'X' => 'x', 'Y' => 'y', 'V' => 'v', 'W' => 'w', 'P' => 'p', 'H' => 'h', 'I' => 'i'); $reflect_keys = array_keys($reflect); // TOWER: may need to add some more reflect algorithms $_reflect = ' $return = 79 - $i; '; break; case 'Long': $reflect = array('A' => 'b', 'B' => 'a', 'C' => 'd', 'D' => 'c', 'X' => 'y', 'Y' => 'x', 'V' => 'v', 'W' => 'w', 'P' => 'p', 'H' => 'i', 'I' => 'h'); $reflect_keys = array_keys($reflect); // TOWER: may need to add some more reflect algorithms $_reflect = ' $tens = (int) floor($i / 10); $return = ((7 - $tens) * 10) + ($i % 10); '; break; case 'Short': $reflect = array('A' => 'd', 'D' => 'a', 'B' => 'c', 'C' => 'b', 'X' => 'y', 'Y' => 'x', 'V' => 'v', 'W' => 'w', 'P' => 'p', 'H' => 'i', 'I' => 'h'); $reflect_keys = array_keys($reflect); // TOWER: may need to add some more reflect algorithms $_reflect = ' $tens = (int) floor($i / 10); $return = ($tens * 10) + (9 - ($i % 10)); '; break; default: return true; break; } // TOWER: may need to change limits for ($i = 0; $i < 80; ++$i) { $c = $xFEN[$i]; if ('0' !== $c && strtoupper($c) === $c && in_array($c, $reflect_keys)) { eval($_reflect); // creates $return if ($reflect[$c] !== $xFEN[$return]) { throw new MyException(__METHOD__ . ': Invalid reflected character found at index ' . $i . ' (' . Pharaoh::index_to_target($i) . ') = ' . $c . '->' . $xFEN[$return] . ' should be ' . $reflect[$c]); } // remove the tested chars $xFEN[$i] = '.'; $xFEN[$return] = '.'; } } // we tested all silver -> red // and matching pairs were removed // now look for any remaining red if (preg_match('/[a-dhipvwxy]/', $xFEN, $matches, PREG_OFFSET_CAPTURE)) { // TODO: get index for faulty red pieces throw new MyException(__METHOD__ . ': Red piece found without matching Silver piece'); } return true; }