Example #1
0
/**
 * select_piece() - Creates  $possible_moves
 * The returned data will be used when building markup with clickable pieces
 */
function select_piece($board_array, $current_player, $from_field)
{
    // Find fields, this piece can generally move to
    // Also checks for obstacles and possible captures
    $clickable = possible_move_list($board_array, $current_player, $from_field);
    // Eliminate moves that would end in own king being in check
    $new = array($clickable[0]);
    list($f_row, $f_col) = field_to_rowcol($from_field);
    $piece = $board_array[$f_row][$f_col];
    foreach ($clickable as $to_field) {
        if ($from_field != $to_field) {
            // Ignore deselection
            list($t_row, $t_col) = field_to_rowcol($to_field);
            $new_array = apply_move($board_array, $f_row, $f_col, $t_row, $t_col);
            $king_field = find_king($new_array, $current_player);
            $hot_fields = hot_fields($new_array, $current_player);
            if (!in_array($king_field, $hot_fields)) {
                // King is NOT under attack when doing the move
                $attacked = false;
                // Not direclty, at least
                // Check, if we try to castle and any fields
                // are under attack, preventing the move.
                $dir = $t_col - $f_col;
                if ($piece == 'L') {
                    // White king
                    if ($dir == -2) {
                        // B, C, D must be free
                        $attacked = in_array('B1', $hot_fields) || in_array('C1', $hot_fields) || in_array('D1', $hot_fields);
                    } else {
                        if ($dir == +2) {
                            // F and G must be free
                            $attacked = in_array('F1', $hot_fields) || in_array('G1', $hot_fields);
                        }
                    }
                }
                if ($piece == 'l') {
                    // Black king
                    if ($dir == -2) {
                        // B, C, D must be free
                        $attacked = in_array('B8', $hot_fields) || in_array('C8', $hot_fields) || in_array('D8', $hot_fields);
                    } else {
                        if ($dir == +2) {
                            // F and G must be free
                            $attacked = in_array('F8', $hot_fields) || in_array('G8', $hot_fields);
                        }
                    }
                }
                if (!$attacked) {
                    $new[] = $to_field;
                }
            }
        }
    }
    $clickable = $new;
    $selected = array($clickable[0]);
    return array($clickable, $selected);
}
Example #2
0
/**
 * king_in_check() - return true, if king is in check
 */
function king_in_check($board_array, $current_player)
{
    $ret = false;
    $king_field = find_king($board_array, $current_player);
    $opponents_pieces = find_movable_pieces($board_array, !$current_player);
    // See, if any of the opponents pieces can capture the player's king
    foreach ($opponents_pieces as $o) {
        $captures = possible_move_list($board_array, !$current_player, $o);
        $ret |= in_array($king_field, $captures);
    }
    return $ret;
}