-1
當前我的移動檢查器出現問題,似乎我放入代碼以使其看起來不在矩陣範圍之外的失敗安全模式無法正常工作,有什麼建議嗎?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
謝謝!我仍然有這樣的問題,即我仍然可以在棋盤上的任何位置放置棋子,但在那裏有幫助嗎? –
@WilliamNapier你的意思是說你可以把它們放在其他物件上?在這種情況下,你可以創建一個數組來保存棋子的位置,並向棋子添加檢查,即'if pieceArray(ti,tj)== 1,break;其他%移動; (在這個例子中,如果棋盤的那一部分沒有棋子,那麼pieceArray(ti,tj)爲零) – user2448027
不,我的意思是我可以將棋子放置在棋盤的任何位置,就像在條件「有效移動」檢查程序仍然沒有正確執行 –