C#写的五子棋程序

C#写的五子棋程序
 
 
<script language="javascript" src="http://usms.tom.com/wxqy_subscribe_new.js?tomuserid=9247" type="text/javascript"></script>

在ImageList控件里加入空白棋子的两幅图片.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace WindowsApplication2
{
 public class Form1 : 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 System.Windows.Forms.MenuItem menuItem7;
  private System.ComponentModel.IContainer components;
  private System.Windows.Forms.ImageList imageList1;
  private const int None = -1;//没有棋子
  private const int White = 0;//代表白棋
  private const int Black = 1;//代表黑棋
  private int [,]checkerBoard = new int [15, 15];//棋盘(用来保存每一个棋子)
  private int nextPlayer;//下一个选手
  private int Player
  {
   get
   {
    return nextPlayer;
   }
   set
   {
    nextPlayer = value;
    ReDrawNextPlayerMark();
   }
  }
  private System.Windows.Forms.MenuItem menuItem8;
  private System.Windows.Forms.MenuItem menuItem9;
  private System.Windows.Forms.MenuItem menuItem10;//下一次该黑棋还是白棋下
  private Stack History;//下棋的历史记录
  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();


   //
   // TODO: Add any constructor code after InitializeComponent call
   //   checkerBoard = new int [15,15];
   History = new Stack();
   Player = White;//默认设置为白棋先下
   Reset();


  }


  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }


  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.menuItem8 = new System.Windows.Forms.MenuItem();
   this.menuItem9 = new System.Windows.Forms.MenuItem();
   this.menuItem4 = new System.Windows.Forms.MenuItem();
   this.menuItem5 = new System.Windows.Forms.MenuItem();
   this.menuItem6 = new System.Windows.Forms.MenuItem();
   this.menuItem7 = new System.Windows.Forms.MenuItem();
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.menuItem10 = new System.Windows.Forms.MenuItem();
   this.menuItem2 = new System.Windows.Forms.MenuItem();
   this.menuItem3 = new System.Windows.Forms.MenuItem();
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.imageList1 = new System.Windows.Forms.ImageList(this.components);
   //
   // menuItem8
   //
   this.menuItem8.Index = 3;
   this.menuItem8.Shortcut = System.Windows.Forms.Shortcut.AltBksp;
   this.menuItem8.Text = "悔一步棋 (&H)";
   this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
   //
   // menuItem9
   //
   this.menuItem9.Index = 1;
   this.menuItem9.Text = "-";
   //
   // menuItem4
   //
   this.menuItem4.Index = 1;
   this.menuItem4.Text = "关于… (&A)";
   this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click_1);
   //
   // menuItem5
   //
   this.menuItem5.Index = 0;
   this.menuItem5.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
   this.menuItem5.Text = "重新开始 (&N)";
   this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
   //
   // menuItem6
   //
   this.menuItem6.Index = 5;
   this.menuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
   this.menuItem6.Text = "退出 (&X)";
   this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
   //
   // menuItem7
   //
   this.menuItem7.Index = 4;
   this.menuItem7.Text = "-";
   //
   // menuItem1
   //
   this.menuItem1.Index = 0;
   this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem5,
                       this.menuItem9,
                       this.menuItem10,
                       this.menuItem8,
                       this.menuItem7,
                       this.menuItem6});
   this.menuItem1.Text = "系统 (&S)";
   this.menuItem1.Popup += new System.EventHandler(this.menuItem1_Popup);
   this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
   //
   // menuItem10
   //
   this.menuItem10.Index = 2;
   this.menuItem10.Text = "黑棋先下 (&B)";
   this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
   //
   // menuItem2
   //
   this.menuItem2.Index = 1;
   this.menuItem2.MdiList = true;
   this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem3,
                       this.menuItem4});
   this.menuItem2.Text = "帮助 (&H)";
   this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
   //
   // menuItem3
   //
   this.menuItem3.Index = 0;
   this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.F1;
   this.menuItem3.Text = "使用方法";
   this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click_1);
   //
   // mainMenu1
   //
   this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                       this.menuItem1,
                       this.menuItem2});
   //
   // imageList1
   //
   this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
   this.imageList1.ImageSize = new System.Drawing.Size(20, 20);
   this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
   this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
   //
   // Form1
   //
   this.AccessibleDescription = "";
   this.AllowDrop = true;
   this.AutoScale = false;
   this.AutoScaleBaseSize = new System.Drawing.Size(10, 25);
   this.BackgroundImage = ((System.Drawing.Bitmap)(resources.GetObject("$this.BackgroundImage")));
   this.ClientSize = new System.Drawing.Size(325, 333);
   this.Cursor = System.Windows.Forms.Cursors.Hand;
   this.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
   this.ImeMode = System.Windows.Forms.ImeMode.Off;
   this.MaximizeBox = false;
   this.Menu = this.mainMenu1;
   this.Name = "Form1";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "五子棋";
   this.Load += new System.EventHandler(this.Form1_Load);


  }
  #endregion


  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }


  private void menuItem4_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }


  private void menuItem3_Click(object sender, System.EventArgs e)
  {


  }


  private void Form1_Load(object sender, System.EventArgs e)
  {


  }


  private void menuItem1_Click(object sender, System.EventArgs e)
  {


  }


  private void menuItem4_Click_1(object sender, System.EventArgs e)
  {
   (new About()).ShowDialog();
  }


  private void menuItem2_Click(object sender, System.EventArgs e)
  {


  }


  private void menuItem3_Click_1(object sender, System.EventArgs e)
  {
   new Help().ShowDialog();
  }


  private void menuItem6_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }


  private void menuItem5_Click(object sender, System.EventArgs e)
  {
   if(History.Count > 0) this.Reset();
  }


  private void menuItem8_Click(object sender, System.EventArgs e)
  {
   Penitence();
  }


  //重载重画函数
  protected override void OnPaint(PaintEventArgs e)
  {
   System.Drawing.Graphics g = this.CreateGraphics();
   for(int x = 0; x < 15; x++)
   {
    for(int y= 0; y < 15; y++)
    {
     if(checkerBoard[ y, x ] != None) DrawChess(new Point(x, y), checkerBoard[ y, x ]);
    }
   }
   ReDrawNextPlayerMark();
   base.OnPaint(e);
  }


  //重载鼠标按下事件
  protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
  {
   base.OnMouseDown(e);
   switch( e.Button )
   {
     //take left button down
    case MouseButtons.Left:
     if( (History.Count == 0) && (2 < e.X) && (e.X < 22) && (309 < e.Y) && (e.Y < 329) ) Player = (Player == White)? Black: White;
     else AddChess( MToA( new Point(e.X, e.Y) ) );
     break;
     //take right button down
    case MouseButtons.Right:
     //     OnRButtonDown(new Point(e.X,e.Y));
     break;
   }
  }


  //把鼠标坐标转换成棋盘坐标
  private Point MToA(Point p)
  {
   return new Point( (p.X - 15) / 20, (p.Y - 6) / 20);
  }


  //开始新的棋局,所有数据复位并重画棋盘
  private void Reset()
  {
   for( int i = 0; i < 15; i++ )
   {
    for( int j = 0; j < 15; j++)
    {
     checkerBoard[i,j] = None;
    }
   }
   History.Clear();//清空历史记录
   DrawCheckerBoard();//重画棋盘
  }


  //重画棋盘
  private void DrawCheckerBoard()
  {
   this.BackColor = System.Drawing.SystemColors.Desktop;
   this.BackColor = System.Drawing.SystemColors.Control;
  }


  //重画下一个选手的标志棋子
  private void ReDrawNextPlayerMark()
  {
   System.Drawing.Graphics g = this.CreateGraphics();
   imageList1.Draw(g, 2, 309, 20, 20, Player);
  }


  //在棋盘上画一个棋子
  private void DrawChess(Point pCoordinates,int iPlayer)
  {
   System.Drawing.Graphics g = this.CreateGraphics();
   imageList1.Draw(g, 15 + pCoordinates.X * 20, 6 + pCoordinates.Y * 20, 20, 20, iPlayer);
  }


  //在指定的棋盘坐标位置添加一个棋子
  private void AddChess(Point p)
  {
   //判断是否超出边界
   if( (p.X < 0 || p.X > 14) || (p.Y < 0 || p.Y > 14) ) return;
   //判断该位置有无棋子
   if( checkerBoard[ p.Y, p.X ] != None ) return;//如果已经有了棋子则退出(这颗棋不能下)
   DrawChess( p,Player );
   if(Player == White)//该白棋下
   {
    checkerBoard[ p.Y, p.X ] = White;
    Player = Black;
   }
   else
   {
    checkerBoard[ p.Y, p.X ] = Black;
    Player = White;
   }
   History.Push( new Point(p.X, p.Y) );//添加历史记录
   CheckGameResult( p );
  }


  //检查游戏结果
  /*
   * 算法:
   * 由于根据游戏规则,只有先放成 5 个相连的棋子的一方或剩,
   * 所以只要以刚刚下过的那个棋子为中心,检查有没有 5 个相连的同颜色棋子就好了。
   */
  private void CheckGameResult(Point sourcePoint)
  {
   int x,y;
   int LastPlayer = (Player == White)? Black: White;
   int n;


   //检查行
   n = 1;
   y = sourcePoint.Y;
   for(x = sourcePoint.X - 1; x >= 0; x--)
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   for(x = sourcePoint.X + 1; x < 15; x++)
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   if(n >= 5)
   {
    Win(LastPlayer);
    return;
   }


   //检查列
   n = 1;
   x = sourcePoint.X;
   for(y = sourcePoint.Y - 1; y >= 0; y--)
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   for(y = sourcePoint.Y + 1; y < 15; y++)
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   if(n >= 5)
   {
    Win(LastPlayer);
    return;
   }


   //检查左斜线
   n=1;
   for(x = sourcePoint.X + 1 , y = sourcePoint.Y - 1; x < 15 && y >= 0; x++, y--)//检查左斜线的上半截
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   for(x = sourcePoint.X - 1 , y = sourcePoint.Y + 1; x >= 0 && y < 15; x--, y++)//检查左斜线的下半截
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   if(n >= 5)
   {
    Win(LastPlayer);
    return;
   }


   //检查右斜线
   n = 1;
   for(x = sourcePoint.X - 1 , y = sourcePoint.Y - 1; x >= 0 && y >= 0; x--, y--)//检查右斜线的上半截
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   for(x = sourcePoint.X + 1 , y = sourcePoint.Y + 1; x < 15 && y < 15; x++, y++)//检查右斜线的下半截
   {
    if(checkerBoard[ y, x ] == LastPlayer) n++;
    else break;
   }
   if(n >= 5)
   {
    Win(LastPlayer);
    return;
   }


   //棋盘满棋,平手
   if(History.Count >= 225)
   {
    Win(None);
    return;
   }


  }


  //显示指定选手的获胜信息
  private void Win(int NPlayer)
  {
   switch(NPlayer)
   {
    case None:
     MessageBox.Show("本局平手!","恭喜!");
     break;
    case White:
     MessageBox.Show("本局 白棋 获胜 !","恭喜!");
     break;
    case Black:
     MessageBox.Show("本局 黑棋 获胜 !","恭喜!");
     break;
   }
   Reset();
  }


  //悔棋 1 步
  private void Penitence()
  {
   if(History.Count > 0)
   {
    Point p = (Point)History.Pop();
    checkerBoard[ p.Y, p.X ] = None;
    nextPlayer = (Player == White)? Black: White;
    DrawCheckerBoard();
   }
  }


  private void menuItem10_Click(object sender, System.EventArgs e)
  {
   Player = (Player == White)? Black: White;
  }


  private void menuItem1_Popup(object sender, System.EventArgs e)
  {
   if(History.Count == 0)
   {
    menuItem10.Visible = true;
    menuItem10.Text = (Player == White)? "黑棋先下 (&B)": "白棋先下 (&W)";
   }
   else menuItem10.Visible = false;
  }


 }
}

 
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
c#五子棋人机对战 核心算法 核心算法就是计算计算机应该在哪里落子。    思路的伪代码如下。    PC_Stone    For i = 1 to 15     For j = 1 to 15     If ( board[i][j] != -1)     Qz[i][j] = -     Esle     FindQz(Qz[i][j])    getTheMaxQz()    而在这个过程中最主要的算法是计算每个点的权重,由此判断电脑应该将棋子落在哪个地方。    计算确定点的权重的函数是FindQz();,函数里面有对于多种不同情况下,函数所赋给那个点的权重值,这些值是累加的。函数主要通过对四个函数X1(),X2(),X3(),X4()的调用来确定每个点所处地位。    FindQz()函数可以分为两部分。    第一部分是假设人在此点落下一子后,此点给人所带来的好处是多少。通过对函数X1()计算如果在点board[i][j]落下,所在行有多少连续的相同的点数。与X1(),类似的是X2()是计算board[i][j]所在列的点数。X3()是计算左高右低的斜排,X4()是左低右高的情况。经过计算后,将这四种情况所带来的改变加到一起,就是将棋子落在这里对假设方带来的好处。    第二部分是假设电脑在此落一点之后,此点给电脑带来的好处是多少。调用过程与第一部分基本一样,没什么不同。    经过这样的调用,将两部分计算出的结果加到一起,算出来的就是电脑下在这一点会带来的所有影响。选取影响最大的一点,落子,这样就能在一定程度上达到某种智能。    对于X1()函数,他的作用实现是这样的。运用两个计数器count与flag,count用于计算有多少相连的相同的子,并且是一board[i][j]为中心,向两边发散(说他是中心并不意味着他是相连的点的中心位置)。Flag是计算相连的子的两端是否有阻挡。阻挡分为两部分,一部分是到了表格的尽头,另一种是被另一种颜色的棋子挡住。这样综合count与flag两个参数,给出board[i][j]点对于行的贡献值。    对于X2(),X3(),X4()他们的原理是与X1()一样的,只不过是坐标不同罢了。这样,计算完之后,再进行比较,就能得到最好的点了。        对于特殊落点的判断问题:   设以围棋棋盘左下角为坐标原点建立直角坐标系xOy,   若(9,10)(9,11)(10,10)(11,9)上有黑子,(8,12)(10,9)(11,8)上有白子,现在到白棋走子,   若走(10,12)(11,11)就属于斜向走子,但是通常直向的(紧贴着棋子走的)走子要比斜向的走子对防守的贡献大,   若走(8,11)(8,10)(8,9)(9,9)其中一个的均属于单侧走子,而另一侧则空虚,所以也不好,   若走(11,10)也不好,因为在(11,8)的子已经对防守有了一定的贡献了,同理走(10,11)的也不好,因为(10,9)的子也对防守有了一定的贡献.所以说此时最佳的走子方法就是走(9,12)   若(8,10)有白子,(10,10)(11,10)(12,10)有黑子,到白子走,由于(8,10)对防守贡献了一部分,所以应该走(13,10)而不走(9,10)。   若为黑走,就应该走(13,10)而不走(9,10),因为白子的(8,10)会削弱它对攻的贡献

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值