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

2016-01-29 13:50 8 1 收藏

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

【 tulaoshi.com - ASP.NET 】

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

{

x = i%15;

y = i/15;

pointSquares[i].Size = size;

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

chessTable[i] = 0;

}

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

}

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

protected override void OnPaint(PaintEventArgs e)

{

//you may paint

Graphics g = e.Graphics;

}

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

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;

}

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

base.OnMouseDown(e);

}

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

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

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

DrawBlack(g,nPos);

chessTable[nPos] = bTurn;

nextTurn = wTurn;

chessIndex.Push(bTurn);

chessIndex.Push(nPos);

}

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

else

{

//draw Black chess

DrawWhite(g,nPos);

chessTable[nPos] = wTurn;

nextTurn = bTurn;

chessIndex.Push(wTurn);

chessIndex.Push(nPos);

}

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

g.Dispose();

//witch win

CheckGameResult(nPos,nextTurn);

}

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

}

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

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;

}

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

}

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

else

{

isFive.Clear();

}

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

}

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

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;

}

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

}

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

else

{

isFive.Clear();

}

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

}

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

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;

}

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

else

{

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

来源:https://www.tulaoshi.com/n/20160129/1491711.html

延伸阅读
标签: 机械迷城
《机械迷城》五子棋13步走法 会员:将军家(原创撰写) 蓝X是电脑先下的地方~ 不管他第一步下在哪里,跟着这个走法下就可以了~ 可能会输一两次,试几次就成功 《机械迷城》图文攻略之前车之鉴 在上一幕的惊心动魄的看守间里,机器人Josef成功调离了看守,不过从看守间可以直达两个地方一个是牢房、另一个就是当初Josef抓捕的房间,对于...
#includestdio.h #includestdlib.h #includegraphics.h #includebios.h #includeconio.h#define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define ESC 0x011b #define SPACE 0x3920 #define BILI 20 #define JZ 4 #define JS 3 #define N 19 int box...
标签: 机械迷城
《机械迷城》五子棋最牛图文攻略 《机械迷城》图文攻略之前车之鉴 在上一幕的惊心动魄的看守间里,机器人Josef成功调离了看守,不过从看守间可以直达两个地方一个是牢房、另一个就是当初Josef抓捕的房间,对于Josef来说逃出牢狱是首要任务,但是鉴于上次偷窥都能中枪的经验,这一次出去绝不能再以身犯险了,应该找找可...
标签: 机械迷城
《机械迷城》五子棋七步解决电脑 *号为电脑先走的地方。无论他第一步走在哪里,只要按照下面数字顺序走即可:   3   12   4   2 11 ...
怎么用ios9备忘录玩五子棋   首先来看看ios9备忘录新功能有哪些: 用内建相机或从照片图库将照片来添加到备忘录中 创建实用的核对清单,轻点一下即可勾选已完成的项目 仅用单指速绘,即可记下闪现的想法 利用其它应用中的共享菜单直接将感兴趣的项目存储到备忘录中。 这里我们运用到的是 单指速绘  功...

经验教程

430

收藏

90
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部