五子棋源码 namespace mengqingsoft { /// <summary> /// Form2 的摘要说明。 /// </summary> public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MenuItem menuItem5; private System.Windows.Forms.MenuItem menuItem6; //************************************************ private string who; private int[,] place; private int[,] plag; private int bb1,bb2; private static int left,right; SolidBrush brush1=new SolidBrush(Color.Black); SolidBrush brush2=new SolidBrush(Color.White); //SolidBrush brush3=new SolidBrush(Color.DarkSeaGreen); private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.RadioButton black; private System.Windows.Forms.RadioButton white; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.SaveFileDialog saveFileDialog1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public Form2() { // // Windows 窗体设计器支持所必需的 // who="black"; place=new int[16,16]; clr(); plag=new int[8,2]; plag[0,0]=-1;plag[0,1]=0; plag[1,0]=-1;plag[1,1]=-1; plag[2,0]=0;plag[2,1]=-1; plag[3,0]=1;plag[3,1]=-1; plag[4,0]=1;plag[4,1]=0; plag[5,0]=1;plag[5,1]=1; plag[6,0]=0;plag[6,1]=1; plag[7,0]=-1;plag[7,1]=1; InitializeComponent();
// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form2)); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.menuItem5 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem6 = new System.Windows.Forms.MenuItem(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.black = new System.Windows.Forms.RadioButton(); this.white = new System.Windows.Forms.RadioButton(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); this.SuspendLayout(); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1, this.menuItem2}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem3, this.menuItem4, this.menuItem5}); this.menuItem1.Text = "文件"; // // menuItem3 // this.menuItem3.Index = 0; this.menuItem3.Text = "保存棋盘"; this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click); // // menuItem4 // this.menuItem4.Index = 1; this.menuItem4.Text = "打开棋盘"; // // menuItem5 // this.menuItem5.Index = 2; this.menuItem5.Text = "退出"; this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click); // // menuItem2 // this.menuItem2.Index = 1; this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem6}); this.menuItem2.Text = "编辑"; // // menuItem6 // this.menuItem6.Index = 0; this.menuItem6.Text = "悔棋"; this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click); // // label1 // this.label1.Location = new System.Drawing.Point(536, 80); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(184, 48); this.label1.TabIndex = 2; // // label2 // this.label2.Location = new System.Drawing.Point(542, 280); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(192, 48); this.label2.TabIndex = 3; // // label3 // this.label3.Location = new System.Drawing.Point(536, 136); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(128, 16); this.label3.TabIndex = 4; // // label4 // this.label4.Location = new System.Drawing.Point(546, 336); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(152, 24); this.label4.TabIndex = 5; // // black // this.black.Location = new System.Drawing.Point(536, 32); this.black.Name = "black"; this.black.TabIndex = 6; this.black.Text = "黑方先手"; this.black.CheckedChanged += new System.EventHandler(this.black_CheckedChanged); // // white // this.white.Location = new System.Drawing.Point(544, 232); this.white.Name = "white"; this.white.TabIndex = 7; this.white.Text = "白方先手"; this.white.CheckedChanged += new System.EventHandler(this.white_CheckedChanged); // // saveFileDialog1 // this.saveFileDialog1.DefaultExt = "five"; // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.Color.Green; this.ClientSize = new System.Drawing.Size(769, 557); this.Controls.Add(this.white); this.Controls.Add(this.black); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Menu = this.mainMenu1; this.MinimizeBox = false; this.MinimumSize = new System.Drawing.Size(777, 607); this.Name = "Form2"; this.Text = "五子棋人人对弈"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.Load += new System.EventHandler(this.Form2_Load); this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form2_MouseUp); this.Closed += new System.EventHandler(this.Form2_Closed); this.ResumeLayout(false);
} #endregion protected override void OnPaint(PaintEventArgs e) { Graphics g=e.Graphics; Pen pen=new Pen(Color.Black); for(int i=45;i<=495;i+=30) g.DrawLine(pen,45,i,495,i); for(int j=45;j<=495;j+=30) g.DrawLine(pen,j,45,j,495); replace(); } //************************************重开始 public void clr() { for(int i=0;i<=15;i++) for(int j=0;j<=15;j++) place[i,j]=-1;
} //*************************************重画棋盘 public void replace() { Graphics g=CreateGraphics(); for(int i=0;i<=15;i++) for(int j=0;j<=15;j++) { if(place[i,j]==0) { int x=(i+1)*30; int y=(j+1)*30; g.FillEllipse(brush1,x,y,30,30); } if(place[i,j]==1) { int x1=(i+1)*30; int y1=(j+1)*30; g.FillEllipse(brush2,x1,y1,30,30); } } } //**********************************判断谁赢,并显示出来 public void win(int x,int y) { int win,f,m,n; for(int i=0;i<4;i++) { win=0; f=place[x,y]; for(m=x,n=y;place[m,n]==f;) { win++;m+=plag[i,0];n+=plag[i,1]; if(m<0||m>15||n<0||n>15) break; } for(m=x,n=y;place[m,n]==f;) { win++;m+=plag[i+4,0];n+=plag[i+4,1]; if(m<0||m>15||n<0||n>15) break; } if(win>5) { if(place[x,y]==0) { bb1++; label3.Text="赢:"+bb1+" "+"输:"+bb2; label4.Text="赢:"+bb2+" "+"输:"+bb1; MessageBox.Show("黑方胜","胜利",MessageBoxButtons.OK,MessageBoxIcon.Information); clr(); white.Enabled=true; black.Enabled=true; Invalidate(); break; } else { bb2++; label3.Text="赢:"+bb1+" "+"输:"+bb2; label4.Text="赢:"+bb2+" "+"输:"+bb1; MessageBox.Show("白方胜","胜利",MessageBoxButtons.OK,MessageBoxIcon.Information); clr(); white.Enabled=true; black.Enabled=true; Invalidate(); break; } } } }
private void Form2_Load(object sender, System.EventArgs e) { //Graphics g=CreateGraphics(); //g.FillRectangle(brush3,45,45,450,450); }
private void Form2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.X<=40||e.X>=500||e.Y<=40||e.Y>=500) return; white.Enabled=false; black.Enabled=false; int x=e.X-e.X%30; int y=e.Y-e.Y%30; int l=x/30-1; int r=y/30-1; left=l; right=r; if(place[l,r]==-1) {
Graphics g=CreateGraphics(); if(who=="black") { g.FillEllipse(brush1,x,y,30,30); place[l,r]=0; win(l,r); label1.Text="黑方禁手"; label1.ForeColor=Color.Black; label2.Text="白方下手"; label2.ForeColor=Color.Red; who="white";
} else { g.FillEllipse(brush2,x,y,30,30); place[l,r]=1; win(l,r); label1.Text="黑方下手"; label1.ForeColor=Color.Red; label2.Text="白方禁手"; label2.ForeColor=Color.Black; who="black"; }
} else return;
}
private void black_CheckedChanged(object sender, System.EventArgs e) { who="black"; white.Enabled=false; black.Enabled=false; }
private void white_CheckedChanged(object sender, System.EventArgs e) { who="white"; white.Enabled=false; black.Enabled=false; }
private void menuItem5_Click(object sender, System.EventArgs e) { //Form1 form1=new Form1(); //form1.Show(); this.Close(); }
private void Form2_Closed(object sender, System.EventArgs e) { Form1 form1=new Form1(); form1.Show(); }
private void menuItem6_Click(object sender, System.EventArgs e) { place[left,right]=-1; replace(); Invalidate(); }
private void menuItem3_Click(object sender, System.EventArgs e) { SaveFileDialog saveFileDialog1=new SaveFileDialog(); saveFileDialog1.ShowDialog(); } } }