function moves = solverv2(board)
[m,n] = size(board);
s = @(i,j) (i + m*(j-1));
moves = [0 0 0 0];
p = 1;
numpegs = nnz(board>0);
lastPegIdx = 0;
while (p < numpegs)
[i,j] = find(board>0);
I = [i;i;i;i];
J = [j;j;j;j];
K = [i;i;i-2;i+2];
L = [j-2;j+2;j;j];
h = find(K>0 & K<=m & L>0 & L<=n);
Kfrom = s(I,J); Kto = s(K,L);
Kmp = (Kfrom+Kto)/2;
h = h(board(Kto(h))==0 & board(Kmp(h))>0);
if isempty(h)
return;
end
scores = board(Kmp(h)) - (lastPegIdx~=Kfrom(h)).*board(Kfrom(h));
[maxscore, q] = max(scores);
pegWeights = board(s(i,j));
score_thresh = mean(pegWeights) - max(pegWeights);
if maxscore < score_thresh
return;
end
f = Kfrom(h(q));
t = Kto(h(q));
mp = (f+t)/2;
board(t) = board(f);
board([f mp]) = 0;
lastPegIdx = t;
[rf cf] = ind2sub([m n],f);
[rt ct] = ind2sub([m n],t);
moves(p,:) = [rf cf rt ct];
p = p + 1;
end
|