-1

目前我的移动检查器出现问题,似乎我在代码中放入的故障保护使其看起来不超出矩阵的范围不起作用,有什么建议吗?

还有一个问题是它似乎不起作用(即我仍然可以将碎片放在任何我想要的地方!)。我之前使用的代码也在下面列出

代码:

function legal = legalMove()
    d_l = [0, -1];
    d_r = [0, 1];
    d_u = [-1, 0];
    d_d = [1, 0];
    d_ul = [-1, -1];
    d_ur = [-1, 1];
    d_dl = [1, -1];
    d_dr = [1, 1];
    directions = {'d_l' 'd_ul' 'd_u' 'd_ur' 'd_r' 'd_dr' 'd_d' 'd_dl'};
    valid_moves = zeros(8,8);

    for ci = 1:8
        for cj = 1:8
            if game_state(ci,cj) == 0 %check element = 0
        for count = 1:8
        d = eval( directions{count} );
        ti = ci+d(1);
        tj = cj+d(2);

        % Check if out of the board
            if (ti > 8 || ti < 1) || (tj > 8 || tj < 1)
                break
            else
        % Number of enemy pieces you went over
        cnt = 0;
        selected = game_state(ti, tj);

        % Move while going over enemy pieces
        while selected == player_number * -1
            ti = ti + d(1);
            tj = tj + d(2);
            selected = game_state(ti, tj);

            % Check if out of the board
                if (ti > 8 || ti < 1) || (tj > 8 || tj < 1)
                break
                else
                end

            % Count pieces you went over
            cnt = cnt + 1;
        end
            end

        % Check if you moved over enemy pieces & whether you landed on your piece
        if selected == player_number
            valid_moves(ti,tj) = 1;
        else
        end

        end
            else
            end
        end
    end

    if ~isempty(valid_moves)
        legal = 1;
    else
        legal = 0;
    end
end

完成@boundries 时返回错误:

Attempted to access game_state(0,7); index must be a positive integer or
logical.

Error in umpire/legalMove (line 217)
                selected = game_state(ti, tj);

Error in umpire/buttonPress (line 85)
        legal = legalMove();

另一块:

function buttonPress(hObject, eventdata)



    ended = game_is_over();

    if ended == 1;
        setAllInactive();
        winner = calc_winner();
        if winner == -1;
            set(stat_text,'string','Winner is White! Restart?')
        elseif winner == 1;
            set(stat_text,'string','Winner is Black! Restart?')
        else
            set(stat_text,'string','Game is a tie! Restart?')
        end
    else
    end

    legal = legalMove();
    if legal ~= 1;
        set(stat_text,'Illegal move! Try again')
        return
    else
    end

    game_state(get(hObject,'userdata')) = player_number;
    drawScreen();
    player_number = player_number * -1;
end
4

1 回答 1

0

在您指定变量selected的第二个位置(靠近函数legalMove的末尾),您在错误的位置进行了板外检查。

这是一个固定版本

    % ...
    % Number of enemy pieces you went over
    cnt = 0;
    selected = game_state(ti, tj);

    % Move while going over enemy pieces
    while selected == player_number * -1
        ti = ti + d(1);
        tj = tj + d(2);

        % Check if out of the board
            if (ti > 8 || ti < 1) || (tj > 8 || tj < 1)
            break
            else
            end

        selected = game_state(ti, tj);

        % Count pieces you went over
        cnt = cnt + 1;
    end
        end

    % Check if you moved over enemy pieces & whether you landed on your piece
    if selected == player_number
        valid_moves(ti,tj) = 1;
    else
    end
end
于 2013-06-05T14:06:53.023 回答