-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickCapturePiece.m
More file actions
138 lines (128 loc) · 5.24 KB
/
ClickCapturePiece.m
File metadata and controls
138 lines (128 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
%CapturePiece Part of the Click Series of Functions - Enables capture
function [chessboard,piece_colour, num_moves,allowscheck]=ClickCapturePiece(v1,v2,x_ori,y_ori,B,piece_colour,chessboard,...
num_moves,parameters,PM,handles,onlyAIoption,move_x,move_y,varargin)
%--------------------------------------------------------------------------
% Init values,conversions and click location
%--------------------------------------------------------------------------
if(mod(B.info.turn,2)==1)
colourturn = 119;
oppositecolour = 98;
else
colourturn = 98;
oppositecolour = 119;
end
if onlyAIoption == 0
clickP = get(gca,'CurrentPoint');
x = ceil(clickP(1,2));
y = ceil(clickP(1,1));
%---- Conversion from Graph grid to B.top grid ---------------------------
x = 13-x;
y = y + 4;
%--------Conversion from B.Top grid to Chessboard grid--------------------
p_x = x - 4; %p_x is necessary because it is the current clicked position
p_y = y - 4;
ori_x = x_ori - 4; %The difference is that ori_x is for chessboard,
ori_y = y_ori - 4; %x_ori is for B.top
else
p_x = move_x; %Where is it moving to
p_y = move_y;
ori_x = x_ori; %Where was it originally
ori_y = y_ori;
end
%-------------------------------------------------------------------------
% Checks if King is exposed to check in any way
%-------------------------------------------------------------------------
%The method used is to create a future chessboard based on the move
%requested
fboard = chessboard;
f_p_colour= piece_colour;
f_num_moves = num_moves;
%This step officially moves the piece
fboard(p_x,p_y) = chessboard(ori_x,ori_y);
f_p_colour(p_x,p_y) = piece_colour(ori_x,ori_y);
f_num_moves(p_x,p_y) = num_moves(ori_x,ori_y) + 1;
%This step empties the previous box
fboard(ori_x,ori_y) = 0;
f_p_colour(ori_x,ori_y) = 0;
f_num_moves(ori_x,ori_y) = 0;
%Analyses the future board
[potentialfuturemoves,capt_index_future] = analyseboard(fboard,...
f_p_colour,f_num_moves,oppositecolour);
[allowscheck]=KingCheck(fboard,f_p_colour,colourturn,...
capt_index_future,potentialfuturemoves);
if allowscheck==1 && onlyAIoption == 0
set(handles.gameconsole,'String','King will be left in check, move invalid')
end
%-------------------------------------------------------------------------
%Ensures it can only move legally
if PM(p_x,p_y)==2 && chessboard(p_x,p_y) ~= 10 && allowscheck==0
B.info.turn = B.info.turn + 1;
%-------------------------------------------------------------------------
% This is to edit the backend chessboard matrix
%-------------------------------------------------------------------------
%This step officially moves the piece
chessboard = fboard;
piece_colour = f_p_colour;
num_moves = f_num_moves;
%-------------To Check Opposing Side -------------------------------------
[potentialmoves,capt_index] = analyseboard(chessboard,piece_colour,num_moves,colourturn);
[checkopp]=KingCheck(chessboard,piece_colour,oppositecolour,capt_index,potentialmoves);
if checkopp == 1 && onlyAIoption == 0
set(handles.checkstat,'String','Check')
[ischeckmate]=checkmate(B,chessboard,piece_colour, num_moves);
if ischeckmate
set(handles.checkstat,'String','Checkmate, White Wins')
end
elseif checkopp == 0 && onlyAIoption ==0
[ischeckmate]=checkmate(B,chessboard,piece_colour, num_moves);
if ischeckmate
set(handles.checkstat,'String','Stalemate')
else
set(handles.checkstat,'String','')
end
end
if onlyAIoption == 0
[B] = readchessboard(B,chessboard,piece_colour);
%-------------------------------------------------------------------------
% Redraws the Board
%-------------------------------------------------------------------------
icount=0;
for i=1:71
icount=icount+1;
if mod(i,2)==1
rectangle('Position',[parameters.xx(icount),parameters.yy(icount),...
parameters.dx ,parameters.dx],'Curvature',[0,0],...
'FaceColor',[0.82 0.545 0.278])
else
rectangle('Position',[parameters.xx(icount),parameters.yy(icount),...
parameters.dx ,parameters.dx],...
'Curvature',[0,0],'FaceColor',[1 0.808 0.62])
end
end
for r=1:parameters.rows
for c=1:parameters.cols
if ~isempty(B.top(r+B.info.pad/2,c+B.info.pad/2).image)
% load the image
[X, map, alpha] = imread(B.top(r+B.info.pad/2,c+B.info.pad/2).image);
% draw the image
imHdls(r,c) = image(c+[0 1]-1,[parameters.rows-1 parameters.rows]-r+1,...
mirrorImage(X),'AlphaData',mirrorImage(alpha),...
'ButtonDownFcn',{@ClickPiece,B,piece_colour,chessboard,...
num_moves,parameters,potentialmoves,handles});
end
end
end
drawnow;
if(get(handles.choice2,'Value')==1)
AIControl(B,piece_colour,chessboard,num_moves,parameters, handles);
end
if(get(handles.choice3,'Value')==1)
AIvsAI(B,piece_colour,chessboard,num_moves,parameters, handles)
end
if(get(handles.choice1,'Value')==1)
PlayerVsPlayer( B,piece_colour,chessboard,num_moves,parameters, handles )
end
end
%---------------------------------------------------------------------------------------
end
end