| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5506 人关注过本帖, 1 人收藏
标题:用c#写的五子棋程序
只看楼主 加入收藏
xiaoxuanfeng
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-11-17
收藏(1)
 问题点数:0 回复次数:9 
用c#写的五子棋程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;



namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ImageList imageList1;
private const int None = -1;//没有棋子
private const int White = 0;//代表白棋
private const int Black = 1;//代表黑棋
private int [,]checkerBoard = new int [15, 15];//棋盘(用来保存每一个棋子)
private int nextPlayer;//下一个选手
private int Player
{
get
{
return nextPlayer;
}
set
{
nextPlayer = value;
ReDrawNextPlayerMark();
}
}
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;//下一次该黑棋还是白棋下
private Stack History;//下棋的历史记录
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();



//
// TODO: Add any constructor code after InitializeComponent call
// checkerBoard = new int [15,15];
History = new Stack();
Player = White;//默认设置为白棋先下
Reset();



}



/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}



#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
//
// menuItem8
//
this.menuItem8.Index = 3;
this.menuItem8.Shortcut = System.Windows.Forms.Shortcut.AltBksp;
this.menuItem8.Text = "悔一步棋 (&H)";
this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
//
// menuItem9
//
this.menuItem9.Index = 1;
this.menuItem9.Text = "-";
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "关于… (&A)";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click_1);
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.menuItem5.Text = "重新开始 (&N)";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 5;
this.menuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
this.menuItem6.Text = "退出 (&X)";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Index = 4;
this.menuItem7.Text = "-";
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem9,
this.menuItem10,
this.menuItem8,
this.menuItem7,
this.menuItem6});
this.menuItem1.Text = "系统 (&S)";
this.menuItem1.Popup += new System.EventHandler(this.menuItem1_Popup);
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem10
//
this.menuItem10.Index = 2;
this.menuItem10.Text = "黑棋先下 (&B)";
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MdiList = true;
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem3,
this.menuItem4});
this.menuItem2.Text = "帮助 (&H)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.F1;
this.menuItem3.Text = "使用方法";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click_1);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
this.imageList1.ImageSize = new System.Drawing.Size(20, 20);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AccessibleDescription = "";
this.AllowDrop = true;
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(10, 25);
this.BackgroundImage = ((System.Drawing.Bitmap)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(325, 333);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.ImeMode = System.Windows.Forms.ImeMode.Off;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "五子棋";
this.Load += new System.EventHandler(this.Form1_Load);



}
#endregion



/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}



private void menuItem4_Click(object sender, System.EventArgs e)
{
this.Close();
}



private void menuItem3_Click(object sender, System.EventArgs e)
{



}



private void Form1_Load(object sender, System.EventArgs e)
{



}



private void menuItem1_Click(object sender, System.EventArgs e)
{



}



private void menuItem4_Click_1(object sender, System.EventArgs e)
{
(new About()).ShowDialog();
}



private void menuItem2_Click(object sender, System.EventArgs e)
{



}



private void menuItem3_Click_1(object sender, System.EventArgs e)
{
new Help().ShowDialog();
}



private void menuItem6_Click(object sender, System.EventArgs e)
{
this.Close();
}



private void menuItem5_Click(object sender, System.EventArgs e)
{
if(History.Count > 0) this.Reset();
}



private void menuItem8_Click(object sender, System.EventArgs e)
{
Penitence();
}



//重载重画函数
protected override void OnPaint(PaintEventArgs e)
{
System.Drawing.Graphics g = this.CreateGraphics();
for(int x = 0; x < 15; x++)
{
for(int y= 0; y < 15; y++)
{
if(checkerBoard[ y, x ] != None) DrawChess(new Point(x, y), checkerBoard[ y, x ]);
}
}
ReDrawNextPlayerMark();
base.OnPaint(e);
}



//重载鼠标按下事件
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseDown(e);
switch( e.Button )
{
//take left button down
case MouseButtons.Left:
if( (History.Count == 0) && (2 < e.X) && (e.X < 22) && (309 < e.Y) && (e.Y < 329) ) Player = (Player == White)? Black: White;
else AddChess( MToA( new Point(e.X, e.Y) ) );
break;
//take right button down
case MouseButtons.Right:
// OnRButtonDown(new Point(e.X,e.Y));
break;
}
}



//把鼠标坐标转换成棋盘坐标
private Point MToA(Point p)
{
return new Point( (p.X - 15) / 20, (p.Y - 6) / 20);
}



//开始新的棋局,所有数据复位并重画棋盘
private void Reset()
{
for( int i = 0; i < 15; i++ )
{
for( int j = 0; j < 15; j++)
{
checkerBoard[i,j] = None;
}
}
History.Clear();//清空历史记录
DrawCheckerBoard();//重画棋盘
}



//重画棋盘
private void DrawCheckerBoard()
{
this.BackColor = System.Drawing.SystemColors.Desktop;
this.BackColor = System.Drawing.SystemColors.Control;
}



//重画下一个选手的标志棋子
private void ReDrawNextPlayerMark()
{
System.Drawing.Graphics g = this.CreateGraphics();
imageList1.Draw(g, 2, 309, 20, 20, Player);
}



//在棋盘上画一个棋子
private void DrawChess(Point pCoordinates,int iPlayer)
{
System.Drawing.Graphics g = this.CreateGraphics();
imageList1.Draw(g, 15 + pCoordinates.X * 20, 6 + pCoordinates.Y * 20, 20, 20, iPlayer);
}



//在指定的棋盘坐标位置添加一个棋子
private void AddChess(Point p)
{
//判断是否超出边界
if( (p.X < 0 || p.X > 14) || (p.Y < 0 || p.Y > 14) ) return;
//判断该位置有无棋子
if( checkerBoard[ p.Y, p.X ] != None ) return;//如果已经有了棋子则退出(这颗棋不能下)
DrawChess( p,Player );
if(Player == White)//该白棋下
{
checkerBoard[ p.Y, p.X ] = White;
Player = Black;
}
else
{
checkerBoard[ p.Y, p.X ] = Black;
Player = White;
}
History.Push( new Point(p.X, p.Y) );//添加历史记录
CheckGameResult( p );
}



//检查游戏结果
/*
* 算法:
* 由于根据游戏规则,只有先放成 5 个相连的棋子的一方或剩,
* 所以只要以刚刚下过的那个棋子为中心,检查有没有 5 个相连的同颜色棋子就好了。
*/
private void CheckGameResult(Point sourcePoint)
{
int x,y;
int LastPlayer = (Player == White)? Black: White;
int n;



//检查行
n = 1;
y = sourcePoint.Y;
for(x = sourcePoint.X - 1; x >= 0; x--)
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
for(x = sourcePoint.X + 1; x < 15; x++)
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
if(n >= 5)
{
Win(LastPlayer);
return;
}



//检查列
n = 1;
x = sourcePoint.X;
for(y = sourcePoint.Y - 1; y >= 0; y--)
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
for(y = sourcePoint.Y + 1; y < 15; y++)
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
if(n >= 5)
{
Win(LastPlayer);
return;
}



//检查左斜线
n=1;
for(x = sourcePoint.X + 1 , y = sourcePoint.Y - 1; x < 15 && y >= 0; x++, y--)//检查左斜线的上半截
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
for(x = sourcePoint.X - 1 , y = sourcePoint.Y + 1; x >= 0 && y < 15; x--, y++)//检查左斜线的下半截
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
if(n >= 5)
{
Win(LastPlayer);
return;
}



//检查右斜线
n = 1;
for(x = sourcePoint.X - 1 , y = sourcePoint.Y - 1; x >= 0 && y >= 0; x--, y--)//检查右斜线的上半截
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
for(x = sourcePoint.X + 1 , y = sourcePoint.Y + 1; x < 15 && y < 15; x++, y++)//检查右斜线的下半截
{
if(checkerBoard[ y, x ] == LastPlayer) n++;
else break;
}
if(n >= 5)
{
Win(LastPlayer);
return;
}



//棋盘满棋,平手
if(History.Count >= 225)
{
Win(None);
return;
}



}



//显示指定选手的获胜信息
private void Win(int NPlayer)
{
switch(NPlayer)
{
case None:
MessageBox.Show("本局平手!","恭喜!");
break;
case White:
MessageBox.Show("本局 白棋 获胜 !","恭喜!");
break;
case Black:
MessageBox.Show("本局 黑棋 获胜 !","恭喜!");
break;
}
Reset();
}



//悔棋 1 步
private void Penitence()
{
if(History.Count > 0)
{
Point p = (Point)History.Pop();
checkerBoard[ p.Y, p.X ] = None;
nextPlayer = (Player == White)? Black: White;
DrawCheckerBoard();
}
}



private void menuItem10_Click(object sender, System.EventArgs e)
{
Player = (Player == White)? Black: White;
}



private void menuItem1_Popup(object sender, System.EventArgs e)
{
if(History.Count == 0)
{
menuItem10.Visible = true;
menuItem10.Text = (Player == White)? "黑棋先下 (&B)": "白棋先下 (&W)";
}
else menuItem10.Visible = false;
}



}
}

搜索更多相关主题的帖子: System 五子棋 Forms using private 
2005-11-25 20:21
王杰
Rank: 2
等 级:新手上路
威 望:4
帖 子:307
专家分:0
注 册:2005-5-13
收藏
得分:0 
欢迎您的到来!喜欢的不得了!

http://zdrjwlb.bulo. QQ群:1659819
2005-11-25 20:49
唐伯猫
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:5323
专家分:58
注 册:2005-8-9
收藏
得分:0 
做成了压缩包,岂不是更好!

<iframe name="alimamaifrm" frameborder="0" marginheight="0" marginwidth="0" border="0" scrolling="no" width="300" height="170" src="/go/app/tbk_app/chongzhi_300_170.php?pid=mm_28854300_2441872_11377541&page=chongzhi_300_170.php&size_w=300&size_h=170&stru_phone=1&stru_game=1&stru_travel=1" ></iframe>
2005-11-25 23:23
zhangwei
Rank: 1
等 级:新手上路
威 望:1
帖 子:277
专家分:0
注 册:2005-5-3
收藏
得分:0 
以下是引用王杰在2005-11-25 20:49:00的发言:
欢迎您的到来!喜欢的不得了!

唉,可惜手头上还有一些东西还没做完! 等手头上的事搞定了,我也做几个!(吹牛,你还没学C#呢)


主动沟通......
2005-11-27 16:07
bjdcbltx
Rank: 1
等 级:新手上路
帖 子:167
专家分:0
注 册:2005-12-4
收藏
得分:0 
边看边学习,对我帮助很大,多谢!

感谢有大家的陪伴!!! E-mail:bjdcbltx@
2005-12-04 15:06
云少
Rank: 1
等 级:新手上路
帖 子:5
专家分:3
注 册:2010-10-16
收藏
得分:0 
挺好
2010-10-16 20:51
金海1108
Rank: 2
等 级:论坛游民
帖 子:22
专家分:54
注 册:2010-10-17
收藏
得分:0 
要是能下载就更好啊!!
2011-03-22 11:36
majiayue
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-5-5
收藏
得分:0 
不能下载啊!!!!
2011-05-16 10:45
NJUPT0902
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-4-26
收藏
得分:0 
回复 楼主 xiaoxuanfeng
你好,你写的程序里面的About()类和Help()如何正确使用,是不是缺少一些文件
2014-04-26 21:12
shhsmy
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-5-3
收藏
得分:0 
可不可以制作成压缩包,发我一下,十分感谢1669667786@,课程设计需要,跪谢!
2015-05-26 22:28
快速回复:用c#写的五子棋程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.063561 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved