public static bool MovePiece(ChessBoard board, byte sourceIndex, byte destinationIndex) { ChessPiece piece = board.pieces[sourceIndex]; //Do the actual move ChessEngine.MoveContent(board, sourceIndex, destinationIndex, ChessPieceType.Queen); PieceValidMoves.GenerateValidMoves(board); //If there is a check in place and still check if (piece.PieceColor == ChessPieceColor.White) { if (board.whiteInCheck) { //Invalid Move -> undo last move ChessEngine.MoveContent(board, destinationIndex, sourceIndex, ChessPieceType.Queen); PieceValidMoves.GenerateValidMoves(board); return(false); } } else if (board.blackInCheck) { //Invalid Move -> undo last move ChessEngine.MoveContent(board, destinationIndex, sourceIndex, ChessPieceType.Queen); PieceValidMoves.GenerateValidMoves(board); return(false); } return(true); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { boardInfo = new ChessBoard(); boardInfo.WhoseMove = ChessPieceColor.White; boardInfo.pieces = new ChessPiece[] { new ChessPiece(ChessPieceType.Rook, 0, true), new ChessPiece(ChessPieceType.Knight, 0, true), new ChessPiece(ChessPieceType.Bishop, 0, true), new ChessPiece(ChessPieceType.Queen, 0, true), new ChessPiece(ChessPieceType.King, 0, true), new ChessPiece(ChessPieceType.Bishop, 1, true), new ChessPiece(ChessPieceType.Knight, 1, true), new ChessPiece(ChessPieceType.Rook, 1, true), new ChessPiece(ChessPieceType.Pawn, 0, true), new ChessPiece(ChessPieceType.Pawn, 1, true), new ChessPiece(ChessPieceType.Pawn, 2, true), new ChessPiece(ChessPieceType.Pawn, 3, true), new ChessPiece(ChessPieceType.Pawn, 4, true), new ChessPiece(ChessPieceType.Pawn, 5, true), new ChessPiece(ChessPieceType.Pawn, 6, true), new ChessPiece(ChessPieceType.Pawn, 7, true), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, new ChessPiece(ChessPieceType.Pawn, 0, false), new ChessPiece(ChessPieceType.Pawn, 1, false), new ChessPiece(ChessPieceType.Pawn, 2, false), new ChessPiece(ChessPieceType.Pawn, 3, false), new ChessPiece(ChessPieceType.Pawn, 4, false), new ChessPiece(ChessPieceType.Pawn, 5, false), new ChessPiece(ChessPieceType.Pawn, 6, false), new ChessPiece(ChessPieceType.Pawn, 7, false), new ChessPiece(ChessPieceType.Rook, 0, false), new ChessPiece(ChessPieceType.Knight, 0, false), new ChessPiece(ChessPieceType.Bishop, 0, false), new ChessPiece(ChessPieceType.Queen, 0, false), new ChessPiece(ChessPieceType.King, 0, false), new ChessPiece(ChessPieceType.Bishop, 1, false), new ChessPiece(ChessPieceType.Knight, 1, false), new ChessPiece(ChessPieceType.Rook, 1, false), }; PieceMoves.InitiateChessPieceMotion(); PieceValidMoves.GenerateValidMoves(boardInfo); Evaluation.EvaluateBoardScore(boardInfo); boardHistory.Add(new ChessBoard(boardInfo)); base.Initialize(); }
public static void EngineMove(ChessBoard board) { if (CheckForMate(board.WhoseMove, board)) { return; } System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //If there is no playbook move search for the best move MoveContent bestMove = SearchMove.AlphaBetaRoot(board, Constants.ply); ChessEngine.MoveContent(board, bestMove.MovingPiecePrimary.SrcPosition, bestMove.MovingPiecePrimary.DstPosition, ChessPieceType.Queen); PieceValidMoves.GenerateValidMoves(board); Evaluation.EvaluateBoardScore(board); System.Diagnostics.Debug.WriteLine("Engine Move Time: " + watch.ElapsedTicks); }