Ejemplo n.º 1
0
 /** public function get_hit_data
  *		Returns the turn 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_hit_data($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;
     $move_data = array();
     // because we may have only hit the piece at A8 (idx: 0), this will
     // return false unless we test for that location specifically
     if ($this->_history[$index]['hits'] || '0' === $this->_history[$index]['hits']) {
         // we need to grab the previous board here, and perform the move
         // without firing the laser so we get the proper orientation
         // of the pieces as they were hit in case any pieces were rotated
         // into the beam
         // create a dummy pharaoh class and set the board as it was prior to the laser (-1)
         $PH = new Pharaoh();
         $PH->set_board(expandFEN($this->_history[$index - 1]['board']));
         $PH->set_extra_info($this->_extra_info);
         // do the move and store that board
         $prev_board = $PH->do_move($this->_history[$index]['move'], false);
         $pieces = array();
         $hits = array_trim($this->_history[$index]['hits'], 'int');
         foreach ($hits as $hit) {
             $pieces[] = $prev_board[$hit];
         }
         $move_data = compact('hits', 'pieces');
     }
     if ($json) {
         return json_encode($move_data);
     }
     return $move_data;
 }
Ejemplo n.º 2
0
    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;
}
Ejemplo n.º 3
0
    /** 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;
    }