五子棋Word下载.docx
《五子棋Word下载.docx》由会员分享,可在线阅读,更多相关《五子棋Word下载.docx(35页珍藏版)》请在冰豆网上搜索。
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()//初始化棋盘
intx,y;
for(x=0;
x<
15;
x++)
for(y=0;
y<
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;
i<
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)
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))
恭喜你,你赢了!
elseif(totalGobangCount==225)
棋盘已满,本局平棋!
else
PointbestPoint=newPoint();
if(FindBestPoint(refbestPoint))
PutAGobang(computerGobangColor,bestPoint);
if(Win(computerGobangColor,bestPoint))
加油哦!
棋盘上所有点均为电脑的禁手点!
privateboolForbiden(intgobangColor,intx,inty)
if(gobangColor==white)
returnfalse;
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:
:
MyChess.Properties.Resources.blackstone;
virtualGobangBoard[x,y]=black;
gobangPictureBox[x,y].Image=global:
MyChess.Properties.Resources.lastblackstone;
if(backStack.Count>
0)
tempPoint=(Point)backStack.Pop();
gobangPictureBox[tempPoint.X,tempPoint.Y].Image=global:
MyChess.Properties.Resources.nullll;
backStack.Push(tempPoint);
MyChess.Properties.Resources.whitestone;
virtualGobangBoard[x,y]=white;
MyChess.Properties.Resources.lastwhitestone;
tempPoint=newPoint(x,y);
gobangPictureBox[x,y].Visible=true;
privateintConnectGobangsCount(intgobangColor,Pointpoint1,Pointpoint2)
//求point1与point2之间可能形成五连子的gobangColor色棋的连子数(包括活棋)
intx,y,i,j,length,xPlus=0,yPlus=0,sum,maxSum=0;
length=Math.Max(Math.Abs(point1.X-point2.X),Math.Abs(point1.Y-point2.Y))+1;
if(point1.X!
=point2.X)xPlus=1;
if(point1.Y!
=point2.Y)yPlus=(point2.Y-point1.Y)/Math.Abs(point2.Y-point1.Y);
length-4;
x=point1.X+i*xPlus;
y=point1.Y+i*yPlus;
sum=0;
for(j=0;
j<
5;
j++)
if(virtualGobangBoard[x+j*xPlus,y+j*yPlus]==gobangColor)
sum++;
elseif(virtualGobangBoard[x+j*xPlus,y+j*yPlus]==-gobangColor)
break;
if(maxSum<
sum)
maxSum=sum;
returnmaxSum;
}//求point1与point2之间可能形成五连子的gobangColor色棋的连子数(包括活棋)
privateboollenthConnect(intx,inty)
{//判断在(x,y)点放一黑棋后是否有长连禁手
Pointleft,right,top,down,leftTop,rightTop,leftDown,rightDown;
inttemp;
inttempppppp=virtualGobangBoard[x,y];
left=newPoint(Math.Max(0,x-5),y);
right=newPoint(Math.Min(14,x+5),y);
top=newPoint(x,Math.Max(0,y-5));
down=newPoint(x,Math.Min(14,y+5));
temp=Math.Min(x-left.X,y-top.Y);
leftTop=newPoint(x-temp,y-temp);
temp=Math.Min(x-left.X,down.Y-y);
leftDown=newPoint(x-temp,y+temp);
temp=Math.Min(right.X-x,y-top.Y);
rightTop=newPoint(x+temp,y-temp);
temp=Math.Min(right.X-x,down.Y-y);
rightDown=newPoint(x+temp,y+temp);
blntemp=(lenthConnectTowPoint(left,right)||lenthConnectTowPoint(top,down)||lenthConnectTowPoint(leftTop,rightDown)||lenthConnectTowPoint(leftDown,rightTop));
virtualGobangBoard[x,y]=tempppppp;
}//判断在(x,y)点放一黑棋后是否有长连禁手
privateboollenthConnectTowPoint(Pointpoint1,Pointpoint2)
//求point1与point2之间是否能形成长连禁手
intx,y,i,j,length,xPlus=0,yPlus=0,sum;
length-5;
6;
if(virtualGobangBoard[x+j*xPlus,y+j*yPlus]==black)
if(sum==6)returntrue;
privateboolActiveConnectGobangs(intgobangColor,intcount,Pointpoint1,Pointpoint2)
{//判断point1与point2之间是否有gobangColor色的活count
inttemp1,temp2;
temp1=Math.Min(Math.Min(Math.Min(5-count,point1.X),point1.Y),14-point1.Y);
temp2=Math.Min(Math.Min(Math.Min(5-count,14-point2.X),14-point2.Y),point2.Y);
length=Math.Max(Math.Abs(point1.X-point2.X),Math.Abs(point1.Y-point2.Y))+1+temp1+temp2;
x=point1.X-temp1*xPlus+i*xPlus;
y=point1.Y-temp1*yPlus+i*yPlus;
if(x+4*xPlus>
14||y+4*yPlus>
14)
4;
if(0<
x&
&
0<
=y-yPlus&
y-yPlus<
=14)
if(sum==count&
virtualGobangBoard[x-xPlus,y-yPlus]==background&
virtualGobangBoard[x+4*xPlus,y+4*yPlus]==background)
returntrue;
}//判断point1与point2之间是否有gobangColor色的活count
privateboolBreakActiveConnectGobangs(intgobangColor,intcount,intx,inty,Pointpoint1,Pointpoint2)
{//(x,y)处放gobangColor色棋后形成活count,且放一反色棋后破坏棋形成活count的能,注意返回时不能破坏棋盘
if(!
ActiveConnectGobangs(gobangColor,count,point