matlab编制游戏,matlab游戏编程实例(五子棋).doc

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值