您好,欢迎访问三七文档
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;usingSystem.Threading;namespaceÎå×ÓÆå{publicpartialclassForm1:Form{intindex=0;privateint[,]virtualGobangBoard=newint[15,15];//ÐéÄâÆåÅÌprivatePictureBox[,]gobangPictureBox=newPictureBox[15,15];//Æå×ÓprivatePointlastMovePoint=newPoint(-1,-1);privateboolblnBegin;privateconstintblack=-1,white=1,background=0;privateintpersonGobangColor,computerGobangColor;privateinttotalGobangCount=0;privateStackbackStack=newStack();//ÓÃÓÚ»ÚÆåµÄÕ»privateStackbackTrackStack=newStack();//ÓÃÓÚ»ØËݵÄÕ»constintM=1;//Ô¤²âµÄ²½ÊýpublicForm1(){InitializeComponent();gobangBoardGroupBox.Paint+=newPaintEventHandler(gobangBoardGroupBox_Paint);InitializeGobangBoard();gobangBoardGroupBox.MouseMove+=newMouseEventHandler(gobangBoardGroupBox_MouseMove);this.MouseMove+=newMouseEventHandler(Form1_MouseMove);gobangBoardGroupBox.MouseClick+=newMouseEventHandler(gobangBoardGroupBox_MouseClick);this.Text=Îå×ÓÆå;}privatevoidInitializeGobangBoard()//³õʼ»¯ÆåÅÌ{gobangBoardGroupBox.Paint+=newPaintEventHandler(gobangBoardGroupBox_Paint);intx,y;for(x=0;x15;x++)for(y=0;y15;y++){gobangPictureBox[x,y]=newPictureBox();gobangPictureBox[x,y].Location=newPoint(10+x*40,10+y*40);gobangPictureBox[x,y].Size=newSize(40,40);gobangPictureBox[x,y].BackColor=Color.Transparent;gobangPictureBox[x,y].SizeMode=PictureBoxSizeMode.CenterImage;gobangPictureBox[x,y].Visible=false;gobangBoardGroupBox.Controls.Add(gobangPictureBox[x,y]);}}privatevoidgobangBoardGroupBox_Paint(objectsender,PaintEventArgse){inti;Graphicsgr=e.Graphics;PenmyPen=newPen(Color.Black,2);SolidBrushbrush=newSolidBrush(Color.Red);for(i=0;i15;i++){gr.DrawLine(myPen,30+i*40,30,30+i*40,590);gr.DrawLine(myPen,30,30+i*40,590,30+i*40);}gr.FillEllipse(brush,306,306,8,8);gr.FillEllipse(brush,147,147,6,6);gr.FillEllipse(brush,467,147,6,6);gr.FillEllipse(brush,147,467,6,6);gr.FillEllipse(brush,467,467,6,6);}//»ÆåÅ̸ñprivatevoidgobangBoardGroupBox_MouseClick(objectsender,MouseEventArgse){intx,y;if(blnBegin){x=(e.X-10)/40;y=(e.Y-10)/40;PutAGobang(personGobangColor,x,y);if(Forbiden(personGobangColor,x,y)){MessageBox.Show(ÄãÊäÁË£¡±¾µãΪ½ûÊֵ㣡,±¾¾Ö½á¹û,MessageBoxButtons.OK,MessageBoxIcon.Information);blnBegin=false;personRadioButton.Enabled=true;computerRadioButton.Enabled=true;startButton.Enabled=true;}elseif(Win(personGobangColor,x,y)){MessageBox.Show(¹§Ï²Ä㣬ÄãÓ®ÁË£¡,±¾¾Ö½á¹û,MessageBoxButtons.OK,MessageBoxIcon.Information);blnBegin=false;personRadioButton.Enabled=true;computerRadioButton.Enabled=true;startButton.Enabled=true;}elseif(totalGobangCount==225){MessageBox.Show(ÆåÅÌÒÑÂú£¬±¾¾ÖƽÆ壡,±¾¾Ö½á¹û,MessageBoxButtons.OK,MessageBoxIcon.Information);blnBegin=false;personRadioButton.Enabled=true;computerRadioButton.Enabled=true;startButton.Enabled=true;}else{PointbestPoint=newPoint();if(FindBestPoint(refbestPoint)){PutAGobang(computerGobangColor,bestPoint);if(Win(computerGobangColor,bestPoint)){MessageBox.Show(ÄãÊäÁË£¡¼ÓÓÍŶ£¡,±¾¾Ö½á¹û,MessageBoxButtons.OK,MessageBoxIcon.Information);blnBegin=false;personRadioButton.Enabled=true;computerRadioButton.Enabled=true;startButton.Enabled=true;}elseif(totalGobangCount==225){MessageBox.Show(ÆåÅÌÒÑÂú£¬±¾¾ÖƽÆ壡,±¾¾Ö½á¹û,MessageBoxButtons.OK,MessageBoxIcon.Information);blnBegin=false;personRadioButton.Enabled=true;computerRadioButton.Enabled=true;startButton.Enabled=true;}}else{MessageBox.Show(¹§Ï²Ä㣬ÄãÓ®ÁË£¡ÆåÅÌÉÏËùÓеã¾ùΪµçÄԵĽûÊֵ㣡,±¾¾Ö½á¹û,MessageBoxButtons.OK,MessageBoxIcon.Information);blnBegin=false;personRadioButton.Enabled=true;computerRadioButton.Enabled=true;startButton.Enabled=true;}}}}privateboolForbiden(intgobangColor,intx,inty){if(gobangColor==white)returnfalse;else{inttemp=virtualGobangBoard[x,y];boolblntemp;virtualGobangBoard[x,y]=background;blntemp=(GetGobangPower(black,x,y)==-1);virtualGobangBoard[x,y]=temp;returnblntemp;}}privatevoidPutAGobang(intgobangColor,Pointpoint)//·ÅÒ»¸ögobangColorÉ«µÄÆåÔÚbestPointÉÏ{PutAGobang(gobangColor,point.X,point.Y);}privatevoidPutAGobang(intgobangColor,intx,inty)//·ÅÒ»¸ögobangColorÉ«µÄÆåÔÚ(x,y)ÉÏ{PointtempPoint=newPoint();if(gobangColor==black){gobangPictureBox[x,y].BackgroundImage=global::Îå×ÓÆå.Properties.Resources.blackstone;virtualGobangBoard[x,y]=black;gobangPictureBox[x,y].Image=global::Îå×ÓÆå.Properties.Resources.lastblackstone;if(backStack.Count0){tempPoint=(Point)backStack.Pop();gobangPictureBox[tempPoint.X,tempPoint.Y].Image=global::Îå×ÓÆå.Properties.Resources.nullll;backStack.Push(tempPoint);}}else{gobangPictureBox[x,y].BackgroundImage=global::Îå×ÓÆå.Properties.Resources.whitestone;virtualGobangBoard[x,y]=white;gobangPictureBox[x,y].Image=global::Îå×ÓÆå.Properties.Resources.lastwhitestone;if(backStack.Count0){tempPoint=(Point)backStack.Pop();gobangPictureBox[tempPoint.X,tempPoint.Y].Image=global::Îå×ÓÆå.Properties.Resources.nullll;backStack.Push(tempPoint);}}tempPoint=newPoint(x,y);backStack.Push(tempPoint);gobangPictureBox[x,y].Visible=t
本文标题:C# 五子棋源代码
链接地址:https://www.777doc.com/doc-4427908 .html