// return true if the other side is of enemy public bool isEnemy(Side other) { return(this.type != other.type); }
// Constructor for the new playe public Player(Side PlayerSide, Type PlayerType, Rules rules) : this(PlayerSide, PlayerType) { m_Rules = rules; }
// constructore with a given piece type and side public Piece(PieceType type, Side side) { this.m_type = type; this.m_side = side; }
// Alpha and beta search to recursively travers the tree to calculate the best move private int AlphaBeta(Side PlayerSide, int depth, int alpha, int beta) { int val; System.Windows.Forms.Application.DoEvents(); // Before we do anything, let's try the null move. It's like giving the opponent // a free shot and see if he can damage us. If he can't, we are in a better position and // can nock down him // "Adaptive" Null-move forward pruning int R = (depth > 6) ? 3 : 2; // << This is the "adaptive" bit // The rest is normal Null-move forward pruning if (depth >= 2 && !m_GameNearEnd && m_Rules.ChessGame.DoNullMovePruning) // disable null move for now { val = -AlphaBeta(m_Rules.ChessGame.EnemyPlayer(PlayerSide).PlayerSide, depth - R - 1, -beta, -beta + 1); // Try a Null Move if (val >= beta) // All the moves can be skipped, i.e. cut-off is possible { return(beta); } } // This variable is set to true when we have found at least one Principle variation node. // Principal variation (PV) node is the one where One or more of the moves will return a score greater than alpha (a PV move), but none will return a score greater than or equal to beta. bool bFoundPv = false; // Check if we have reached at the end of the search if (depth <= 0) { // Check if need to do queiscent search to avoid horizontal effect if (m_Rules.ChessGame.DoQuiescentSearch) { return(QuiescentSearch(PlayerSide, alpha, beta)); } else { return(m_Rules.Evaluate(PlayerSide)); // evaluate the current board position } } // Get all the legal moves for the current side ArrayList TotalMoves = m_Rules.GenerateAllLegalMoves(PlayerSide); // Loop through all the legal moves and get the one with best score foreach (Move move in TotalMoves) { // Now to get the effect of this move; execute this move and analyze the board m_Rules.ExecuteMove(move); // Principle variation node is found if (bFoundPv && m_Rules.ChessGame.DoPrincipleVariation) { val = -AlphaBeta(m_Rules.ChessGame.EnemyPlayer(PlayerSide).PlayerSide, depth - 1, -alpha - 1, -alpha); if ((val > alpha) && (val < beta)) // Check for failure. { val = -AlphaBeta(m_Rules.ChessGame.EnemyPlayer(PlayerSide).PlayerSide, depth - 1, -beta, -alpha); // Do normal Alpha beta pruning } } else { val = -AlphaBeta(m_Rules.ChessGame.EnemyPlayer(PlayerSide).PlayerSide, depth - 1, -beta, -alpha); // Do normal Alpha beta pruning } m_TotalMovesAnalyzed++; // Increment move counter m_Rules.UndoMove(move); // undo the move // This move will never played by the opponent, as he has already better options if (val >= beta) { return(beta); } // This is the best move for the current side (found so far) if (val > alpha) { alpha = val; bFoundPv = true; // we have found a principle variation node } } return(alpha); }