国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

c#寫的五子棋程序,供學(xué)習(xí)WinForms的鼠標(biāo)事件和使用GDI+,源碼下載。

2024-07-21 02:20:16
字體:
供稿:網(wǎng)友
前幾天沒事,寫了一個小程序,可以用于學(xué)習(xí)c#。
程序使用了vs.net環(huán)境編譯,你的機(jī)器只要安裝了.net framework sdk就可以運(yùn)行。
源碼和執(zhí)行文件可以下載
你不想下載也可讀一下源碼(圖片資源等需要下載)。
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;
        }
    }
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁强县| 周口市| 蓬溪县| 临沭县| 孟州市| 连山| 榆社县| 横峰县| 揭东县| 额尔古纳市| 凤台县| 隆安县| 应城市| 哈密市| 师宗县| 商丘市| 清原| 潮州市| 高青县| 洛扎县| 南安市| 平遥县| 韶山市| 晋州市| 沂源县| 灌云县| 双桥区| 宣威市| 郴州市| 当阳市| 南平市| 鹰潭市| 河西区| 内江市| 丰镇市| 绥宁县| 黄大仙区| 衡山县| 名山县| 巴南区| 峡江县|