资源描述:
《matlab游戏编程实例(五子棋)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、这是单机版的五子棋,做为学习用的一个matlab代码,两个人轮流下,电脑自动判别胜负。此代码为realghost开发,如有转载,请保留这部分信息。若要交流,请与slqinyi@163.com联系functionfive()figure(1)axis([012012]);holdonaxisofffori=1:11%»ÆåÅÌline([111],[ii]);line([ii],[111]);endaxisequalqishou=1;%ÅжÏÆåÊÖµÄÑÕÉ«£¬¿ªÊ¼µÄʱºòÊǺìÉ«£
2、¬È¡0ʱÊǺÚÉ«boardstatus=zeros(10);%ÆåÅÌ״̬¾ØÕó£¬È¡0Ϊ¿Õ£¬È¡1Ϊºì£¬È¡2ΪºÚwhile1holdonposflag=1;%ÓÃÓÚÅжÏϵÄÆåÊÇ·ñÓÐЧ£¬È¡1˵Ã÷ϵÄÆåÎÞЧ£¬ÒªÖØÐÂÏÂwhileposflag[xpos,ypos]=ginput(1);xpos=0.5*(floor(xpos)+ceil(xpos));ypos=0.5*(floor(ypos)+ceil(ypos));if
3、xpos<=0.5
4、xpos>=11
5、ypos<=0.5
6、ypos>=11%µãµ½ÆåÅÌÍâÁËcontinueendrx=floor(xpos);ry=floor(ypos);ifboardstatus(rx,ry)==1
7、boardstatus(rx,ry)==2%¸ÃλÖÃÒѾÓÐÆå×Ócontinue;endposflag=0;endifqishou==1drawthego(xpos,ypos,qishou);boardstatus(rx,ry)=1;qishou=0;elsedr
8、awthego(xpos,ypos,qishou);boardstatus(rx,ry)=2;qishou=1;endifiswin(boardstatus,rx,ry)ifqishou==1winmsg='ºÚÆåÁ¬³ÉÁËÎå¿Å£¬ºÚʤ£¡';elsewinmsg='ºìÆåÁ¬³ÉÁËÎå¿Å£¬ºìʤ£¡';endmsgbox(winmsg)return%ºÍÆåµÄ״̬²»ÏëÅжÏÁË¡£¡£¡£¡£¡£¡£endaxis([012012]);axisequalendfi
9、gure(1)axis([010010]);axisequalfunctiondrawthego(x,y,flag)%ÔÚ(x,y)´¦»Ò»¸öÆå×Ó%flagÈ¡1ʱ»ºìÆå%flagÈ¡0ʱ»ºÚÆåt=0:pi/100:2*pi;rx=x+0.5*cos(t);ry=y+0.5*sin(t);ifflag==1fill(rx,ry,'r');elsefill(rx,ry,'k');endfunctionw=iswin(board,x,y)%ÅжÏÊÇ·ñÓ®Æåw=0;ifisem
10、pty(find(board)==0)w=2;%ÆåÅÌÉÏÏÂÂúÆåÁË£¬ÅÐΪºÍÆåreturn;endflag=board(x,y);%¸ÕϵÄÆå%ͳ¼ÆµÚyÁÐk=0;%ͳ¼ÆÁ¬ÔÚÒ»ÆðµÄÆåµÄ¸öÊýfori=x:10ifboard(i,y)~=flagbreakendk=k+1;endifk>=5w=1;returnendfori=x-1:-1:1ifboard(i,y)~=flagbreak;endk=k+1;endifk>=5w=1;returnend%
11、ͳ¼ÆµÚxÐÐk=0;fori=y:10ifboard(x,i)~=flagbreak;endk=k+1;endifk>=5w=1;returnendfori=y-1:-1:1ifboard(x,i)~=flagbreak;endk=k+1;endifk>=5w=1;returnend%ͳ¼ÆÖ÷¶Ô½ÇÏßk=0;fori=0:10ifx+i>10breakendify+i>10break;endifboard(x+i,y+i)~=flagbreak;endk=k+1;endifk>=5w=
12、1;returnendfori=1:10ifx-i<0break;endify-i<0break;endifboard(x-i,y-i)~=flagbreak;endk=k+1;endifk>=5w=1;returnend%ͳ¼Æ¸±¶Ô½ÇÏßk=0;fori=0:10ifx-i<0break;endify+i>10break;endifboard(x-i,y+i)~=flagbreak;endk=k+1;endifk>=5w=1;returnendfori=1: