0

我一直在做一项任务,需要我在 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 循环来确定是否保持当前方向,但我似乎无法找到自己的方法。

任何援助将不胜感激。

谢谢你的时间!

4

1 回答 1

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
于 2013-06-04T20:20:38.180 回答