Reversi有效移动检查器

时间:2013-06-04 18:57:52

标签: matlab

我一直在做一项任务,要求我在matlab中重新创建反转。

我遇到了一个突破点,我无法弄清楚如何编写代码段而不将其转换为线性索引。

基本上我需要做的是找出一种方法,将方向向量(即[-1; 0]垂直向上)添加到我已经存在的game_state向量中,以便找到它上面的数据。

    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 before continuing
                for count = 1:8
                    d = directions(count);
                    selected = 
                    while selected == player_number * -1 %while the selected piece is of enemy type

                          %move as long as you find your opponents stones
                         if you found at least 1 opponent stone and you end up on your own stone

                         else
                         end
                    end
                end

        else
        end
    end
end
   end

之后我的第二个问题是实际循环本身。 虽然我认为可以使用while循环来确定是否保持当前的方向,但我似乎无法找到自己做的方法。

任何帮助都将不胜感激。

谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

directions更改为包含字符串的单元格数组:

...
directions = { 'd_l' 'd_ul' 'd_u' 'd_ur' 'd_r' 'd_dr' 'd_d' 'd_dl' };
...

然后代码的下一部分就会像这样(填空!):

for count = 1:8
    d = eval( directions{count} );
    ti = ci+d(1);
    tj = cj+d(2);

    % Check if out of the board
    ...

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

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

        % Check if out of the board
        ...

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

    % Check if you moved over enemy pieces & whether you landed on your piece
    ...

end