📄 is_in_check.m
字号:
function [answer, color] = is_in_check(from, to)
% 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)
%
% 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
%
answer = 0; % not in check
color = 0;
try_moving = 0;
global board
persistent avoid_recursion %empty when called the first time
if isempty(avoid_recursion)
avoid_recursion = 1;
if nargin==2 % move first the figure
if board.figures(from(1),from(2)) ~= ' ' %there is a figure moved
try_moving=1;
%figures
fig_to = board.figures(to(1),to(2)); %remember
board.figures(to(1),to(2))=board.figures(from(1),from(2));
board.figures(from(1),from(2))=' ';
%values
w_to = board.white(to(1),to(2)); %remember
board.white(to(1),to(2))=board.white(from(1),from(2));
board.white(from(1),from(2))=0; %free it up
b_to = board.black(to(1),to(2)); %remember
board.black(to(1),to(2))=board.black(from(1),from(2));
board.black(from(1),from(2))=0; %free it up
end
end
% look whether king is in check
[answer, color] = in_check;
% move figure back from where it came
if try_moving %then reset
%figures
board.figures(from(1),from(2))=board.figures(to(1),to(2));
board.figures(to(1),to(2)) = fig_to;
%values
board.white(from(1),from(2)) = board.white(to(1),to(2));
board.white(to(1),to(2)) = w_to;
board.black(from(1),from(2)) = board.black(to(1),to(2));
board.black(to(1),to(2)) = b_to;
end % finished reversing move
avoid_recursion = []; %lift recursion prohibtion
end
%-----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -