📄 stopcursor.m
字号:
function stopCursor(varargin)
% stopCursor Stops the cursor from moving and proceeds with the chess game.
% The cursors motion is stopped and the game logic is executed.
%
% This function is called from the Mouse Button Up Callback, and causes the
% mouse to stop moving. It checks whether the move (currently stored in the
% cursor) is legal. If it is legal the half-move is executed, otherwise the
% cursor color is set to red, indicating that it was illegal.
% If the half-move was legal, EDI moves. All moves are recorded here. Also
% the decision of who won the game is made.
% Hence, this function is one of the central control parts of the chess
% program.
%
%
%% Input and Output:
% * global board ... chess board and related information
% * global history ... game history
% * global game ... status of the game
% * varargin ... currently not used
%
% Example
% Stops a moving cursor and executes the move (stored in the cursor structure).
% |stopCursor|
%
% See also: cursorListener, setCursor, reset_cursor
%
%% 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 added surrender status of EDI
global cursor;
global board;
global history;
global game;
set(gcf,'WindowButtonMotionFcn',''); % first stop moving
hObject = cursor.to_h; %get(gcf,'CurrentObject');
cursor.to = cursor.current;
save('temp_board.mat','board','history','game','cursor'); % always save the board
if legal_move
% move the figure
fig = board.figures(cursor.from(1),cursor.from(2));
[name, handle_chess_figure] = getFigureName(fig);
handle_chess_figure('move',cursor.from, cursor.to);
%---------------------------------------------------------------
% include into move history and display move
addHistoryMove(cursor.from,cursor.to);
if board.active_color %white
fprintf(1, [num2str(board.move_nb,'%2i') '. ' history.white{length(history.white)} '\t\t'] );
else
fprintf(1, [history.black{length(history.black)} '\n'] );
end
%change color
board.active_color = mod(board.active_color+1,2);
%---------------------------------------------------------------
% set cursor.from
cursor.from = cursor.to;
pos = matrix2xy(cursor.from);
set(cursor.from_h,'XData',pos(1)-1+[0 1 1 0],'YData',pos(2)-1+[0 0 1 1]);
% set cursor.to
set(hObject,'EdgeColor','g');
% reset cursor
reset_cursor;
if ~game.status
msgbox({' *** Game over ***',...
' --- You have won!!! ---', ...
' Congratulations'},'Game status','warn')
else % still playing
%---------------------------------------------------------------
% Now it is EDIs turn
EDIs_move;
% move is completed
board.move_nb=board.move_nb+1;
board.active_color = mod(board.active_color+1,2); %change color
if game.status == -1 % EDI surrendered
msgbox({' *** Game over ***',...
' --- You have won!!! ---', ...
' EDI surrendered.'},'Game status','warn')
elseif ~game.status % game over
msgbox({'Game over',' --- Edi has won!!! ---', 'Better Luck - next time.'},'Game status','warn')
end
end
else %illegal move
if ~isempty(hObject)
% set cursor.to
set(hObject,'EdgeColor','r');
set(gcf,'WindowButtonMotionFcn',@displayCursor); % restart moving
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -