Matlab Reversi'有效移动'检查

时间:2013-06-05 13:34:59

标签: matlab reversi

目前我的移动检查器存在问题,似乎我在代码中输入的故障保护使其看起来不在矩阵的范围之外是不行的,有什么建议吗?

还有一个问题似乎不起作用(即我仍然能够在任何地方放置碎片!)。我之前使用的代码也列在下面

代码:

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

1 个答案:

答案 0 :(得分:0)

在第二个分配变量 selected 的位置(靠近函数 legalMove 的末尾),您在错误的位置进行了板外检查。< / p>

这是一个固定版本

    % ...
    % 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