C#实现的中国象棋程序开发笔记
2009-06-01 08:31:14 来源:WEB开发网最近,和朋友下象棋,然后想到这个多年陈旧的代码(这些代码有些参考了网络的一些帖子),曾经因为不知道如何实现人机对战而放弃继续研究。如今,这位朋友,给了我又重新找回来的兴趣,所以来这里请大家帮忙,如何实现人机对战,让电脑自动下呢?
当前,已经完成黑、红两方的下棋规则,但是还没有实现人机对战,目前只能人人对战,也就是说一个具有下棋规则的棋盘而已。
为了方便大家给我出招解惑,我先说一下自己程序的原理:
1, 32个棋子都是具体的类,并都是继承于ChessWorldBase。
棋子基类
using System;
using System.Collections;
using System.Windows.Forms;
namespace Zivsoft.Business.Chess
{
/// <summary>
/// 棋子
/// </summary>
internal abstract class ChessWordBase:IChess
{
public const int XBoardLength = 9;
public const int YBoardLength = 10;
public static ArrayList All = new ArrayList();
/// <summary>
/// 棋盘范围内
/// </summary>
/// <returns></returns>
public virtual bool Check()
{
if ((this.X > 9) || (this.X < 1))
{
return false;
}
if ((this.Y > 10) || (this.Y < 1))
{
return false;
}
return true;
}
/// <summary>
/// 被吃掉
/// </summary>
public virtual void Destroy()
{
this.IsDogFall = false;
this.X = 0;
this.Y = 0;
}
public virtual void Init()
{
this.IsDogFall = true;
this.X = 0;
this.Y = 0;
}
#region IChess Members
/// <summary>
///
/// </summary>
public bool IsRedChess
{
get;
set;
}
/// <summary>
///
/// </summary>
public bool IsDogFall
{
get;
set;
}
/// <summary>
/// 进攻
/// </summary>
public bool IsAttack
{
get;
set;
}
public int NextX
{
get;
set;
}
public int NextY
{
get;
set;
}
public int X
{
get;
set;
}
public int Y
{
get;
set;
}
/// <summary>
///
/// </summary>
public string Name
{
get;
set;
}
public abstract ArrayList GetNextLocation();
public void Move(int ix,int iy)
{
this.MoveNext();
if (this.Check(ix, iy, GetNextLocation()))
{
X = ix;
Y = iy;
}
}
private void MoveNext()
{
this.NextX = this.X;
this.NextY = this.Y;
}
public abstract bool Check(int x, int y, ArrayList al);
public void Move(int iX, int iY, object qz)
{
if (qz == null)
{
this.Move(iX, iY);
}
else
{
this.MoveNext();
if (this.Check(iX, iY, GetNextLocation()))
{
X = iX;
Y = iY;
((ChessWordBase)qz).Destroy();
}
}
}
#endregion
}
}
更多精彩
赞助商链接