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

爱之海1980

爱之海1980

2016-01-29 13:50

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

前几天没事,写了一个小程序,可以用于学习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 description for Form1.

///

public class FiveForm : System.WinForms.Form

{

///<summary

/// Required designer variable.

///

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++)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

{

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

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

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+i14||y-i<0||y-i14)

{

continue;

}

else

{

if(chessTable[(y-i)*15+x+i] ==

展开更多 50%)
分享

猜你喜欢

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

电脑网络
c#写的五子棋程序 供学习WinForms的鼠标事件和使用GDI+

五子棋

编程语言 网络编程
五子棋

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

五子棋算法探讨

编程语言 网络编程
五子棋算法探讨

五子棋技巧有哪些

生活常识
五子棋技巧有哪些

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Director五子棋游戏的制作

设计软件 平面设计软件 广告设计软件
Director五子棋游戏的制作

《机械迷城》五子棋走法

机械迷城
《机械迷城》五子棋走法

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

《怪物猎人X》猎喵养成心得 加强战斗力!

《怪物猎人X》猎喵养成心得 加强战斗力!

3DS《火焰纹章if》暗夜第十章过法详解

3DS《火焰纹章if》暗夜第十章过法详解
下拉加载更多内容 ↓