简单解释逻辑类程序
%initialize
clear
clc
clf
B=zeros(3,3);
C=zeros(3,3);
% circling
% chess board
plot([1.5,1.5],[0.5,3.5],'b')
hold on
plot([2.5,2.5],[0.5,3.5],'b')
hold on
plot([0.5,3.5],[1.5,1.5],'b')
hold on
plot([0.5,3.5],[2.5,2.5],'b')
hold on
while 1
% justice legality
while 2
% get position
a = round(ginput(1));
x = a(1,1);
y = a(1,2);
if B(x,y) == 0
scatter(x,y,500,'x','r')
hold on
B(x,y) = 1;
break
a = round(ginput(1));
x = a(1,1);
y = a(1,2);
else
end
end
%justice win
if B(1,1) == 1 && B(1,2) == 1 && B(1,3) == 1
msgbox('赢了!', 'chess');
break
end
if B(2,1) == 1 && B(2,2) == 1 && B(2,3) == 1
msgbox('赢了!', 'chess');
break
end
if B(3,1) == 1 && B(3,2) == 1 && B(3,3) == 1
msgbox('赢了!', 'chess');
break
end
if B(1,1) == 1 && B(2,1) == 1 && B(3,1) == 1
msgbox('赢了!', 'chess');
break
end
if B(1,2) == 1 && B(2,2) == 1 && B(3,2) == 1
msgbox('赢了!', 'chess');
break
end
if B(1,3) == 1 && B(2,3) == 1 && B(3,3) == 1
msgbox('赢了!', 'chess');
break
end
if B(1,1) == 1 && B(2,2) == 1 && B(3,2) == 1
msgbox('赢了!', 'chess');
break
end
p = 0;
while 3
%AI time
pause(1)
%thinking win
d1=1;d2=1;
a = [d1,d2];
while 4
C = B;
if B(d1,d2) == 0
C(d1,d2) = -1;
if sum(C(d1,1:3)==-1)==3 | sum(C(1:3,d2)==-1)==3
p = 1;
break
end
if a == [1,1] | a == [2,2] | a == [3,3]
if sum(diag(C)==-1) == 3
p = 1;
break
end
end
if a == [1,3] | a == [2,2] | a == [3,1]
if sum(diag(C(1:end,end:-1:1))==-1) == 3
p = 1;
break
end
end
else
end
if d1 == 3 % next point
if d2 == 3
break
else
d1 = 1;
d2 = d2+1;
d1 = d1+1;
end
else
end
end
if p == 1
break
else
%thinking loose
d1=1;d2=1;
a = [d1,d2];
p = 0;
while 5
C = B;
if B(d1,d2) == 0
C(d1,d2) = 1;
if sum(C(d1,1:3)==1)==3 | sum(C(1:3,d2)==1)==3
end
if a == [1,1] | a == [2,2] | a == [3,3]
if sum(diag(C)==1) == 3
p = 1;
break
p = 1;
break
end
end
if a == [1,3] | a == [2,2] | a == [3,1]
if sum(diag(C(1:end,end:-1:1))==1) == 3
p = 1;
break
end
end
else
end
if d1 == 3 % next point
if d2 == 3
break
else
d1 = 1;
d2 = d2+1;
end
else
end
d1 = d1+1;
if B(d1,d2) == 0
break
else
d1 = round(2*rand(1))+1;
d2 = round(2*rand(1))+1;
a = [d1,d2];
end
end
if p == 0
while 6
end
end
break
break
else
end
end
B(d1,d2) = -1;
scatter(d1,d2,500,'g')
hold on
%justice lose
if B(1,1) == -1 && B(1,2) == -1 && B(1,3) == -1
msgbox('输了!', 'chess');
break
end
if B(2,1) == -1 && B(2,2) == -1 && B(2,3) == -1
msgbox('输了!', 'chess');
break
end
if B(3,1) == -1 && B(3,2) == -1 && B(3,3) == -1
msgbox('输了!', 'chess');
break
end
if B(1,1) == -1 && B(2,1) == -1 && B(3,1) == -1
msgbox('输了!', 'chess');
break
end
if B(1,2) == -1 && B(2,2) == -1 && B(3,2) == -1
msgbox('输了!', 'chess');
break
end
if B(1,3) == -1 && B(2,3) == -1 && B(3,3) == -1
msgbox('输了!', 'chess');
break
end
if B(1,1) == -1 && B(2,2) == -1 && B(3,2) == -1
msgbox('输了!', 'chess');
break
end
end