C#连连看-源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;//ArrayList命名空间namespace连连看{publicpartialclassForm1:Form{privateBitmapSource;//所有动物图案的图片privateintW=50;//动物方块图案的宽度privateintGameSize=10;//布局大小即行列数privateboolSelect_first=false;//是否已经选中第一块privateintx1,y1;//被选中第一块的地图坐标privateintx2,y2;//被选中第二块的地图坐标Pointz1,z2;//折点棋盘坐标privateintm_nCol=10;privateintm_nRow=10;privateint[]m_map=newint[10*10];privateintBLANK_STATE=-1;publicenumLinkType{LineType,OneCornerType,TwoCornerType};LinkTypeLType;//连通方式publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){Source=(Bitmap)Image.FromFile(..\\..\\res\\animal.bmp);this.pictureBox1.Height=W*(m_nRow+2);this.pictureBox1.Width=W*(m_nCol+2);this.pictureBox1.Top=0;this.pictureBox1.Left=0;//当前窗体标题栏高度intd=(this.Height-this.ClientRectangle.Height);this.Height=this.pictureBox1.Height+this.pictureBox1.Top+d;this.Width=this.pictureBox1.Width+this.pictureBox1.Left;//for(inti=0;i10*10;i++)//{//m_map[i]=i%6;//}StartNewGame();Init_Graphic();}privatevoidStartNewGame(){//初始化地图,将地图中所有方块区域位置置为空方块状态for(intiNum=0;iNum(m_nCol*m_nRow);iNum++){m_map[iNum]=BLANK_STATE;}Randomr=newRandom();//生成随机地图//将所有的动物物种放进一个临时的地图tmpMap中ArrayListtmpMap=newArrayList();for(inti=0;i(m_nCol*m_nRow)/4;i++)for(intj=0;j4;j++)tmpMap.Add(i);//每次从上面的临时地图tmpMap中取走(获取后并在临时地图删除)//一个动物放到地图的空方块上for(inti=0;im_nRow*m_nCol;i++){//随机挑选一个位置intnIndex=r.Next()%tmpMap.Count;//获取该选定物件放到地图的空方块m_map[i]=(int)tmpMap[nIndex];//在临时地图tmpMap除去该动物tmpMap.RemoveAt(nIndex);}}privatevoidInit_Graphic(){Graphicsg=get_Graphic();//生成Graphics对象for(inti=0;i10*10;i++){g.DrawImage(create_image(m_map[i]),W*(i%GameSize)+W,W*(i/GameSize)+W,W,W);}}privateGraphicsget_Graphic(){if(pictureBox1.Image==null){Bitmapbmp=newBitmap(pictureBox1.Width,pictureBox1.Height);pictureBox1.Image=bmp;}Graphicsg=Graphics.FromImage(pictureBox1.Image);returng;}publicGraphicsGetGraphicsObject(refPictureBoxpic){System.Drawing.Graphicsg;Bitmapbmp=newBitmap(pic.Width,pic.Height);pic.Image=bmp;g=Graphics.FromImage(bmp);returng;}//create_image()方法实现按标号n从所有动物图案的图片中截图。privateBitmapcreate_image(intn)//按标号n截图{Bitmapbit=newBitmap(W,W);Graphicsg=Graphics.FromImage(bit);//生成Graphics对象Rectanglea=newRectangle(0,0,W,W);Rectangleb=newRectangle(0,n*39,39,39);//截取原图中b矩形区域的图形g.DrawImage(Source,a,b,GraphicsUnit.Pixel);returnbit;}//////检测是否已经赢得了游戏///boolIsWin(){//检测所有是否尚有非未被消除的方块//(非BLANK_STATE状态)for(inti=0;im_nRow*m_nCol;i++){if(m_map[i]!=BLANK_STATE){returnfalse;}}returntrue;}privateboolIsSame(intx1,inty1,intx2,inty2){if(m_map[y1*m_nCol+x1]==m_map[y2*m_nCol+x2])returntrue;elsereturnfalse;}////X直接连通即垂直方向连通//boolX_Link(intx,inty1,inty2){//保证y1的值小于y2if(y1y2){//数据交换intn=y1;y1=y2;y2=n;}//直通for(inti=y1+1;i=y2;i++){if(i==y2)returntrue;if(m_map[i*m_nCol+x]!=BLANK_STATE)break;}returnfalse;}////Y直接连通即水平方向连通//boolY_Link(intx1,intx2,inty){if(x1x2){intx=x1;x1=x2;x2=x;}//直通for(inti=x1+1;i=x2;i++){if(i==x2)returntrue;if(m_map[y*m_nCol+i]!=BLANK_STATE)break;}returnfalse;}////1直角接口连通//boolOneCornerLink(intx1,inty1,intx2,inty2){if(x1x2)//目标点(x1,y1),(x2,y2)两点交换{intn=x1;x1=x2;x2=n;n=y1;y1=y2;y2=n;}if(y2y1)//(x1,y1)为矩形左下顶点,(x2,y2)点为矩形右上顶点{//判断矩形右下角折点(x2,y1)是否空if(m_map[y1*m_nCol+x2]==BLANK_STATE){if(Y_Link(x1,x2,y1)&&X_Link(x2,y1,y2))//判断折点(x2,y1)与两个目标点是否直通{z1.X=x2;z1.Y=y1;//保存折点坐标到z1returntrue;}}//判断矩形左上角折点(x1,y2)是否空if(m_map[y2*m_nCol+x1]==BLANK_STATE){if(Y_Link(x2,x1,y2)&&X_Link(x1,y2,y1))//判断折点(x1,y2)与两个目标点是否直通{z1.X=x1;z1.Y=y2;//保存折点坐标到z1returntrue;}}returnfalse;}else//(x1,y1)为矩形左上顶点,(x2,y2)点为矩形右下顶点{//判断矩形左下角折点(x1,y2)是否空if(m_map[y2*m_nCol+x1]==BLANK_STATE){if(Y_Link(x1,x2,y2)&&X_Link(x1,y1,y2))//判断折点(x1,y2)与两个目标点是否直通{z1.X=x1;z1.Y=y2;//保存折点坐标到z1returntrue;}}//判断矩形右上角折点(x2,y1)是否空if(m_map[y1*m_nCol+x2]==BLANK_STATE){if(Y_Link(x1,x2,y1)&&X_Link(x2,y1,y2))//判断折点(x2,y1)与两个目标点是否直通{z1.X=x2;z1.Y=y1;//保存折点坐标到z1returntrue;}}returnfalse;}}//////2折点连通///boolTwoCornerLink(intx1,inty1,intx2,inty2){if(x1x2){intn=x1;x1=x2;x2=n;n=y1;y1=y2;y2=n;}//右intx,y;for(x=x1+1;x=m_nCol;x++){if(x==m_nCol)//两个折点在选中方块的右侧,且两个折点在图案区域之外if(XThrough(x2+1,y2,true))//Y_Link(x2+1,m_nCol-1,y2)&&m_map[y1*(m_nCol-1)+x]==BLANK_STATE{z2.X=m_nCol;z2.Y=y1;z1.X=m_nCol;z1.Y=y2;returntrue;}elsebreak;if(m_map[y1*m_nCol+x]!=BLANK_STATE)break;if(OneCornerLink(x,y1,x2,y2)){z2.X=x;z2.Y=y1;returntrue;}}//左for(x=x1-1;x=-1;x--){if(x==-1)//两个折点在选中方块的左侧,且两个折点在图案区域之外if(XThrough(x2-1,y2,false)){z2.X=-1;z2.Y=y1;z1.X=-1;z1.Y=y2;returntrue;}elsebreak;if(m_map[y1*m_nCol+x]!=BLANK_STATE)break;if(OneCornerLink(x,y1,x2,y2)){z2.X=x;z2.Y=y1;returntrue;}}//上for(y=y1-1;y=-1;y--){if(y==-1)//两个折点在选中方块的上侧,且两个折点在图案区域之外if(YThrough(x2,y2-1,false)){z2.X=x1;z2.Y=-1;z1.X=x2;z1.Y=-1;returntrue;}elsebreak;if(m_map[y*m_nCol+x1]!=BLANK_STATE)break;if(OneCornerLink(x1,y,x2,y2)){z2.X=x1;z2.Y=y;returntrue;}}//下for(y=y1+1;y=m_nRow;y++){if(y==m_nRow)//两个折点在选中方块的下侧,且两个折点在图案区域之外if(YThrough(x2,y2+1,true)){z2.X=x1;z2.Y=m_nRow;z1.X=x2;z1.Y=m_nRow;returntr

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功