您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > matlab游戏编程实例(五子棋)
这是单机版的五子棋,做为学习用的一个matlab代码,两个人轮流下,电脑自动判别胜负。此代码为realghost开发,如有转载,请保留这部分信息。若要交流,请与slqinyi@163.com联系functionfive()figure(1)axis([012012]);holdonaxisofffori=1:11%»ÆåÅÌline([111],[ii]);line([ii],[111]);endaxisequalqishou=1;%ÅжÏÆåÊÖµÄÑÕÉ«£¬¿ªÊ¼µÄʱºòÊǺìÉ«£¬È¡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));ifxpos=0.5|xpos=11|ypos=0.5|ypos=11%µãµ½ÆåÅÌÍâÁËcontinueendrx=floor(xpos);ry=floor(ypos);ifboardstatus(rx,ry)==1|boardstatus(rx,ry)==2%¸ÃλÖÃÒѾÓÐÆå×Ócontinue;endposflag=0;endifqishou==1drawthego(xpos,ypos,qishou);boardstatus(rx,ry)=1;qishou=0;elsedrawthego(xpos,ypos,qishou);boardstatus(rx,ry)=2;qishou=1;endifiswin(boardstatus,rx,ry)ifqishou==1winmsg='ºÚÆåÁ¬³ÉÁËÎå¿Å£¬ºÚʤ£¡';elsewinmsg='ºìÆåÁ¬³ÉÁËÎå¿Å£¬ºìʤ£¡';endmsgbox(winmsg)return%ºÍÆåµÄ״̬²»ÏëÅжÏÁË¡£¡£¡£¡£¡£¡£endaxis([012012]);axisequalendfigure(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;ifisempty(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%ͳ¼ÆµÚ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+i10breakendify+i10break;endifboard(x+i,y+i)~=flagbreak;endk=k+1;endifk=5w=1;returnendfori=1:10ifx-i0break;endify-i0break;endifboard(x-i,y-i)~=flagbreak;endk=k+1;endifk=5w=1;returnend%ͳ¼Æ¸±¶Ô½ÇÏßk=0;fori=0:10ifx-i0break;endify+i10break;endifboard(x-i,y+i)~=flagbreak;endk=k+1;endifk=5w=1;returnendfori=1:10ifx+i10break;endify-i0break;endifboard(x+i,y-i)~=flagbreak;endk=k+1;endifk=5w=1;returnend
本文标题:matlab游戏编程实例(五子棋)
链接地址:https://www.777doc.com/doc-4369332 .html