最近,和朋友下象棋,然後想到這個多年陳舊的代碼(這些代碼有些參考了網絡的一些帖子),曾經因為不知道如何實現人機對戰而放棄繼續研究。如今,這位朋友,給了我又重新找回來的興趣,所以來這裡請大家幫忙,如何實現人機對戰,讓電腦自動下呢?
當前,已經完成黑、紅兩方的下棋規則,但是還沒有實現人機對戰,目前只能人人對戰,也就是說一個具有下棋規則的棋盤而已。
為了方便大家給我出招解惑,我先說一下自己程序的原理:
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
}
}