最近,和朋友下象棋,然後想到這個多年陳舊的代碼(這些代碼有些參考了網絡的一些帖子),曾經因為不知道如何實現人機對戰而放棄繼續研究。如今,這位朋友,給了我又重新找回來的興趣,所以來這裡請大家幫忙,如何實現人機對戰,讓電腦自動下呢?
當前,已經完成黑、紅兩方的下棋規則,但是還沒有實現人機對戰,目前只能人人對戰,也就是說一個具有下棋規則的棋盤而已。
為了方便大家給我出招解惑,我先說一下自己程序的原理:
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
}
}
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);
}
}
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();
}
目前,我在網上查了不少資料,大家都想求一個比較厲害的象棋程序,可目前我的程序是最弱智的,CPU自己不會下。有人告訴我,說需要調用一些已經大師們的下棋殘局API,而無需自己寫一套規則,我覺得也是,不然寫棋的那個人一定是下棋高手,可是我棋藝一般,但我很想寫一個比較厲害的程序,讓我的象棋以CPU的身份來挑戰那些大師,呵呵,就是這樣一個想法,目前用C#實現,先不討論性能,能有人指點實現一下也不錯。
如果有人有興趣,我想把代碼放在officelive.com上,其實重要不是代碼了,感覺這些程序的算法和思想設計很重要,歡迎高手指點一二。:-)