📄 edis_move.m
字号:
function EDIs_move
% EDIs_move Executes EDIs move and updates the game history
% EDIs move is executed and the game history is updated.
%
% In this version EDI is pretty greedy. He looks whether there is
% something to "eat"(beat). If there is something to eat, he does without
% regard, whether he will loose something or even wether he gets himself
% into a check. Hence, illegal moves can occure.
% Actually I implemented already a "is_in_check" function (used for the
% castling). In one of my next steps I will integrate it here.
%
% How does EDI work? For each of his figures he looks out for the best
% move. The best move for each figues is determined in each corresponding
% figure classe (i.e. Knight). From all the best moves he chooses the one
% which promisses the highest gain. Right now he looks only half a move
% ahead.
%
%% Used Parameters
% * global board ... chess board and related information
% * global EDIs ... global information about EDI (i.e. color)
% * global history ... game history (all the past moves)
%
% Example
% Let's say, we are in the middle of a game. Then EDI computes the next
% half-move, performs it and also records it.
%
% See also: Chess, stopCursor, Knight, addHistoryMove
%
%% Signature
% Author: W.Garn
% E-Mail: wgarn@yahoo.com
% Date: 2006/03/23 12:00:00
%
% Copyright 2006 W.Garn
%
%% Last modifications
% * 30/3/2006 EDI sees that he "is in check", tries with a "best move" to
% avoid the loss.
%
global board
global EDIs
global history
global game
logFlag = 1; logFile=1;
% first evaluate the move
% select figure
if logFlag, fprintf(logFile,'EDI started ...\n'); end
if EDIs.color %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
if logFlag, fprintf(logFile,'EDI has analysed all his figures.\n'); end
if ~isempty(gain)
% choose figure which gives the best gain
I = find ( gain == max(gain)); %figures with max. gain
% select one of the figures with max. gain randomly
sI = ceil(length(I)*rand); %(0,1]
% is in check
[answer, color] = is_in_check(from{I(sI)}, to{I(sI)});
if logFlag, if answer, fprintf(logFile,'EDI identified check!\n');end, end
%---------------------------------------------------------------
% if check then systematic approach
% 1. is there a best move which prohibits the check
check=answer; %1...check, 0...no check
k=0;
if (answer && color(1)==EDIs.color) || length(color)==2 % equals EDI is in check then
% systematic approach of choosing a figure
[s,I] = sort(gain);
k = length(s);
while check && k>0
sI = I(k);
if ~is_in_check(from{sI}, to{sI})
check=0; % found a way to avoid the check!
if logFlag, fprintf(logFile,'A best move to avoid the check: %s to %s.\n',matrix2chess(from{sI}),matrix2chess(to{sI}));end
end
k=k-1;
end
end
%--------------
% 2. if no best move prohibits the check, then look for alternatives
if k==0 && check && color(1)==EDIs.color % found no way to avoid check, using the best moves
if logFlag, fprintf(logFile,'EDI can not avoid check!\n',name);end
% there is no best move to avoid the check.
% alternatevly implement ...
% 1. run away
EDIs_King_pos = King('getPosition',EDIs.color);
P = King('possible_moves',EDIs_King_pos); % all possible moves
if ~isempty(P)
% perform move
if logFlag, fprintf(logFile,'EDI will run away with his King.\n'); end
King('move',EDIs_King_pos{1} , P(1,:)); % to the first possible field
% update record
addHistoryMove(EDIs_King_pos{1},P(1,:));
if EDIs.color %white
fprintf(1, [num2str(board.move_nb) '. ' history.white{length(history.white)} '\t\t'] );
else
fprintf(1, [history.black{length(history.black)} '\n'] );
end
if logFlag, fprintf(logFile,'EDI recored his %i. move.\n',board.move_nb); end
else % no field to go to
game.status = -1; % Edi surrenders
end
% 2. beat attacking figure
% 3. put something in between
%---------------------------------------------------------------
% perform move and register it (if no check problem occured)
elseif game.status>0 % game is still on
% perform move
[name, handle_chess_figure] = getFigureName(fig{I(sI)});
if logFlag, fprintf(logFile,'EDI will move his %s.\n',name); end
if ~isempty(handle_chess_figure)
handle_chess_figure('move',from{I(sI)} , to{I(sI)});
end
if logFlag, fprintf(logFile,'EDI moved his %s.\n',name); end
% update record
addHistoryMove(from{I(sI)},to{I(sI)});
if EDIs.color %white
fprintf(1, [num2str(board.move_nb) '. ' history.white{length(history.white)} '\t\t'] );
else
fprintf(1, [history.black{length(history.black)} '\n'] );
end
if logFlag, fprintf(logFile,'EDI recored his %i. move.\n',board.move_nb); end
end
else % no best move found
game.status = -1; % Edi surrenders
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -