资源描述:
《双人对战版五子棋Matlab程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、functionfivechess1()figure('color',[0.80.650.5])axis([016016]);holdonfori=1:15line([115],[ii],'Color','k');line([ii],[115],'Color','k');endaxistightaxisequalaxisoffqishou=0;boardstatus=zeros(15);while1holdonposflag=1;whileposflag[xpos,ypos]=ginput(1);ifxpos>=0.5*(floor(xpos)
2、+ceil(xpos))xpos=ceil(xpos);elseifxpos<0.5*(floor(xpos)+ceil(xpos))xpos=floor(xpos);elsecontinueendifypos>=0.5*(floor(ypos)+ceil(ypos))ypos=ceil(ypos);elseifypos<0.5*(floor(ypos)+ceil(ypos))ypos=floor(ypos);elsecontinueendifxpos<=0.5
3、
4、xpos>15
5、
6、ypos<=0.5
7、
8、ypos>15continueendrx
9、=xpos;ry=ypos;ifboardstatus(rx,ry)==1
10、
11、boardstatus(rx,ry)==2continue;endposflag=0;endifqishou==0drawthego(xpos,ypos,qishou);boardstatus(rx,ry)=1;qishou=1;elsedrawthego(xpos,ypos,qishou);boardstatus(rx,ry)=2;qishou=0;endifiswin(boardstatus,rx,ry)==1ifqishou==1winmsg='blackwin
12、';elsewinmsg='whitewin';endmsgbox(winmsg)returnendifiswin(boardstatus,rx,ry)==2winmsg='equal';msgbox(winmsg)returnendaxis([012012]);axisequalaxistightendfigure(1)axis([010010]);axisequalfunctiondrawthego(x,y,flag)t=0:pi/100:2*pi;rx=x+0.42*cos(t);ry=y+0.42*sin(t);ifflag==1fil
13、l(rx,ry,'w');elsefill(rx,ry,'k');endfunctionw=iswin(board,x,y)w=0;flag=board(x,y);k=0;fori=x:15ifboard(i,y)~=flagbreakendk=k+1;endifk>=5w=1;returnendfori=x-1:-1:1ifboard(i,y)~=flagbreak;endk=k+1;endifk>=5w=1;returnendk=0;fori=y:15ifboard(x,i)~=flagbreak;endk=k+1;endifk>=5w=1
14、;returnendfori=y-1:-1:1ifboard(x,i)~=flagbreak;endk=k+1;endifk>=5w=1;returnendk=0;fori=0:15ifx+i>15breakendify+i>15break;endifboard(x+i,y+i)~=flagbreak;endk=k+1;endifk>=5w=1;returnendfori=1:15ifx-i<=0break;endify-i<=0break;endifboard(x-i,y-i)~=flagbreak;endk=k+1;endifk>=5w=1
15、;returnendk=0;fori=0:15ifx-i<=0break;endify+i>15break;endifboard(x-i,y+i)~=flagbreak;endk=k+1;endifk>=5w=1;returnendfori=1:15ifx+i>15break;endify-i<=0break;endifboard(x+i,y-i)~=flagbreak;endk=k+1;endifk>=5w=1;returnendifmin(min(board))~=0w=2;returnend