庞大资源库的计算机教程网站!
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
首页 操作系统 程序设计 图形图像 媒体动画 机械电子 WEB开发 数 据 库 办公系列 路由技术 网络原理 网络应用
认证考试 安全技术
首页>程序设计>C#语言>游戏编程>正文
资料搜索
Google搜索
Google
返回上级列表

推荐文章

快速保存网页中所有图片的方法
Windows中让光驱巧妙“隐身”技
防范非法用户入侵Win 2000/XP系
两款比较典型的ASP木马防范方法
有关表格边框的css语法整理
Windows XP中可以被禁用的服务
SQL Server导出导入数据方法
Javascript所有对象的属性的获
网页(HTML)中的特殊字符
与篮球共舞,尽显模式本色
QQ病毒的手工清除方法
Photoshop为极品美女打造性感睫
天衣无缝:IIS与PHP水火也相容
SQL Server存储过程编写和优化

c#写的五子棋程序

 作者:xumei    日期:2005-8-8 9:52:36
字号选择〖 〗/ 双击滚屏 单击停止   
     前几天没事,写了一个小程序,可以用于学习C#。
  程序使用了VS.NET环境编译,你的机器只要安装了.NET Framework SDK就可以运行。
  源码和执行文件可以下载
  你不想下载也可读一下源码(图片资源等需要下载)。
  namespace Leimom.FiveChess
  {
   using System;
   using System.Drawing;
   using System.Collections;
   using System.ComponentModel;
   using System.WinForms;
   using System.Data;
   /// <summary>
   /// Summary description for Form1.
   /// </summary>
   public class FiveForm : System.WinForms.Form
   {
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ComponentModel.Container components;
   private System.WinForms.ImageList imageListbw;
   //define the hot Rectangle
   private Rectangle[] pointSquares;
   //chess information
   private int[] chessTable;
   private int nextTurn;
   private const int bTurn = 1;
   private const int wTurn = 2;
   private Stack chessIndex;
   public FiveForm()
   {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
   //
   // TODO: Add any constructor code after InitializeComponent call
   //
   chessIndex = new Stack();
   nextTurn = bTurn;
   chessTable = new int[225];
   pointSquares = new Rectangle[225];
   Size size = new Size(18,18);
   int x = 0;
   int y = 0;
   for(int i = 0;i < 225;i++)
   {
   x = i%15;
   y = i/15;
   pointSquares[i].Size = size;
   pointSquares[i].Offset(9+x*20,6+y*20);
   chessTable[i] = 0;
   }
   }
  
   protected override void OnPaint(PaintEventArgs e)
   {
   //you may paint
   Graphics g = e.Graphics;
   }
   protected override void OnMouseDown(System.WinForms.MouseEventArgs e)
   {
   switch( e.Button )
   {
   //take left button down
   case MouseButtons.Left:
   OnLButtonDown(new Point(e.X,e.Y));
   break;
   //take right button down
   case MouseButtons.Right:
   OnRButtonDown(new Point(e.X,e.Y));
   break;
   }
   base.OnMouseDown(e);
   }
   private void OnLButtonDown(Point p)
   {
   int nPos = GetRectID(p);
   //click hot Rectangle witch have no chess
   if(nPos != -1&&chessTable[nPos] == 0)
   {
   Graphics g = this.CreateGraphics();
   if(nextTurn==bTurn)
   {
   //draw white chess
   DrawBlack(g,nPos);
   chessTable[nPos] = bTurn;
   nextTurn = wTurn;
   chessIndex.Push(bTurn);
   chessIndex.Push(nPos);
   }
   else
   {
   //draw Black chess
   DrawWhite(g,nPos);
   chessTable[nPos] = wTurn;
   nextTurn = bTurn;
   chessIndex.Push(wTurn);
   chessIndex.Push(nPos);
   }
   g.Dispose();
   //witch win
   CheckGameResult(nPos,nextTurn);
   }
   }
   private void CheckGameResult(int nPos,int nextTurn)
   {
   //witch win
   Stack isFive = new Stack();
   int thisTurn = (nextTurn == bTurn)?wTurn:bTurn;
   int x = nPos%15;
   int y = nPos/15;
   //scan x have five
   for(int i=0;i<15;i++)
   {
   if(chessTable[y*15+i] == thisTurn)
   {
   isFive.Push(y*15+i);
   if(isFive.Count == 5)
   {
   MessageBox.Show("Game Over","Notes",MessageBox.OK);
   ReSetGame();
   return;
   }
   }
   else
   {
   isFive.Clear();
   }
   }
   isFive.Clear();
   //scan y have five
   for(int i=0;i<15;i++)
   {
   if(chessTable[i*15+x] == thisTurn)
   {
   isFive.Push(i*15+x);
   if(isFive.Count == 5)
   {
   MessageBox.Show("Game Over","Notes",MessageBox.OK);
   ReSetGame();
   return;
   }
   }
   else
   {
   isFive.Clear();
   }
   }
   isFive.Clear();
   //scan x=y have five
   for(int i=-14;i<15;i++)
   {
   if(x+i<0||x+i>14||y-i<0||y-i>14)
   {
   continue;
   }
   else
   {
   if(chessTable[(y-i)*15+x+i] == thisTurn)
   {
   isFive.Push((y-i)*15+x+i);
   if(isFive.Count == 5)
   {
   MessageBox.Show("Game Over","Notes",MessageBox.OK);
   ReSetGame();
   return;
   }
   }
   else
   {
   isFive.Clear();
   }
   }
   }
   isFive.Clear();
   //scan x=-y have five
   for(int i=-14;i<15;i++)
   {
   if(x+i<0||x+i>14||y+i<0||y+i>14)
   {
   continue;
   }
   else
   {
   if(chessTable[(y+i)*15+x+i] == thisTurn)
   {
   isFive.Push((y+i)*15+x+i);
   if(isFive.Count == 5)
   {
   MessageBox.Show("Game Over","Notes",MessageBox.OK);
   ReSetGame();
   return;
   }
   }
   else
   {
   isFive.Clear();
   }
   }
   }
   isFive.Clear();
   }
   private void ReSetGame()
   {
   //reset game
   nextTurn = bTurn;
   for(int i=0;i<225;i++)
   {
   chessTable[i] = 0;
   }
   this.Invalidate();
   }
   private int GetRectID(Point p)
   {
   //get witch rectangle click
   for(int i = 0;i < 225;i++)
   {
   if(pointSquares[i].Contains( p ))
   {
   return i;
   }
   }
   return -1;
   }
   private void OnRButtonDown(Point p)
   {
   //regret chess
   int nPos,x,y;
   if(chessIndex.Count != 0)
   {
   nPos = (int)chessIndex.Pop();
   x = nPos%15;
   y = nPos/15;
   chessTable[nPos] = 0;
   nextTurn = (int)chessIndex.Pop();
   this.Invalidate(new Rectangle(new Point(8+x*20,5+y*20),new Size(20,20)));
   }
   }
   private void DrawBlack(Graphics g,int nPos)
   {
   //draw Black chess
   int x,y;
   x = nPos%15;
   y = nPos/15;
   imageListbw.DrawImage(g,8+20*x,5+20*y,20,20,0,0);
   }
   private void DrawWhite(Graphics g,int nPos)
   {
   //draw White chess
   int x,y;
   x = nPos%15;
   y = nPos/15;
   imageListbw.DrawImage(g,8+20*x,5+20*y,20,20,0,1);
   }
   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   public override void Dispose()
   {
   base.Dispose();
   components.Dispose();
   }
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof(FiveForm));
   this.components = new System.ComponentModel.Container ();
   this.imageListbw = new System.WinForms.ImageList ();
   //@this.TrayHeight = 90;
   //@this.TrayLargeIcon = false;
   //@this.TrayAutoArrange = true;
   //@imageListbw.SetLocation (new System.Drawing.Point (7, 7));
   imageListbw.ImageSize = new System.Drawing.Size (20, 20);
   imageListbw.ImageStream = (System.WinForms.ImageListStreamer) resources.GetObject ("imageListbw.ImageStream");
   imageListbw.ColorDepth = System.WinForms.ColorDepth.Depth8Bit;
   imageListbw.TransparentColor = System.Drawing.Color.Yellow;
   this.Text = "FiveForm";
   this.MaximizeBox = false;
   this.AutoScaleBaseSize = new System.Drawing.Size (6, 14);
   this.BorderStyle = System.WinForms.FormBorderStyle.FixedSingle;
   this.BackgroundImage = (System.Drawing.Image) resources.GetObject ("$this.BackgroundImage");
   this.TransparencyKey = System.Drawing.Color.White;
   this.ClientSize = new System.Drawing.Size (314, 311);
   }
  
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   public static int Main(string[] args)
   {
   Application.Run(new FiveForm());
   return 0;
   }
   }
  }
上一篇:“扫雷”游戏的幕后    下一篇:Visual C#编写3D游戏框架示例  
[发送给好友]  [关闭窗口]  [返回顶部]   转载请注明来源:www.it00.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
责任编辑: 原点 投稿作者: xumei
信息来源: 网络 录入时间: 2005-8-8 9:52:36
关于我们 - 广告服务 - 版权申明 - 网站地图 - 联系方式 - 总编信箱 - 会员投稿