function connectfour(varargin)
global mode;
mode=varargin{1};
if nargin==1,
handles.fig=openfig('connectfour.fig');
set(handles.fig,'WindowButtonDownFcn',[mfilename '(''boardclick'',guidata(gcbo))'])
set(get(handles.fig,'children'),'visible','on')
handles=guihandles(handles.fig);
handles.turn=1;
handles.board=zeros(6,7);
handles.snapshot=handles.board;
drawboard(handles)
drawboard1(handles)
guidata(handles.ax,handles)
else
feval(varargin{:});
end
end
function w=checkifend(handles)
[r,c]=size(handles.board);
[w,ind]=check4win(handles.board);
if w,
if w<3,
set(handles.message,'string',sprintf('玩家 %d win!',w))
plot(ind(2,:)-0.5,r-ind(1,:)+0.5,'k','linewidth',3)
untitled;
else
set(handles.message,'string',sprintf('It''s a draw!',w))
end
set(handles.fig,'windowbuttondownfcn','')
end
end
function resetfcn(handles)
handles.board=zeros(6,7);
handles.snapshot=handles.board;
handles.turn=1;
set(handles.fig,'WindowButtonDownFcn',[mfilename '(''boardclick'',guidata(gcbo))'])
set(handles.message,'string','四子棋')%Player 1 choose a column.
drawboard(handles)
guidata(handles.fig,handles)
end
function boardclick(handles) %玩家A
set(handles.fig,'WindowButtonDownFcn','')
point=get(handles.ax,'currentpoint');
point=point(1,1:2);
board=handles.board;
[r,c]=size(board);
if getMode()==0
switch (get(gcbf,'SelectionType'))
case 'normal'
try
if handles.turn==1
if point(1)>0 & point(1)<c & point(2)>0 & point(2)<r,
point=floor(point)+1;
handles.turn=1;
[handles.board,result]=makemove(handles.board,1,point(1));
if result, %若click的"行"還沒被放滿
handles=addpeice(handles);
if ~checkifend(handles), %如果還沒有輸贏
handles.turn=2;
checkifend(handles);
end
end
end
else
msgbox('現在輪到黃色玩家囉!')
end
catch
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 'alt'
try
if handles.turn==2
if point(1)>0 & point(1)<c & point(2)>0 & point(2)<r,
point=floor(point)+1;
[handles.board,result]=makemove(handles.board,2,point(1));
if result, %若click的"行"還沒被放滿
handles=addpeice(handles);
if ~checkifend(handles), %如果還沒有輸贏
handles.turn=1;
checkifend(handles);
end
end
end
else
msgbox('現在輪到紅色玩家囉!')
end
catch
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
else
try
if point(1)>0 & point(1)<c & point(2)>0 & point(2)<r,
point=floor(point)+1;
handles.turn=1;
[handles.board,result]=makemove(handles.board,1,point(1));
if result,
handles=addpeice(handles);
handles.turn=2;
if ~checkifend(handles),
handles=halzmove(handles);
handles=addpeice(handles);
checkifend(handles);
end
end
end
catch
end
end
guidata(handles.fig,handles)
set(handles.fig,'WindowButtonDownFcn',[mfilename '(''boardclick'',guidata(gcbo))'])
end
function score=scoreboard(board,turn,offense)
oponent=2/turn;
if offense,
score=maxinarow(board,turn);
else
score=1/maxinarow(board,oponent);
end
for j=1:size(board,2),
nextBoard=makemove(board,oponent,j);
w=check4win(nextBoard);
if w==oponent,score=-1;end
end
end
function drawboard(handles)
board=handles.board;
[r,c]=size(board);
axes(handles.ax),cla,hold on
radius=0.35;
t=-pi/2:0.01:pi/2;
x1=[0 0 radius.*cos(t) 0 0 0.5 0.5 0];
y1=[-0.5 -radius radius.*sin(t) radius 0.5 0.5 -0.5 -0.5];
x2=[0 0 -radius.*cos(t) 0 0 -0.5 -0.5 0];
for j=1:c,
for k=1:r,
patch(x1+j-0.5,y1+r-k+0.5,'b','edgecolor','none'),axis equal
patch(x2+j-0.5,y1+r-k+0.5,'b','edgecolor','none')
end
end
axis([0 c 0 r],'equal','off')
end
function drawboard1(handles)
backgroundImage = importdata('1.jpg');
axes(handles.axes1);
image(backgroundImage);
axis off
end
function handles=addpeice(handles)
board=handles.board;
[r,c]=size(board);
axes(handles.ax),hold on
radius=0.35;
t=0:0.01:2*pi;
x=radius.*cos(t);
y=radius.*sin(t);
[m,n]=find(board~=handles.snapshot);
colors=[1 0 0;1 1 0];
start=r+1;
finish=r-m+0.5;
step=0.5;
h=patch(x+n-0.5,start,colors(board(m,n),:));
kids=get(handles.ax,'children');
set(handles.ax,'children',[kids(2:end);kids(1)])
fall=start;
while fall>finish & ishandle(h),fall=fall-step;set(h,'ydata',y+fall),drawnow,end
s=get(handles.fig,'userdata');
sound(s.y2,s.fs)
handles.snapshot=board;
end
function n = maxinarow(board,player)
w=check4win(board);
if w==player,n=4;
else
n=0;
[r,c]=find(board==player);
horz_step=[-1 0 1 1 1 0 -1 -1];
vert_step=[1 1 1 0 -1 -1 -1 0];
n=0;
for j=1:length(r),
for k=1:8,
if r(j)-3*vert_step(k)>0 & r(j)-3*vert_step(k)<=size(board,1) & c(j)+3*horz_step(k)>0 && c(j)+3*horz_step(k)<=size(board,2),
if vert_step(k)~=0,
checkrows=r(j):-vert_step(k):r(j)-3*vert_step(k);
else
checkrows=r(j).*ones(1,4);
end
if horz_step(k)~=0,
checkcols=c(j):horz_step(k):c(j)+3*horz_step(k);
else
checkcols=c(j).*ones(1,4);
end
for m=1:4,
group(m)=board(checkrows(m),checkcols(m));
end
if ~isempty(find(group==2/player))
temp=0;
else
peices=find(group==player);
switch length(peices)
case 1
temp=1;
case 2
if diff(peices)==1,
temp=2;
else
temp=1;
end
case 3
if group(1)==player & group(4)==player,
temp=2;
else
temp=3;
end
end
end
n=n+10^temp;
end
end
end
end
end
function [w,ind] = check4win(b)
[w,ind]=checkplayer(b,1);
if w==0,[w,ind]=checkplayer(b,2); end
if isempty(find(b==0)) & w==0,w=3;end
end
function [w,ind]=checkplayer(board,player)
[r,c]=find(board==player);
horz_step=[-1 0 1 1 1 0 -1 -1];
vert_step=[1 1 1 0 -1 -1 -1 0];
w=0;
ind=[];
for j=1:length(r),
for k=1:8,
if r(j)-3*vert_step(k)>0 & r(j)-3*vert_step(k)<=size(board,1) & c(j)+3*horz_step(k)>0 & c(j)+3*horz_step(k)<=size(board,2),
if vert_step(k)~=0,
checkrows=r(j):-vert_step(k):r(j)-3*vert_step(k);
else
checkrows=r(j).*ones(1,4);
end
if horz_step(k)~=0,
checkcols=c(j):horz_step(k):c(j)+3*horz_step(k);
else
checkcols=c(j).*ones(1,4);
end
for m=1:4,
group(m)=board(checkrows(m),checkcols(m));
end
if sum(abs(group-player))==0,
ind=[checkrows;checkcols];
w=player;
return
end
end
end
end
end
function [b,result] = makemove(b,player,col)
if b(1,col)~=0,
result=0;
return
end
for j=1:size(b,1),
if b(j,col)~=0, j=j-1; break, end
end
b(j,col)=player;
result=1;
end
function Info_Callback(hObject, eventdata, handles)
msgbox(['1.雙方必須輪流把一枚己棋投入開口,讓棋子因地心引力落下在底部或其他棋子上。',char(10),'2.當己方四枚棋子以縱、橫、斜方向連成一