📄 chess.m
字号:
function Chess(which_figures)
% CHESS To play a game of chess.
% This function enables you to play a game of chess. You can select between
% two sets of figures ('which_figures').
%
% Inputs:
% * which_figures ... the set of figures (1...text, 2...figures)
%
% Example
% Starts a game of chess with the basic set of figures
%+ Chess
%
% To play a game of chess with letters instead of figures execute:
%+ Chess(1)
%
% See also: EDIs_move, King, Queen, Rook, Bishop, Knight, Pawn, stopCursor
%
%% Signature
% Author: W.Garn
% E-Mail: wgarn@yahoo.com
% Date: 2006/03/23 12:00:00
%
% Copyright 2006 W.Garn
%
if nargin<1
which_figures=2;
end
if ~(which_figures==1 || which_figures==2)
which_figures=2;
end
clc;
global board; % all information related to the chess board (NOT display)
global layer; % display info
%global cursor; % cursor information for moving the figures
global history; % record of moves
global EDIs %EDIs information (Artificial Intelligence)
global game;
% Initialize
game.status = 1; %0...game over; 1...game on; -1...EDI surrenders
reset_cursor;
% Display - Layer information
shift=-2;
layer.figures = 1.1+shift;
layer.board = 1+shift;
layer.board_frame = .9+shift;
layer.cursor = 1.05+shift;
% New game
start_position = [...
'rnbqkbnr';...
'pppppppp';...
' '; ' '; ' '; ' ';...
'PPPPPPPP';...
'RNBQKBNR'];
% Store board information
board.figures = start_position; % current position of figures
board.black = zeros(8); % only the values of the black figures
board.white = zeros(8);
board.active_color = 1; %1... white, 0...black
board.move_nb = 1;
board.last2field = ' '; % field before half move
board.which_set = which_figures; % which figures to use (1...text, 2... basic set)
% History of the game (record)
history.white = {}; %in chess notation
history.white_pos = {}; %in matrix notation
history.black = {};
history.black_pos = {};
% EDIs information
EDIs.color = 0; %black
fig = figure('Name','Chess - Greedy Edi','NumberTitle','off');
set(fig,'Color',[.9 .8 .5]);
ax = axes('color','none', ...
'xlim',[0 8]+[-.1 .1],'ylim',[0 8]+[-.1 .1],...
'xtick',[],'ytick',[],...
'NextPlot','add',...
'Layer','bottom',...
'DataAspectRatio',[1 1 1],...
'XColor','k','YColor','k','Box','On','LineWidth',5);
% Display figures
Knight('clear'); Pawn('clear'); Rook('clear');Bishop('clear');
Queen('clear');King('clear');
hf = zeros(8);
for i=1:8
for j=1:8
if board.figures(i,j)~=' '
fig = board.figures(i,j);
[value, handle_chess_figure] = getFigureName(fig);
if ~isempty(handle_chess_figure)
handle_chess_figure('init',[i j],getColor([i j]),[fig matrix2chess([i j])]); % matrix position, color, instance name
else
hf(i,j) = text(j-.5,9-i-.5,layer.figures,fig ,'FontName','Comic Sans MS','FontSize',25,'Color','k','HorizontalAlignment','center');
if double(fig)<double('a') % black
board.white(i,j)=Value_of_Figure(fig);
else %white
board.black(i,j)=Value_of_Figure(fig);
end
end
end
end
end
% Init additional figures
% pos = [4 5];
%Knight('init',pos,1,['N' matrix2chess(pos)]); % matrix position, color, instance name
% Board Layout
% For the patches to be in the background a second ax object must be
% created! - don't ask me why!
axx = axes('color','none', ...
'xlim',[0 8]+[-.1 .1],'ylim',[0 8]+[-.1 .1],...
'xtick',[],'ytick',[],...
'NextPlot','add',...
'Layer','bottom',...
'DataAspectRatio',[1 1 1],...
'XColor','k','YColor','k','Box','On','LineWidth',5);
uistack(axx,'bottom'); %This causes the patches to be in the background
if board.which_set==2, alphamap([0:1:255]/255'); end
for i = 1:8
for j=1:8
if mod(i+j,2), color = [.94 .94 .94]; %white
else color = [.87 .49 0]; %black
end
h = patch(i-1+[0 1 1 0], j-1+[0 0 1 1], [1 1 1 1]*layer.board, color);
end
end
h_new_game = uicontrol('Style', 'pushbutton', ...
'String','New game',...
'Unit','normalized',...
'Position', [.018 .85 .14 .07],...
'FontName','Comic',...
'FontUnits','normalized',...
'FontSize',.4,...
'Callback','close(gcf); Chess;');
% 'BackgroundColor',BackgroundColor,...
% "Program lives in events from now on."
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -