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".

in_check
function [answer, color] = in_check
% THIS FUNCTION IS OBSOLOETE
% is_in_check  Whether a figure will be in check.
% If a figure is moved from a to b, whether and if, which color is in check
% 
% This function is useful for the castling and to check whether a move is legal.
%
% Inputs:
% * from ... field from where the chess figure will move
% * to ... field to which it is supposed to move
% * global history ... the game history
% * global board ... the chess board and realted information
%
% Outputs:
% * answer ... Boolean answer (true if the figure was moved once).
% * color ... the color of the king which is checked (default black)
% If both kings are checked then "color(1)" is black and "color(2)" is
% white.
%
% Example
% Assume there is a certain chess postion and you like to try to move
% a chess figure from d4 [5 4] to d3 [6 4], then |is_in_check([5 4],[6 4])|
% will tell you whether this move will cause any king to be in check and
% "color" will tell you which king it will be. (Note, that the figure is
% not moved.)
%
% See also: King, Chess
%
% ToDo:
% It is possible that both kings are checked!
%
%% Signature
% Author: W.Garn
% E-Mail: wgarn@yahoo.com
% Date: 2006/03/23 12:00:00 
% 
% Copyright 2006 W.Garn
%
error('The "in_check" function is obsolote!');

global board
persistent avoid_recursion %empty when called the first time
% persistent variables are dangerous during debugging, because you have to
% manually reset them!!!

%logFlag=1; logFile=1;
if isempty(avoid_recursion)
    answer = 0; color=[];
    for color_k=1:2 % check both colors

        if mod(color_k,2) %white
            ef = board.white;
        else
            ef = board.black;
        end

        % get for each figure, its best move
        fig  = {}; % figures
        from = {}; % from where
        to   = {}; % to where
        gain = [];
        fig_nb = 1; % number of figure
        for i=1:8
            for j=1:8
                if ef(i,j)>0 % Edis figure
                    fig{fig_nb} = board.figures(i,j);
                    from{fig_nb} = [i j];
                    [name, handle_chess_figure] = getFigureName(fig{fig_nb});
                    if ~isempty(handle_chess_figure)
                        [tf, ga] = handle_chess_figure('best_move',[i j]);
                        if ~isempty(tf) % there is a best move
                            to{fig_nb}   = tf;
                            gain(fig_nb) = ga;
                            fig_nb       = fig_nb+1;
                        end
                    end
                end
            end
        end

        % choose figure which gives the best gain
        I = find ( gain == max(gain)); %figures with max. gain
        if gain(I(1)) == inf
            answer=1; % king is in check
            color = [color mod(color_k+1,2)]; % possible that both kings are checked.
            %if logFlag, fprintf(logFile,'King would be (is) in check.\n'); end 
            break;
        end
    end
    avoid_recursion = []; %lift recursion prohibtion!!!!
end

Contact us