频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> Asp.Net教程 -> c#写的五子棋程序,供学习WinForms的鼠标事件和使用GD

c#写的五子棋程序,供学习WinForms的鼠标事件和使用GD

作者:未知  来源:转载  发布时间:2005-7-20 10:05:40  发布人:acx

减小字体 增大字体

前几天没事,写了一个小程序,可以用于学习C#。

程序使用了VS.NET环境编译,你的机器只要安装了.NET Framework SDK就可以运行。

源码和执行文件可以下载

http://www.wh-adv.com/download/five.zip

你不想下载也可读一下源码(图片资源等需要下载)。

namespace Leimom.FiveChess

{

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.WinForms;

    using System.Data;

    /// <summary>

    ///    Summary des cription 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.Size = size;

    pointSquares.Offset(9+x*20,6+y*20);

    chessTable = 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 = 0;

   }

   this.Invalidate();

  }

  private int GetRectID(Point p)

  {

   //get witch rectangle click

   for(int i = 0;i < 225;i++)

   {

    if(pointSquares.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;

        }

    }

}
       
       

将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· 制作我们自己的Ebay(拍卖..
· PHP3 入门教程(极短篇)..
· 在HTML中传送中文到Flas..
· 优化MySQL数据库性能的八..
· 了解WEB页面工具语言XML..
· NET移植案例学习:建造W..
· Delphi中的Access技巧集..
· mysql常识和基本操作
· 微软全球发动史上最大起..
· 解析:如何在 ASP.NET 中..
· PHP4与MySQL数据库操作函..
· PHP教程.应用实例13
· 群雄点评盛大网游免费 第..
· Dreamweaver MX进阶教程..
· WSH实用讲座---第二讲 创..
· 用FrontPage 2000制作鼠..
相关文章
· c#写的,供学习WinForms的鼠..
· C#写的支持SMTP验证的发送邮..
· C#写的支持SMTP验证的发送邮..
· 用c#写的asp+域名查询程序
· c#写的五子棋程序,供学习W..
· 一个用C#写的词法分析程序..
· 用c#写的smtp邮件发送类
· C#写的数据库操作类!
· 一个用c#写的扫描asp源码漏..
· 一个用c#写的扫描asp源码漏..
· asp+初体验---用c#写的asp+..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号