function computerdown(x,y)
%Actions after mouse downc
% computer step
% See also renju.
%By:Qin Qiang
%Email: qinqiang.1987@163.com
%Date: 2010/11/30
global chessboardMatrix
nodes=15;
row=nodes;
col=nodes;
%判断游戏结束?如果是,程序返回
global winIF
if winIF~=0
return;
end
%computer step
%定义棋盘空闲子的重要性矩阵
chessfenshu=zeros(row,col,'uint8');
for m=1:15
for n=1:15
if chessboardMatrix(m,n)==0
%定义每空闲子落下进攻子jgfenshu或者防守子fsfenshu的最优棋型分数
jgfenshu=0;fsfenshu=0;
%4个方向下判断落子位置在各方向两边四子(每个方向共8子)为进攻子(1)、防守子(2)、空闲(0)、超出棋盘(3),checkbow四行分别存横、竖、左斜、右斜
checkbow=zeros(4,8);
%检索横向棋型,并存储横向8子的落子情况
for t=1:4
%如果超出棋盘范围,给虚拟该位置赋值3
if n-t<=0
checkbow(1,5-t)=3;
%如果在棋盘范围内,正常判断赋值
else
%该位置有落子,判断为进攻子(1)或者防守子(2)
if chessboardMatrix(m,n-t)~=0
if mod(chessboardMatrix(m,n-t),2)==0
checkbow(1,5-t)=1;
else
checkbow(1,5-t)=2;
end
%给位置空闲赋值0
else
checkbow(1,5-t)=0;
end
end
end
for t=1:4
%如果超出棋盘范围,给虚拟该位置赋值3
if n+t>=16
checkbow(1,4+t)=3;
%如果在棋盘范围内,正常判断赋值
else
if chessboardMatrix(m,n+t)~=0
if mod(chessboardMatrix(m,n+t),2)==0
checkbow(1,4+t)=1;
else
checkbow(1,4+t)=2;
end
else
checkbow(1,4+t)=0;
end
end
end
%检索竖向棋型,并存储竖向8子的落子情况
for t=1:4
if m-t<=0
checkbow(2,5-t)=3;
else
if chessboardMatrix(m-t,n)~=0
if mod(chessboardMatrix(m-t,n),2)==0
checkbow(2,5-t)=1;
else
checkbow(2,5-t)=2;
end
else
checkbow(2,5-t)=0;
end
end
end
for t=1:4
if m+t>=16
checkbow(2,4+t)=3;
else
if chessboardMatrix(m+t,n)~=0
if mod(chessboardMatrix(m+t,n),2)==0
checkbow(2,4+t)=1;
else
checkbow(2,4+t)=2;
end
else
checkbow(2,4+t)=0;
end
end
end
%检索左斜向棋型,并存储左斜向8子的落子情况
for t=1:4
if (m+t>=16)||(n-t<=0)
checkbow(3,5-t)=3;
else
if chessboardMatrix(m+t,n-t)~=0
if mod(chessboardMatrix(m+t,n-t),2)==0
checkbow(3,5-t)=1;
else
checkbow(3,5-t)=2;
end
else
checkbow(3,5-t)=0;
end
end
end
for t=1:4
if (m-t<=0)||(n+t>=16)
checkbow(3,4+t)=3;
else
if chessboardMatrix(m-t,n+t)~=0
if mod(chessboardMatrix(m-t,n+t),2)==0
checkbow(3,4+t)=1;
else
checkbow(3,4+t)=2;
end
else
checkbow(3,4+t)=0;
end
end
end
%检索右斜向棋型,并存储右斜向8子的落子情况
for t=1:4
if (m-t<=0)||(n-t<=0)
checkbow(4,5-t)=3;
else
if chessboardMatrix(m-t,n-t)~=0
if mod(chessboardMatrix(m-t,n-t),2)==0
checkbow(4,5-t)=1;
else
checkbow(4,5-t)=2;
end
else
checkbow(4,5-t)=0;
end
end
end
for t=1:4
if (m+t>=16)||(n+t>=16)
checkbow(4,4+t)=3;
else
if chessboardMatrix(m+t,n+t)~=0
if mod(chessboardMatrix(m+t,n+t),2)==0
checkbow(4,4+t)=1;
else
checkbow(4,4+t)=2;
end
else
checkbow(4,4+t)=0;
end
end
end
%检索该空闲子当落下进攻子时的最优棋型分数
% 建立每子四个方向(横、竖、左斜、右斜)棋型代码向量,并初始化
chessluozi=[0 0 0 0];
%检索各个方向(横、竖、左斜、右斜)棋型
for t=1:4
%成5
if isequal(checkbow(t,1:4),[1 1 1 1])||isequal(checkbow(t,2:5),[1 1 1 1])||isequal(checkbow(t,3:6),[1 1 1 1])||isequal(checkbow(t,4:7),[1 1 1 1])||isequal(checkbow(t,5:8),[1 1 1 1])
chessluozi(t)=1;
%成活4
elseif isequal(checkbow(t,1:5),[0 1 1 1 0])||isequal(checkbow(t,2:6),[0 1 1 1 0])||isequal(checkbow(t,3:7),[0 1 1 1 0])||isequal(checkbow(t,4:8),[0 1 1 1 0])
chessluozi(t)=2;
%成死4
elseif isequal(checkbow(t,1:5),[2 1 1 1 0])||isequal(checkbow(t,2:6),[2 1 1 1 0])||isequal(checkbow(t,3:7),[2 1 1 1 0])||isequal(checkbow(t,4:8),[2 1 1 1 0])||isequal(checkbow(t,1:5),[0 1 1 1 2])||isequal(checkbow(t,2:6),[0 1 1 1 2])||isequal(checkbow(t,3:7),[0 1 1 1 2])||isequal(checkbow(t,4:8),[0 1 1 1 2])
chessluozi(t)=3;
%成活3
elseif isequal(checkbow(t,1:6),[0 0 1 1 0 0])||isequal(checkbow(t,2:7),[0 0 1 1 0 0])||isequal(checkbow(t,3:8),[0 0 1 1 0 0])
chessluozi(t)=4;
%成死3
elseif isequal(checkbow(t,2:6),[2 1 1 0 0])||isequal(checkbow(t,3:7),[2 1 1 0 0])||isequal(checkbow(t,4:8),[2 1 1 0 0])||isequal(checkbow(t,1:5),[0 0 1 1 2])||isequal(checkbow(t,2:6),[0 0 1 1 2])||isequal(checkbow(t,3:7),[0 0 1 1 2])||isequal(checkbow(t,1:5),[2 0 1 1 0])||isequal(checkbow(t,2:6),[2 0 1 1 0])||isequal(checkbow(t,3:7),[2 0 1 1 0])||isequal(checkbow(t,2:6),[0 1 1 0 2])||isequal(checkbow(t,3:7),[0 1 1 0 2])||isequal(checkbow(t,4:8),[0 1 1 0 2])
chessluozi(t)=5;
%成活2
elseif isequal(checkbow(t,2:6),[0 0 1 0 0])||isequal(checkbow(t,3:7),[0 0 1 0 0])
chessluozi(t)=6;
%成死2
elseif isequal(checkbow(t,3:7),[2 1 0 0 0])||isequal(checkbow(t,4:8),[2 1 0 0 0])||isequal(checkbow(t,2:6),[0 0 0 1 2])||isequal(checkbow(t,1:5),[0 0 0 1 2])
chessluozi(t)=7;
else
chessluozi(t)=8;
end
end
%确定进攻的棋型并存进攻最优棋型分数
%定义minjg为进攻时四个