現在、C# でチェス エンジンを開発しようとしています。前のスレッドで詳細な回答をいただいたおかげで、現在、ゲーム構造にビットボード システムを適用する方法を研究しています。原則として、私はオブジェクト指向設計をこの新しいエンジンの概念に適用しようとしていますが、まだ答えられていない質問がいくつかあります。
UInt64 フィールドに依存するビットボード構造を実装してその概念を抽象化し、おそらく GetFirstBit() または Shift(..) または PopCount(..) などのメソッドを提供したいと思いますが、それがパフォーマンスにどのように影響するかわかりませんそしてメモリ割り当て。参照コピーのおかげでパフォーマンスを向上させるには、クラスの方が優れているでしょうか。それとも、非常に小さなオブジェクトの場合、ヒープは物事を複雑にするだけでしょうか?
通常の配列のように単一ビットで入力するインデクサーを実装することもできますが、それはリソースの無駄でしょうか、それとも (チェス エンジンの場合) 良い考えでしょうか?
プロジェクトへの変更を最小限に抑えようとしていますが、すべてのピース階層と Move および Square クラスが取り除かれ、それ以上使用されないことに気付きました... その設計を放棄するか、それらのクラスを再利用できますか?何とかして?
これは、エンジンに実装したいもののプロトタイプです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chess_Engine___NOGUI
{
public struct BitBoard
{
public UInt64 bitBoard;
public BitBoard(UInt64 board)
{
bitBoard = board;
}
public static implicit operator BitBoard(UInt64 board)
{
return new BitBoard(board);
}
public static implicit operator UInt64(BitBoard board)
{
return board.bitBoard;
}
public static BitBoard operator <<(BitBoard board, int shift)
{
return board.bitBoard << shift;
}
public static BitBoard operator >>(BitBoard board, int shift)
{
return board.bitBoard >> shift;
}
public static BitBoard operator &(BitBoard a, BitBoard b)
{
return a.bitBoard & b.bitBoard;
}
public static BitBoard operator |(BitBoard a, BitBoard b)
{
return a.bitBoard | b.bitBoard;
}
public static BitBoard operator ^(BitBoard a, BitBoard b)
{
return a.bitBoard ^ b.bitBoard;
}
public static BitBoard operator ~(BitBoard a)
{
return ~a.bitBoard;
}
}
}
そして、ここで私が保存したいクラス...
これは私のMoveクラスです:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chess_Engine___NOGUI
{
class NullMove : Move
{
public NullMove()
: base(null, null, null)
{
}
}
class Move
{
public string Algebraic
{
get
{
return ToAlgebraic();
}
} // JUST FOR DEBUG
public Square FromSquare { get; set; }
public Square ToSquare { get; set; }
public Piece PieceMoved { get; set; }
public Piece PieceCaptured { get; set; }
public PieceType PiecePromoted { get; set; }
public bool HasPromoted
{
get
{
return PiecePromoted != PieceType.None;
}
}
public bool IsEnpassant { get; set; }
public bool HasCaptured
{
get
{
if (PieceCaptured != null)
return true;
else
return false;
}
}
public bool IsCastling
{
get
{
return IsLongCastling || IsShortCastling;
}
}
public bool IsLongCastling
{
get
{
if (PieceMoved is King)
{
if (FromSquare.X - ToSquare.X == 2)
return true;
else
return false;
}
else
{
return false;
}
}
}
public bool IsShortCastling
{
get
{
if (PieceMoved is King)
{
if (FromSquare.X - ToSquare.X == -2)
return true;
else
return false;
}
else
{
return false;
}
}
}
public bool IsCheck { get; set; }
public bool IsCheckMate { get; set; }
public bool IsDoublePawnPush
{
get
{
if (PieceMoved.Type == PieceType.Pawn)
if (!HasCaptured)
if (ToSquare.X == FromSquare.X)
if (SideMove == PieceColor.White)
{
if (ToSquare.Y - FromSquare.Y == 2)
return true;
}
else
{
if (ToSquare.Y - FromSquare.Y == -2)
return true;
}
return false;
}
}
public PieceColor SideMove
{
get
{
return PieceMoved.Color;
}
}
public Piece RookMoved { get; set; }
public Square KingPosition { get; set; }
public Square RookPosition { get; set; }
public float Score { get; set; }
public Move(Square fromSquare, Square toSquare, Piece pieceMoved, PieceType piecePromoted = PieceType.None)
{
this.FromSquare = fromSquare;
this.ToSquare = toSquare;
this.PieceMoved = pieceMoved;
this.PiecePromoted = piecePromoted;
}
public static bool operator ==(Move a, Move b)
{
return a.Equals(b);
}
public static bool operator !=(Move a, Move b)
{
return !a.Equals(b);
}
public override bool Equals(object other)
{
if (other is Move)
{
Move compare = (Move)other;
return (this.FromSquare == compare.FromSquare && this.ToSquare == compare.ToSquare);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public string ToAlgebraic()
{
StringBuilder algebraic = new StringBuilder();
if (IsCastling) // se e` una mossa di arrocco
{
if (IsShortCastling)
algebraic.Append("O-O"); // arrocco corto
else
algebraic.Append("O-O-O"); // arrocco lungo
}
else
{
algebraic.Append(FromSquare.ToAlgebraic());
if (HasCaptured)
algebraic.Append("x"); // cattura
algebraic.Append(ToSquare.ToAlgebraic());
}
if (HasPromoted)
algebraic.Append(PiecePromoted.GetInitial());
if (IsCheck)
if (IsCheckMate)
algebraic.Append("#"); // scacco matto
else
algebraic.Append("+"); // scacco
return algebraic.ToString();
}
}
}
これが私のSquareクラスです:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chess_Engine___NOGUI
{
sealed class Square
{
public int X { get; set; }
public int Y { get; set; }
public Square(int x, int y)
{
this.X = x;
this.Y = y;
}
public static implicit operator Square(string str)
{
// converte la notazione algebrica (es. a1) in coordinate decimali
str = str.ToLower(); // converte la stringa in minuscolo
int x = (int)(str[0] - 'a');
int y = (int)(str[1] - '1');
return new Square(x, y);
}
public static bool operator ==(Square a, Square b)
{
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
if (((object)a == null) || ((object)b == null))
{
return false;
}
if (a is Square)
{
Square compare = (Square)b;
return (a.X == compare.X && a.Y == compare.Y);
}
else
{
return false;
}
}
public static bool operator !=(Square a, Square b)
{
return !(a == b);
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public string ToAlgebraic()
{
string str = "";
str += (char)(this.X + 97);
str += (this.Y + 1).ToString();
return str;
}
}
}
ここに私の抽象的なピースクラスがあります:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chess_Engine___NOGUI
{
public enum PieceType { None, Pawn, Knight, Bishop, Rook, Queen, King }
public enum PieceColor { None, White, Black }
public static class Extensions
{
public static PieceColor GetOpposite(this PieceColor color)
{
if (color == PieceColor.White)
return PieceColor.Black;
if (color == PieceColor.Black)
return PieceColor.White;
else
return PieceColor.None;
}
public static char GetInitial(this PieceType type)
{
switch (type)
{
case PieceType.Bishop:
return 'B';
case PieceType.King:
return 'K';
case PieceType.Knight:
return 'N';
case PieceType.Pawn:
return 'P';
case PieceType.Queen:
return 'Q';
case PieceType.Rook:
return 'R';
default:
return ' ';
}
}
}
abstract class Piece
{
public char Notation { get; set; }
protected List<Move> movesList;
public Square startingSquare { get; set; }
public Square square { get; protected set; }
public Square lastSquare { get; set; }
public PieceType Type { get; set; }
public PieceColor Color { get; set; }
public virtual bool AlreadyBeenMoved
{
get
{
return square != startingSquare;
}
}
public Piece(Square square, PieceColor color)
{
this.startingSquare = square;
this.square = square;
this.lastSquare = square;
this.Color = color;
this.movesList = new List<Move>();
}
public void Move(Square destination)
{
square = destination; // aggiorna la posizione attuale
}
public bool ShouldUpdateMoves()
{
if (lastSquare == square) // se il pezzo non si e` mosso
{
if (movesList.Count > 0)
return false;
}
else
{
lastSquare = square;
movesList.Clear();
}
return true;
}
public abstract List<Move> GetMoves();
}
}
ここで正しい答えを得るには、速度の最適化と優れたオブジェクト指向設計が非常に重要な要素であることを強調したいと思います。
ありがとうございます :)