我在C#中使用自己的国际象棋引擎。实际上我在移动生成器上搜索错误,但我意识到我的实际国际象棋系统太慢了(甚至每分钟21分钟(6))。这是我的Github repository。
我使用简单的层次结构来实现碎片和实现碎片列表的板类。由于这个项目的面向对象性质,我选择不使用多维矩阵来表示板,因为每个块都有自己的位置。问题是,为了从板上获得一块,知道它的位置,它需要O(n),其中n是当前板上的件数。
在移动生成器中,我得到所有可能的移动假设一个empy板,然后我用静态类检查它们(因为一块不应该关心板状态)。我访问了一些网站,包括国际象棋编程Wiki。我看到有很多类型的电路板表示,但在我的实际状态中,我不知道哪个是最好的(性能和简单性)。我想这就是全部,我希望你能帮助我:)。
我欢迎任何关于我的项目的建议;)谢谢大家。
这是我的董事会成员:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chess_Engine___NOGUI
{
class Board
{
public const byte BoardSize = 8;
private Game game;
private List<Piece> pieceList = new List<Piece>();
private List<Piece> whitePieceList = new List<Piece>();
private List<Piece> blackPieceList = new List<Piece>();
public Board(Game game)
{
this.game = game;
}
public void AddPiece(Piece piece)
{
pieceList.Add(piece);
switch (piece.Color)
{
case PieceColor.Black:
blackPieceList.Add(piece);
break;
case PieceColor.White:
whitePieceList.Add(piece);
break;
}
}
public void RemovePiece(Piece piece)
{
pieceList.Remove(piece);
switch (piece.Color)
{
case PieceColor.Black:
blackPieceList.Remove(piece);
break;
case PieceColor.White:
whitePieceList.Remove(piece);
break;
}
}
public Square GetKingPosition(PieceColor color)
{
if (color == PieceColor.White)
foreach (Piece piece in whitePieceList)
{
if (piece.Type == PieceType.King)
return piece.square;
}
else
foreach (Piece piece in blackPieceList)
{
if (piece.Type == PieceType.King)
return piece.square;
}
throw new Exception("il re deve essere sempre presente");
}
public Piece GetPiece(Square square, PieceColor color = PieceColor.None)
{
switch (color)
{
case PieceColor.White:
{
foreach (Piece piece in whitePieceList)
{
if (piece.square == square)
return piece;
}
return new NullPiece(square);
}
case PieceColor.Black:
{
foreach (Piece piece in blackPieceList)
{
if (piece.square == square)
return piece;
}
return new NullPiece(square);
}
default:
{
foreach (Piece piece in pieceList)
{
if (piece.square == square)
return piece;
}
return new NullPiece(square);
}
}
}
public List<Piece> GetPieceList(PieceColor color)
{
switch (color)
{
case PieceColor.Black:
return blackPieceList;
case PieceColor.White:
return whitePieceList;
default:
return pieceList;
}
}
public int GetNumberOfPieces(PieceType type, PieceColor color)
{
int num = 0;
foreach (Piece piece in GetPieceList(color))
{
if (piece.Type == type)
num++;
}
return num;
}
public bool IsEmpty(Square square)
{
if ((GetPiece(square)) is NullPiece)
{
return true;
}
return false;
}
public void Equip()
{
this.Clear();
PieceFactory pieceFactory = new PieceFactory();
/*PEDONI*/
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(0, 1)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(1, 1)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(2, 1)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(3, 1)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(4, 1)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(5, 1)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(6, 1)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.White, new Square(7, 1)));
//
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(0, 6)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(1, 6)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(2, 6)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(3, 6)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(4, 6)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(5, 6)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(6, 6)));
AddPiece(pieceFactory.Create(PieceType.Pawn, PieceColor.Black, new Square(7, 6)));
/*TORRI*/
AddPiece(pieceFactory.Create(PieceType.Rook, PieceColor.White, new Square(0, 0)));
AddPiece(pieceFactory.Create(PieceType.Rook, PieceColor.White, new Square(7, 0)));
//
AddPiece(pieceFactory.Create(PieceType.Rook, PieceColor.Black, new Square(0, 7)));
AddPiece(pieceFactory.Create(PieceType.Rook, PieceColor.Black, new Square(7, 7)));
/*CAVALLI*/
AddPiece(pieceFactory.Create(PieceType.Knight, PieceColor.White, new Square(1, 0)));
AddPiece(pieceFactory.Create(PieceType.Knight, PieceColor.White, new Square(6, 0)));
//
AddPiece(pieceFactory.Create(PieceType.Knight, PieceColor.Black, new Square(1, 7)));
AddPiece(pieceFactory.Create(PieceType.Knight, PieceColor.Black, new Square(6, 7)));
/*ALFIERI*/
AddPiece(pieceFactory.Create(PieceType.Bishop, PieceColor.White, new Square(2, 0)));
AddPiece(pieceFactory.Create(PieceType.Bishop, PieceColor.White, new Square(5, 0)));
//
AddPiece(pieceFactory.Create(PieceType.Bishop, PieceColor.Black, new Square(2, 7)));
AddPiece(pieceFactory.Create(PieceType.Bishop, PieceColor.Black, new Square(5, 7)));
/*RE*/
AddPiece(pieceFactory.Create(PieceType.King, PieceColor.White, new Square(4, 0)));
//
AddPiece(pieceFactory.Create(PieceType.King, PieceColor.Black, new Square(4, 7)));
/*REGINE*/
AddPiece(pieceFactory.Create(PieceType.Queen, PieceColor.White, new Square(3, 0)));
//
AddPiece(pieceFactory.Create(PieceType.Queen, PieceColor.Black, new Square(3, 7)));
}
public void Clear()
{
pieceList.Clear();
whitePieceList.Clear();
blackPieceList.Clear();
}
public void LoadGame(FenString fen)
{
this.Clear();
foreach (Piece piece in fen.PiecePlacement)
{
AddPiece(piece);
}
}
public void MakeMove(Move move)
{
if (move.IsCastling)
{
move.RookMoved.Move(move.RookPosition);
if (move.IsShortCastling)
game.GetPlayer(move.SideMove).CanShortCastle = false;
else
game.GetPlayer(move.SideMove).CanLongCastle = false;
}
else
{
if (move.HasCaptured)
{
RemovePiece(move.PieceCaptured);
}
if (move.HasPromoted)
{
RemovePiece(move.PieceMoved);
AddPiece(PieceFactory.CreatePiece(move.PiecePromoted, move.SideMove, move.ToSquare));
}
}
// En passant target square updating
game.EnPassantSquareStack.Push(game.EnPassantSquare); // save the current target square for the unmake-move method
if (move.IsDoublePawnPush)
{
Square targetSquare;
if (move.SideMove == PieceColor.White)
targetSquare = new Square(move.ToSquare.X, 2);
else
targetSquare = new Square(move.ToSquare.X, 5);
game.EnPassantSquare = targetSquare;
}
else if (game.EnPassantSquare != null)
{
game.EnPassantSquare = null;
}
move.PieceMoved.Move(move.ToSquare); // move piece
}
public void CancelMove(Move move)
{
if (move.IsCastling)
{
move.PieceMoved.Move(move.FromSquare);
move.RookMoved.Move(move.RookMoved.startingSquare);
if (move.IsShortCastling)
game.GetPlayer(move.SideMove).CanShortCastle = true;
else
game.GetPlayer(move.SideMove).CanLongCastle = true;
}
else
{
if (move.HasCaptured)
{
AddPiece(move.PieceCaptured);
}
if (move.HasPromoted)
{
RemovePiece(GetPiece(move.ToSquare));
AddPiece(move.PieceMoved);
}
}
// En passant target square updating
game.EnPassantSquare = game.EnPassantSquareStack.Pop();
move.PieceMoved.Move(move.FromSquare);
}
}
}
答案 0 :(得分:5)
基于数组的表示通常包含每个部分及其正方形的片段列表,因此您无需遍历整个板块以找到一块(编辑:查看您的代码,您似乎已经这样做了吗?) 。比董事会代表更重要的是如何实施董事会运作。例如,测试国王是否在检查中不需要生成整个移动列表;你只需要向国王扫描一下敌人的碎片。
看了一下你的代码之后,似乎你使用了一个合法的移动生成器,并且你制作/取消制作(或许还有其他东西?)来检查合法性。后者不是必需的,这取决于你是否开始检查以及是否有任何碎片固定。
我认为你知道现在的标准是位板,因为它可以执行许多单件操作并在64位平台上得到很好的提升。我从邮箱阵列方法切换到我自己的引擎中的位板,目前perft(6)是3秒。最初它是30秒。位板使评估更加简单,因此还需要考虑。
答案 1 :(得分:3)
首先感谢您的代码链接,因为这使得回答更容易。
你所遇到的问题是,如你所提到的,你遍历所有部分以找到一块非常慢的部分。
如果你想保持实现类似于现有的和面向对象的实现,我建议你使用Dictionary
对象来存储这些部分。键可以是板上的位置(即A4),值可以是块本身。 Dictionary
是一个比List
更好的选择,因为你可以进行查找,而不必遍历所有部分。
同样在您的GetNumberOfPieces
方法中,即使在当前的实现中,也应该使用List.Count
属性。
但Zong Li的答案可能更适合国际象棋引擎。
修改强>
我更多地查看了你的代码,我注意到你正在传递Square
个对象,所以也许你可以将Dictionary
实现为Square,Piece而不是coordinate,piece的字典。