c#五子棋游戏.docx
《c#五子棋游戏.docx》由会员分享,可在线阅读,更多相关《c#五子棋游戏.docx(22页珍藏版)》请在冰豆网上搜索。
![c#五子棋游戏.docx](https://file1.bdocx.com/fileroot1/2023-1/10/df0a62ab-d15a-4748-a90d-4b57ec07cc0c/df0a62ab-d15a-4748-a90d-4b57ec07cc0c1.gif)
c#五子棋游戏
//button1——开始游戏
//button2——重新开始
//panal1——游戏界面
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Drawing.Imaging;
usingSystem.Drawing.Drawing2D;
usingSystem.Threading;
namespace图像处理
{
publicpartialclassForm1:
Form
{
xyzclass1=newxyz();
staticint[,]count=newint[15,15];
privateenumkind{white,black};
privatekindflag=kind.black;
privateBitmapbm=newBitmap(900,700);
publicForm1()
{
InitializeComponent();
}
privateclassxyz
{
publicintx{get;set;}
publicinty{get;set;}
publicintz{get;set;}
publicintposition{get;set;}
publicintlose{get;set;}
}
privatebooljudgewin()
{
intpro=1;
intk=1;
for(inti=0;i<15;i++)
for(intj=0;j<15;j++)
if(count[i,j]!
=0)
{
while(true)//往右边遍历
{
if(i+k<0||i+k>=15||j<0||j>=15)
{
pro=1;
k=1;
break;
}
if(count[i+k,j]==count[i,j])
{
pro++;
k++;
}
else
{
pro=1;
k=1;
break;
}
if(pro>=5)
{
if(count[i,j]==1)
MessageBox.Show("您赢了!
");
else
MessageBox.Show("您输了!
");
returntrue;
}
}
while(true)//往右下遍历
{
if(i+k>=15||i+k<0||j+k>=15||j+k<0)
{
pro=1;
k=1;
break;
}
if(count[i+k,j+k]==count[i,j])
{
pro++;
k++;
}
else
{
pro=1;
k=1;
break;
}
if(pro>=5)
{
if(count[i,j]==1)
MessageBox.Show("您赢了!
");
else
MessageBox.Show("您输了!
");
returntrue;
}
}
while(true)//往下边遍历
{
if(i>=15||j+k>=15||i<0||j+k<0)
{
pro=1;
k=1;
break;
}
if(count[i,j+k]==count[i,j])
{
pro++;
k++;
}
else
{
pro=1;
k=1;
break;
}
if(pro>=5)
{
if(count[i,j]==1)
MessageBox.Show("您赢了!
");
else
MessageBox.Show("您输了!
");
returntrue;
}
}
while(true)//往左下遍历
{
if(i-k>=15||i-k<0||j+k>=15||j+k<0)
{
pro=1;
k=1;
break;
}
if(count[i-k,j+k]==count[i,j])
{
pro++;
k++;
}
else
{
pro=1;
k=1;
break;
}
if(pro>=5)
{
if(count[i,j]==1)
MessageBox.Show("您赢了!
");
else
MessageBox.Show("您输了!
");
returntrue;
}
}
}
returnfalse;
}
privatevoidmachine()//机器落子
{
class1=newxyz();
intpro=1;
intk=1;
for(inti=0;i<15;i++)
for(intj=0;j<15;j++)
if(count[i,j]!
=0)
{
while(true)//往下边遍历
{
if(i<15&&j+k<15&&i>=0&&j+k>=0)
{//不越界
if(count[i,j+k]==count[i,j])
{
pro++;
k++;
}
else
{
if(count[i,j+k]!
=0&&(j-1)<0)
{//上面是墙
pro=1;
k=1;
break;
}
if(count[i,j+k]!
=0&&count[i,j-1]!
=0)
{
pro=1;
k=1;
break;
}
if(count[i,j+k]!
=0)
{
pro--;
if(class1.z{//记录点
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=3;
class1.lose=-1;
k=1;
pro=1;
break;
}
}
if(class1.z{
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=3;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
else//下面越界
{
if(count[i,j-1]!
=0)
{//上面是子
pro=1;
k=1;
break;
}
pro--;
if(class1.z{
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=3;
class1.lose=-1;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
while(true)//往右边遍历
{
if(i+k>=0&&i+k<15&&j>=0&&j<15)
{
if(count[i+k,j]==count[i,j])
{
pro++;
k++;
}
else
{
if(count[i+k,j]!
=0&&i-1<0)
{//左边是墙
pro=1;
k=1;
break;
}
if(count[i+k,j]!
=0&&count[i-1,j]!
=0)
{
pro=1;
k=1;
break;
}
if(count[i+k,j]!
=0)
{
pro--;
if(class1.z{//记录点
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=1;
class1.lose=-1;
k=1;
pro=1;
break;
}
}
if(class1.z{
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=1;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
else
{
if(count[i-1,j]!
=0)
{//左边是子
pro=1;
k=1;
break;
}
pro--;
if(class1.z{//记录点
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=1;
class1.lose=-1;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
while(true)//往右下遍历
{
if(i+k<15&&i+k>=0&&j+k<15&&j+k>=0)
{
if(count[i+k,j+k]==count[i,j])
{
pro++;
k++;
}
else
{
if(count[i+k,j+k]!
=0&&(i-1<0||j-1<0))
{//左上是墙
pro=1;
k=1;
break;
}
if(count[i+k,j+k]!
=0&&count[i-1,j-1]!
=0)
{
pro=1;
k=1;
break;
}
if(count[i+k,j+k]!
=0)
{
pro--;
if(class1.z{//记录点
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=2;
class1.lose=-1;
k=1;
pro=1;
break;
}
}
if(class1.z{
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=2;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
else
{
if(count[i-1,j-1]!
=0)
{//左上是墙
pro=1;
k=1;
break;
}
pro--;
if(class1.z{//记录点
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=2;
class1.lose=-1;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
while(true)//往左下遍历
{
if(i-k<15&&i-k>=0&&j+k<15&&j+k>=0)
{
if(count[i-k,j+k]==count[i,j])
{
pro++;
k++;
}
else
{
if(count[i-k,j+k]!
=0&&(i+1>15||j-1<0))
{
pro=1;
k=1;
break;
}
if(count[i-k,j+k]!
=0&&count[i+1,j-1]!
=0)
{
pro=1;
k=1;
break;
}
if(count[i-k,j+k]!
=0)
{
pro--;
if(class1.z{//记录点
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=4;
class1.lose=-1;
k=1;
pro=1;
break;
}
}
if(class1.z{
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=4;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
else
{
if(count[i+1,j-1]!
=0)
{
pro=1;
k=1;
break;
}
pro--;
if(class1.z{//记录点
class1.x=i;
class1.y=j;
class1.z=pro;
class1.position=4;
class1.lose=-1;
k=1;
pro=1;
break;
}
else//什么都不做
{
k=1;
pro=1;
break;
}
}
}
}
if(class1.position==1)
if(class1.lose==-1)
class1.x--;
else
class1.x+=class1.z;
if(class1.position==2)
if(class1.lose==-1)
{
class1.x--;
class1.y--;
}
else
{
class1.x+=class1.z;
class1.y+=class1.z;
}
if(class1.position==3)
if(class1.lose==-1)
class1.y--;
else
class1.y+=class1.z;
if(class1.position==4)
if(class1.lose==-1)
{
class1.x++;
class1.y--;
}
else
{
class1.x-=class1.z;
class1.y+=class1.z;
}
this.richTextBox1.AppendText("电脑落子横"+(class1.x+1)+",竖"+(class1.y+1)+"\n");
paintwhite(newRectangle(class1.x*40-15,class1.y*40-15,30,30));
count[class1.x,class1.y]=2;
if(judgewin()==true)
return;
flag=kind.black;
}
privatevoidpaintwhite(Rectanglerect)
{
Graphicsg=Graphics.FromImage(bm);
g.TranslateTransform(20,20);
g.FillEllipse(newLinearGradientBrush(newPoint(rect.X,rect.Y),newPoint(rect.X+15,rect.Y+15),Color.FromArgb(250,Color.White),Color.FromArgb(255,Color.White)),rect);
}
privatevoidpaintblack(Rectanglerect)
{
Graphicsg=Graphics.FromImage(bm);
g.TranslateTransform(20,20);
g.FillEllipse(newLinearGradientBrush(newPoint(rect.X,rect.Y),newPoint(rect.X+15,rect.Y+15),Color.FromArgb(250,Color.Black),Color.FromArgb(200,Color.Black)),rect);
}
privatevoidpaintpanel()
{
Graphicsg=Graphics.FromImage(bm);
g.TranslateTransform(20,20);
g.DrawRectangle(newPen(Color.Black,3),0,0,560,560);
for(inti=1;i<=14;i++)
{
g.DrawLine(newPen(Color.Black,1),newPoint(i*40,0),newPoint(i*40,560));
g.DrawLine(newPen(Color.Black,1),newPoint(0,i*40),newPoint(560,i*40));
}
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
this.button2.Visible=false;
for(inti=0;i<15;i++)
for(intj=0;j<15;j++)
count[i,j]=0;
paintpanel();
this.pictureBox1.BackgroundImage=null;
this.pictureBox1.BackgroundImage=bm;
}
privatevoidpictureBox1_MouseClick(objectsender,MouseEventArgse)
{
if(this.pictureBox1.BackgroundImage==null)
return;
if(flag==kind.white)
return;
intX,Y;
if(Convert.ToInt16(e.X)<20||Convert.ToInt16(e.Y)<20)
return;
if(Convert.ToInt16(e.X-20)%40<20)
X=(Convert.ToInt16(e.X-20)/40)*40;
else
X=(Convert.ToInt16(e.X-20)/40+1)*40;
if(Convert.ToInt16(e.Y-20)%40<20)
Y=(Convert.ToInt16(e.Y-20)/40)*40;
else
Y=(Convert.ToInt16(e.Y-20)/40+1)*40;
//人落子记为1
if(count[X/40,Y/40]==1)
return;
count[X/40,Y/40]=1;
this.richTextBox1.AppendText("您落子横"+(X/40+1).ToString()+",竖"+(Y/40+1)+"\n");
paintblack(newRectangle(X-15,Y-15,30,30));
this.pictureBox1.BackgroundImage=null;
this.pictureBox1.BackgroundImage=bm;
flag=kind.white;
if(judgewin()==true)
return;
machine();
}
privatevoidpictureBox1_BackgroundImageChanged(objectsender,EventArgse)
{
/*if(flag==kind.white)
{
Threadth=newThread(newThreadStart(machine));
th.Start();
}*/
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
this.button1.Visible=false;
this.button2.Visible=tru