clear
clc
global lx ly
lx=0;
ly=0;
pieces=zeros(19,19);%0:无子;1:黑子;2:白子
phase=0;
state=1;
playcolor=1;%玩家颜色:1黑2白
ai=0;%与AI博弈
figure('NumberTitle','off','Name','Go');
hold off
hold on
result=uicontrol('Style','edit','Position',[10,210,80,30]);
blackresult=uicontrol('Style','edit','Position',[10,290,80,30]);
whiteresult=uicontrol('Style','edit','Position',[10,250,80,30]);
uicontrol('string','数子','Position',[10,10,80,30],'callback','state=3;');
uicontrol('string','新局','Position',[10,170,80,30],'callback','state=2;');
uicontrol('string','人机对战','Position',[10,50,80,30],'callback','ai=1;');
uicontrol('string','双人对战','Position',[10,90,80,30],'callback','ai=0;');
uicontrol('string','人机换色','Position',[10,130,80,30],'callback','playcolor=3-playcolor;');
axis([0 20 0 20]);
axis square
grid on
set(gca,'XTick',1:19);
set(gca,'YTick',1:19);
board();
pause(0.1);%暂停0.1s让axis显示
while 1
switch state
case 1
if ai%人机博弈
if playcolor==1%玩家执黑
if mod(phase,2)%AI回合
if pieces(4,4)==0&&phase<10%抢占四点
[pieces,phase]=lay(pieces,4,4,phase,1);
elseif pieces(4,16)==0&&phase<10
[pieces,phase]=lay(pieces,4,16,phase,1);
elseif pieces(16,4)==0&&phase<10
[pieces,phase]=lay(pieces,16,4,phase,1);
elseif pieces(16,16)==0&&phase<10
[pieces,phase]=lay(pieces,16,16,phase,1);
end
if mod(phase,2)%AI回合
[x,y]=ailay(pieces,2);%寻找AI落子点
if x==0||y==0
state=0;
else
[pieces,phase]=lay(pieces,x,y,phase,1);
end
end
else%玩家回合
[x,y]=ginput(1);
x=round(x);
y=round(y);
if x>=1&&x<=19&&y>=1&&y<=19&&pieces(x,y)==0
[pieces,phase]=lay(pieces,x,y,phase,1);
pause(0.1);%暂停0.1s让axis显示
end
end
else%玩家执白
if mod(phase,2)%玩家回合
[x,y]=ginput(1);
x=round(x);
y=round(y);
if x>=1&&x<=19&&y>=1&&y<=19&&pieces(x,y)==0
[pieces,phase]=lay(pieces,x,y,phase,1);
pause(0.1);%暂停0.1s让axis显示
end
else%AI回合
if pieces(4,4)==0&&phase<10%抢占四点
[pieces,phase]=lay(pieces,4,4,phase,1);
elseif pieces(4,16)==0&&phase<10
[pieces,phase]=lay(pieces,4,16,phase,1);
elseif pieces(16,4)==0&&phase<10
[pieces,phase]=lay(pieces,16,4,phase,1);
elseif pieces(16,16)==0&&phase<10
[pieces,phase]=lay(pieces,16,16,phase,1);
end
if mod(phase,2)==0%AI回合
[x,y]=ailay(pieces,2);
if x==0||y==0
state=0;
else
[pieces,phase]=lay(pieces,x,y,phase,1);
end
end
end
end
else%两人博弈
[x,y]=ginput(1);
x=round(x);
y=round(y);
if x>=1&&x<=19&&y>=1&&y<=19&&pieces(x,y)==0
[pieces,phase]=lay(pieces,x,y,phase,1);
end
end
case 2
cla;
board();
pieces=zeros(19,19);%0:无子;1:黑子;2:白子
phase=0;
state=1;
pause(0.1);%暂停0.1s让axis显示
case 3
eyes=zeros(19,19);%目0:未遍历;1:黑目;2:白目;3:公共目
for a=1:19
for b=1:19
if pieces(a,b)==0&&eyes(a,b)==0
[~,eyes(a,b)]=eyejudge(pieces,zeros(19,19),a,b,0);
eyes=eyearound(eyes,pieces,a,b,eyes(a,b));%感染周围空地
end
end
end
black=0;
white=0;
for a=1:19
for b=1:19
switch pieces(a,b)
case 0
if eyes(a,b)==1
black=black+0.5;
draweye(a,b,1);
elseif eyes(a,b)==2
draweye(a,b,2);
white=white+0.5;
else
black=black+0.25;
white=white+0.25;
end
case 1
black=black+1;
case 2
white=white+1;
end
end
end
set(blackresult,'string',['黑棋',num2str(black,'%.2f'),'子']);
set(whiteresult,'string',['白棋',num2str(white,'%.2f'),'子']);
d=black-white-3.75;
if d>0
set(result,'string',['黑胜',num2str(d,'%.2f'),'子']);
else
set(result,'string',['白胜',num2str(-d,'%.2f'),'子']);
end
state=4;
artificialpieces=pieces;
case 4
[x,y]=ginput(1);
x=round(x);
y=round(y);
if x>=1&&x<=19&&y>=1&&y<=19
switch pieces(x,y)
case 0
if eyes(x,y)==1
eyes(x,y)=2;
black=black-0.5;
white=white+0.5;
draweye(x,y,2);
elseif eyes(x,y)==2
eyes(x,y)=3;
white=white-0.25;
black=black+0.25;
draweye(x,y,0);
else
eyes(x,y)=1;
black=black+0.25;
white=white-0.25;
draweye(x,y,1);
end
case 1
if artificialpieces(x,y)==1
[artificialpieces,count]=draweyes(artificialpieces,pieces,x,y,2,pieces(x,y),0);
black=black-count;
else
[artificialpieces,count]=draweyes(artificialpieces,pieces,x,y,1,pieces(x,y),0);
black=black+count;
end
case 2
if artificialpieces(x,y)==2
[artificialpieces,count]=draweyes(artificialpieces,pieces,x,y,1,pieces(x,y),0);
white=white-count;
else
[artificialpieces,count]=draweyes(artificialpieces,pieces,x,y,2,pieces(x,y),0);
white=white+count;
end
end
set(blackresult,'string',['黑棋',num2str(black,'%.2f'),'子']);
set(whiteresult,'string',['白棋',num2str(white,
评论2
最新资源