C#实现的中国象棋程序开发笔记——遇到困难,请大家帮忙!!
最近,和朋友下象棋,然后想到这个多年陈旧的代码(这些代码有些参考了网络的一些帖子),曾经因为不知道如何实现人机对战而放弃继续研究。如今,这位朋友,给了我又重新找回来的兴趣,所以来这里请大家帮忙,如何实现人机对战,让电脑自动下呢?当前,已经完成黑、红两方的下棋规则,但是还没有实现人机对战,目前只能人人对战,也就是说一个具有下棋规则的棋盘而已。为了方便大家给我出招解惑,我先说一下自己程序的原理……
最近,和朋友下象棋,然后想到这个多年陈旧的代码(这些代码有些参考了网络的一些帖子),曾经因为不知道如何实现人机对战而放弃继续研究。如今,这位朋友,给了我又重新找回来的兴趣,所以来这里请大家帮忙,如何实现人机对战,让电脑自动下呢?
当前,已经完成黑、红两方的下棋规则,但是还没有实现人机对战,目前只能人人对战,也就是说一个具有下棋规则的棋盘而已。
为了方便大家给我出招解惑,我先说一下自己程序的原理:
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
}
}
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
}
}
2. 象棋接口
象棋接口
using System;
using System.Collections;
namespace Zivsoft.Business.Chess
{
/// <summary>
/// 象棋接口
/// </summary>
interface IChess
{
/// <summary>
///
/// </summary>
int NextX { get; }
/// <summary>
///
/// </summary>
int NextY { get; }
/// <summary>
///
/// </summary>
/// <returns></returns>
ArrayList GetNextLocation();
/// <summary>
/// 如果是红色返回为真
/// </summary>
/// <returns></returns>
bool IsRedChess { get; }
/// <summary>
///
/// </summary>
bool IsDogFall { get; }
/// <summary>
/// 是否是进攻
/// </summary>
/// <returns></returns>
bool IsAttack { get; }
/// <summary>
///
/// </summary>
string Name { get; }
/// <summary>
///
/// </summary>
int X { get; }
/// <summary>
///
/// </summary>
int Y { get; }
/// <summary>
///
/// </summary>
/// <param name="X"></param>
/// <param name="Y"></param>
void Move(int x, int y);
/// <summary>
/// 移动
/// </summary>
/// <param name="X"></param>
/// <param name="Y"></param>
/// <param name="obj"></param>
void Move(int x, int y, object obj);
}
}
using System;
using System.Collections;
namespace Zivsoft.Business.Chess
{
/// <summary>
/// 象棋接口
/// </summary>
interface IChess
{
/// <summary>
///
/// </summary>
int NextX { get; }
/// <summary>
///
/// </summary>
int NextY { get; }
/// <summary>
///
/// </summary>
/// <returns></returns>
ArrayList GetNextLocation();
/// <summary>
/// 如果是红色返回为真
/// </summary>
/// <returns></returns>
bool IsRedChess { get; }
/// <summary>
///
/// </summary>
bool IsDogFall { get; }
/// <summary>
/// 是否是进攻
/// </summary>
/// <returns></returns>
bool IsAttack { get; }
/// <summary>
///
/// </summary>
string Name { get; }
/// <summary>
///
/// </summary>
int X { get; }
/// <summary>
///
/// </summary>
int Y { get; }
/// <summary>
///
/// </summary>
/// <param name="X"></param>
/// <param name="Y"></param>
void Move(int x, int y);
/// <summary>
/// 移动
/// </summary>
/// <param name="X"></param>
/// <param name="Y"></param>
/// <param name="obj"></param>
void Move(int x, int y, object obj);
}
}
3. 接下来就是关键的部分,我曾在这里实现机器自动下棋,但是还是碰到一些困难
棋子移动
/// <summary>
/// 被吃时调用,或吃子时调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChessWordUserControl_Click(object sender, EventArgs e)
{
if (Factory.IsTurnToRedChessWalking)
{
//机器下棋(Red)
if (this.IsRedChess)
{
//红方(机器)
}
else
{
//红方吃掉黑方
this.Eat(false);
return;
}
}
else
{
//用户下棋
if (this.IsRedChess)
{
//黑方吃掉红方
this.Eat(false);
return;
}
else
{
//黑方(用户)
}
}
if (Factory.OldQiZhi != null)
{
Factory.OldQiZhi._move = EChessDraw.Null;
Factory.OldQiZhi.Refresh();
}
Factory.WeiZhi_AL = this.GetNextLocation();
Factory.OldQiZhi = this;
this._move = EChessDraw.Click;
this.Refresh();
}
/// <summary>
/// 被吃时调用,或吃子时调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChessWordUserControl_Click(object sender, EventArgs e)
{
if (Factory.IsTurnToRedChessWalking)
{
//机器下棋(Red)
if (this.IsRedChess)
{
//红方(机器)
}
else
{
//红方吃掉黑方
this.Eat(false);
return;
}
}
else
{
//用户下棋
if (this.IsRedChess)
{
//黑方吃掉红方
this.Eat(false);
return;
}
else
{
//黑方(用户)
}
}
if (Factory.OldQiZhi != null)
{
Factory.OldQiZhi._move = EChessDraw.Null;
Factory.OldQiZhi.Refresh();
}
Factory.WeiZhi_AL = this.GetNextLocation();
Factory.OldQiZhi = this;
this._move = EChessDraw.Click;
this.Refresh();
}
目前,我在网上查了不少资料,大家都想求一个比较厉害的象棋程序,可目前我的程序是最弱智的,CPU自己不会下。有人告诉我,说需要调用一些已经大师们的下棋残局API,而无需自己写一套规则,我觉得也是,不然写棋的那个人一定是下棋高手,可是我棋艺一般,但我很想写一个比较厉害的程序,让我的象棋以CPU的身份来挑战那些大师,呵呵,就是这样一个想法,目前用C#实现,先不讨论性能,能有人指点实现一下也不错。
如果有人有兴趣,我想把代码放在officelive.com上,其实重要不是代码了,感觉这些程序的算法和思想设计很重要,欢迎高手指点一二。:-)