matlab游戏编程实例(五子棋)
这是单机版的五子棋,做为学习用的一个matlab代码,两个人轮流下,电脑自动判别胜负。
此代码为realghost开发,如有转载,请保留这部分信息。
若要交流,请与 slqinyi@163.com 联系
function five()
figure(1)
axis([0 12 0 12]);
hold on
axis off
for i = 1:11%?-???ì
line([1 11],[i i]);
line([i i],[1 11]);
end
axis equal
qishou = 1; % ?D????ê?μ???é?£??aê?μ?ê±oòê?oìé?£?è?0ê±ê?oúé?
boardstatus = zeros(10);% ???ì×′ì????ó£?è?0?a??£?è?1?aoì£?è?2?aoú
while 1
hold on
posflag = 1; % ó?óú?D????μ???ê?·?óDD§£?è?1?μ?÷??μ????TD§£?òa??D???
while posflag
[xpos,ypos] = ginput(1);
xpos = 0.5*(floor(xpos)+ceil(xpos));
ypos = 0.5*(floor(ypos)+ceil(ypos));
if xpos<=0.5|xpos>=11|ypos<=0.5|ypos>=11% μ?μ????ìíaá?
continue
end
rx = floor(xpos);
ry = floor(ypos);
if boardstatus(rx,ry)==1|boardstatus(rx,ry)==2%??????ò??-óD??×ó
continue;
end
posflag = 0;
end
if qishou==1
drawthego(xpos,ypos,qishou);
boardstatus(rx,ry)=1;
qishou = 0;
else
drawthego(xpos,ypos,qishou);
boardstatus(rx,ry)=2;
qishou = 1;
end
if iswin(boardstatus,rx,ry)
if qishou==1
winmsg = 'oú??á?3éá?????£?oúꤣ?';
else
winmsg = 'oì??á?3éá?????£?oìꤣ?';
end
msgbox(winmsg)
return
% oí??μ?×′ì?2????D??á??£?£?£?£?£?£
end
axis([0 12 0 12]);
axis equal
end
figure(1)
axis([0 10 0 10]);
axis equal
function drawthego(x,y,flag)
% ?ú(x,y)′|?-ò?????×ó
% flagè?1ê±?-oì??
% flagè?0ê±?-oú??
t = 0:pi/100:2*pi;
rx = x+0.5*cos(t);
ry = y+0.5*sin(t);
if flag==1
fill(rx,ry,'r');
else
fill(rx,ry,'k');
end
function w = iswin(board,x,y)
% ?D??ê?·?ó???
w = 0;
if isempty(find(board)==0)
w=2;%???ìé????ú??á?£??D?aoí??
return;
end
flag = board(x,y);% ????μ???
%í3??μúyáD
k = 0; % í3??á??úò??eμ???μ???êy
for i = x:10
if board(i,y)~=flag
break
end
k = k+1;
end
if k>=5
w=1;
return
end
for i