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