搜档网
当前位置:搜档网 › C#实现的中国象棋程序开发笔记

C#实现的中国象棋程序开发笔记

C#实现的中国象棋程序开发笔记

最近,和朋友下象棋,然后想到这个多年陈旧的代码(这些代码有些参考了网络的一些帖子),曾经因为不知道如何实现人机对战而放弃继续研究。如今,这位朋友,给了我又重新找回来的兴趣,所以来这里请大家帮忙,如何实现人机对战,让电脑自动下呢?
当前,已经完成黑、红两方的下棋规则,但是还没有实现人机对战,目前只能人人对战,也就是说一个具有下棋规则的棋盘而已。

为了方便大家给我出招解惑,我先说一下自己程序的原理:

1, 32个棋子都是具体的类,并都是继承于ChessWorldBase。

棋子基类

using System;
using System.Collections;
using System.Windows.Forms;

namespace Zivsoft.Business.Chess
{
///
/// 棋子
///
internal abstract class ChessWordBase:IChess
{
public const int XBoardLength = 9;
public const int YBoardLength = 10;
public static ArrayList All = new ArrayList();


///
/// 棋盘范围内
///
///
public virtual bool Check()
{
if ((this.X > 9) || (this.X < 1))
{
return false;
}
if ((this.Y > 10) || (this.Y < 1))
{
return false;
}
return true;
}

///
/// 被吃掉
///
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
///
///
///
public bool IsRedChess
{
get;
set;
}

///
///
///
public bool IsDogFall
{
get;

set;
}

///
/// 进攻
///
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;
}

///
///
///
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
{
///
/// 象棋接口
///
interface IChess
{
///
///
///
int NextX { get; }
///
///
///
int NextY { get; }
///
///
///
///
ArrayList GetNextLocation();
///
/// 如果是红色返回为真
///
///
bool IsRedChess { get; }
///
///
///
bool IsDogFall { get; }
///
/// 是否是进攻
///
///
bool IsAttack { get; }
///
///
///
string Name { get; }
///
///
///
int X { get; }
///
///
///
int Y { get; }
///
///
///
///
///
void Move(int x, int y);
///
/// 移动
///
///
///
///
void Move(int x, int y, object obj);
}
}

来源:(https://www.sodocs.net/doc/5214567897.html,/s/blog_5ffac3550100d950.html) - C#实现的中国象棋程序开发笔记_qw宝宝_新浪博客
3. 接下来就是关键的部分,我曾在这里实现机器自动下棋,但是还是碰到一些困难

棋子移动

///
/// 被吃时调用,或吃子时调用
///
///
///
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#实现,先不讨论性能,能有人指点实现一下也不错。

如果有人有兴趣,我想把代码放在https://www.sodocs.net/doc/5214567897.html,上,其实重要不是代码了,感觉这些程序的算法和思想设计很重要,欢迎高手指点一二。:-)

相关主题