現在、移動チェッカーに問題があり、マトリックスの境界の外に見えないようにコードに入れているフェイルセーフが機能していないようです。何か提案はありますか?
また、機能していないように見えるという問題もあります (つまり、好きな場所にピースを配置できます!)。以前に使用したコードも以下にリストされています
コード:
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
実行時にエラーが返されました @ 境界:
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