Code covered by the BSD License  

Highlights from
Chess with "Greedy Edi"

image thumbnail

Chess with "Greedy Edi"

by

 

07 Nov 2009 (Updated )

Play chess against "Greedy Edi".

undoMove
function undoMove
% This function undos the last two half-moves.
global history;

if ~isempty(history.white) && ~isempty(history.black)
    for k=1:2 % last two half-moves
        % who was the last one to move: white or black
        % squence is important!!!
        if length(history.black)>=length(history.white)
            colour = 0; % blacks last move
        else
            colour = 1; % whites last move
        end

        if colour % undo white half move
            n    = length(history.white);
            [from, to]  = history.white_pos{n,:};
            fig  = history.white_fig{n};
            kfig = history.gained_figure_by_white{n};
        else % undo black half move
            n    = length(history.black);
            [from, to]  = history.black_pos{n,:};
            fig  = history.black_fig{n};
            kfig = history.gained_figure_by_black{n};
        end

        [value, handle_chess_figure] = getFigureName(fig); %get figure info
        handle_chess_figure('move',to, from); % move back
        resurrect_figure (kfig, to); % resurrect figure

        
        if colour % white then remove half-move from history
            if n>1
                history.white_pos(n,:)=[]; % note to delete an element from a list use round brackets!!!
                history.white_fig(n)=[];
                history.white(n)=[];
                history.gained_figure_by_white(n)=[];
            else % no more move needs special delete
                history.white_pos=[]; % note to delete an element from a list use round brackets!!!
                history.white_fig=[];
                history.white=[];
                history.gained_figure_by_white=[];
            end
        else
            if n>1
                history.black_pos(n,:)=[];
                history.black_fig(n)=[];
                history.black(n)=[];
                history.gained_figure_by_black(n)=[];
            else % no more move needs special delete
                history.black_pos=[];
                history.black_fig=[];
                history.black=[];
                history.gained_figure_by_black=[];
            end
        end
    end   
    
    global board;
    board.move_nb=board.move_nb-1;
end

Contact us