/** public function get_move * Returns the data for the given move index * * @param int optional move history index * @param bool optional return as JSON string * @return array or string previous turn */ public function get_move($index = null, $json = false) { call(__METHOD__); call($index); call($json); if (is_null($index)) { $index = count($this->_history) - 1; } $index = (int) $index; $json = (bool) $json; $turn = $this->_history[$index]; $board = expandFEN($turn['board']); if (!empty($this->_history[$index - 1])) { $board = expandFEN($this->_history[$index - 1]['board']); } if (!$turn['move']) { if ($json) { return 'false'; } return false; } $move = array(); $move[0] = Pharaoh::target_to_index(substr($turn['move'], 0, 2)); if ('-' == $turn['move'][2]) { $move[1] = $move[0][0]; $move[2] = $turn['move'][3]; } else { $move[1] = Pharaoh::target_to_index(substr($turn['move'], 3, 2)); $move[2] = (int) (':' == $turn['move'][2]); } $move[3] = Pharaoh::get_piece_color($board[$move[0]]); if ($json) { return json_encode($move); } $move['from'] = $move[0]; $move['to'] = $move[1]; $move['extra'] = $move[2]; $move['color'] = $move[3]; return $move; }
/** static public function is_valid_setup * Tests the validity of the given setup * * @param string setup * @return bool valid setup */ public static function is_valid_setup($setup) { call(__METHOD__); call($setup); // expand the board FEN $xFEN = expandFEN($setup); if (80 != strlen($xFEN)) { throw new MyException(__METHOD__ . ': Incorrect board size'); } // test for a pharaoh on both sides if (!preg_match('/P/', $xFEN, $s_match) || !preg_match('/p/', $xFEN, $r_match)) { throw new MyException(__METHOD__ . ': Missing one or both Pharaohs'); } // make sure there's only one of each pharaoh if (1 != count($s_match) || 1 != count($r_match)) { throw new MyException(__METHOD__ . ': Too many of one or both Pharaohs'); } // test for a sphynx on both sides (if any) if (preg_match('/[EFJK]/i', $xFEN) && (!preg_match('/[EFJK]/', $xFEN, $s_match) || !preg_match('/[efjk]/', $xFEN, $r_match))) { throw new MyException(__METHOD__ . ': Missing one or both Sphynxes'); } // make sure there's only one of each sphynx if (1 != count($s_match) || 1 != count($r_match)) { throw new MyException(__METHOD__ . ': Too many of one or both Sphynxes'); } // look for invalid characters if (preg_match('/[^abcdefhijklmnoptvwxy0]/i', $xFEN)) { throw new MyException(__METHOD__ . ': Invalid characters found in setup'); } // test for pieces on incorrect colors $not_silver = array(0, 10, 20, 30, 40, 50, 60, 70, 8, 78); foreach ($not_silver as $i) { if ('0' != $xFEN[$i] && 'silver' == Pharaoh::get_piece_color($xFEN[$i])) { throw new MyException(__METHOD__ . ': Silver piece on red square at ' . $i); } } $not_red = array(9, 19, 29, 39, 49, 59, 69, 79, 1, 71); foreach ($not_red as $i) { if ('0' != $xFEN[$i] && 'red' == Pharaoh::get_piece_color($xFEN[$i])) { throw new MyException(__METHOD__ . ': Red piece on silver square at ' . $i); } } return true; }