/// <summary>
        /// Evaluates a board. The number of point is greater than 0 if white is in advantage, less than 0 if black is.
        /// </summary>
        /// <param name="pBoard">           Board.</param>
        /// <param name="piPiecesCount">    Number of each pieces</param>
        /// <param name="posInfo">          Information about pieces position</param>
        /// <param name="iWhiteKingPos">    Position of the white king</param>
        /// <param name="iBlackKingPos">    Position of the black king</param>
        /// <param name="bWhiteCastle">     White has castled</param>
        /// <param name="bBlackCastle">     Black has castled</param>
        /// <param name="iMoveCountDelta">  Number of possible white move - Number of possible black move</param>
        /// <returns>
        /// Points
        /// </returns>
        public override int Points(ChessBoard.PieceE[]   pBoard,
            int[]                 piPiecesCount,
            ChessBoard.PosInfoS   posInfo,
            int                   iWhiteKingPos,
            int                   iBlackKingPos,
            bool                  bWhiteCastle,
            bool                  bBlackCastle,
            int                   iMoveCountDelta)
        {
            int     iRetVal = 0;

            for (int iIndex = 0; iIndex < piPiecesCount.Length; iIndex++) {
                iRetVal += s_piPiecePoint[iIndex] * piPiecesCount[iIndex];
            }
            if (pBoard[12] == ChessBoard.PieceE.Pawn) {
                iRetVal -= 4;
            }
            if (pBoard[52] == (ChessBoard.PieceE.Pawn | ChessBoard.PieceE.Black)) {
                iRetVal += 4;
            }
            if (bWhiteCastle) {
                iRetVal += 10;
            }
            if (bBlackCastle) {
                iRetVal -= 10;
            }
            iRetVal += iMoveCountDelta;
            return(iRetVal);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Find the best move for a player using minmax search
        /// </summary>
        /// <param name="chessBoard">       Chess board</param>
        /// <param name="searchMode">       Search mode</param>
        /// <param name="ePlayerColor">     Color doing the move</param>
        /// <param name="moveList">         Move list</param>
        /// <param name="arrIndex">         Order of evaluation of the moves</param>
        /// <param name="posInfo">          Information about pieces attacks</param>
        /// <param name="moveBest">         Best move found</param>
        /// <param name="iPermCount">       Nb of permutations evaluated</param>
        /// <param name="iCacheHit">        Nb of cache hit</param>
        /// <param name="iMaxDepth">        Maximum depth evaluated</param>
        /// <returns>
        /// true if a move has been found
        /// </returns>
        protected override bool FindBestMove(ChessBoard                 chessBoard,
                                             SearchEngine.SearchMode    searchMode,
                                             ChessBoard.PlayerColorE    ePlayerColor,
                                             List<ChessBoard.MovePosS>  moveList, 
                                             int[]                      arrIndex,
                                             ChessBoard.PosInfoS        posInfo,
                                             ref ChessBoard.MovePosS    moveBest,
                                             out int                    iPermCount,
                                             out int                    iCacheHit,
                                             out int                    iMaxDepth)
        {
            bool                bRetVal = false;
            DateTime            dtTimeOut;
            int                 iDepth;
            int                 iPermCountAtLevel;

            iPermCount  = 0;
            iCacheHit   = 0;
            if (searchMode.m_iSearchDepth == 0) {
                dtTimeOut   = DateTime.Now + TimeSpan.FromSeconds(searchMode.m_iTimeOutInSec);
                iDepth      = 0;
                do {
                    bRetVal     = FindBestMoveUsingMinMaxAtDepth(chessBoard, searchMode, ePlayerColor, moveList, arrIndex, iDepth + 1, ref moveBest, out iPermCountAtLevel);
                    iPermCount += iPermCountAtLevel;
                    iDepth++;
                } while (DateTime.Now < dtTimeOut);
                iMaxDepth = iDepth;
            } else {
                iMaxDepth = searchMode.m_iSearchDepth;
                bRetVal   = FindBestMoveUsingMinMaxAtDepth(chessBoard, searchMode, ePlayerColor, moveList, arrIndex, iMaxDepth, ref moveBest, out iPermCount);
            }
            return(bRetVal);
        }
 /// <summary>
 /// Create a new game using the specified list of moves
 /// </summary>
 /// <param name="chessBoardStarting">   Starting board or null if standard board</param>
 /// <param name="listMove">             List of moves</param>
 /// <param name="eNextMoveColor">       Color starting to play</param>
 /// <param name="strWhitePlayerName">   Name of the player playing white pieces</param>
 /// <param name="strBlackPlayerName">   Name of the player playing black pieces</param>
 /// <param name="eWhitePlayerType">     Type of player playing white pieces</param>
 /// <param name="eBlackPlayerType">     Type of player playing black pieces</param>
 /// <param name="spanPlayerWhite">      Timer for white</param>
 /// <param name="spanPlayerBlack">      Timer for black</param>
 public override void CreateGameFromMove(ChessBoard                  chessBoardStarting,
     List<ChessBoard.MovePosS>   listMove,
     ChessBoard.PlayerColorE     eNextMoveColor,
     string                      strWhitePlayerName,
     string                      strBlackPlayerName,
     PgnParser.PlayerTypeE       eWhitePlayerType,
     PgnParser.PlayerTypeE       eBlackPlayerType,
     TimeSpan                    spanPlayerWhite,
     TimeSpan                    spanPlayerBlack)
 {
     base.CreateGameFromMove(chessBoardStarting,
                             listMove,
                             eNextMoveColor,
                             strWhitePlayerName,
                             strBlackPlayerName,
                             eWhitePlayerType,
                             eBlackPlayerType,
                             spanPlayerWhite,
                             spanPlayerBlack);
     if (eWhitePlayerType == PgnParser.PlayerTypeE.Program) {
         if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
             Father.PlayingMode              = MainWindow.PlayingModeE.ComputerAgainstComputer;
         } else {
             Father.PlayingMode              = MainWindow.PlayingModeE.PlayerAgainstComputer;
             Father.m_eComputerPlayingColor  = ChessBoard.PlayerColorE.White;
         }
     } else if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
         Father.PlayingMode              = MainWindow.PlayingModeE.PlayerAgainstComputer;
         Father.m_eComputerPlayingColor  = ChessBoard.PlayerColorE.Black;
     } else {
         Father.PlayingMode = MainWindow.PlayingModeE.PlayerAgainstPlayer;
     }
     Father.SetCmdState();
 }
Exemplo n.º 4
0
        /// <summary>
        /// Compute the zobrist key for a board
        /// </summary>
        /// <param name="peBoard">      Board</param>
        public static long ComputeBoardZobristKey(ChessBoard.PieceE[] peBoard)
        {
            long    lRetVal = 0;

            for (int iIndex = 0; iIndex < 64; iIndex++) {
                lRetVal ^= s_pi64RndTable[(iIndex << 4) + (int)peBoard[iIndex]];
            }
            return(lRetVal);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Update the Zobrist key using the specified move
        /// </summary>
        /// <param name="i64ZobristKey">Zobrist key</param>
        /// <param name="iPos">         Piece position</param>
        /// <param name="eOldPiece">    Old value</param>
        /// <param name="eNewPiece">    New value</param>
        public static long UpdateZobristKey(long i64ZobristKey, int iPos, ChessBoard.PieceE eOldPiece, ChessBoard.PieceE eNewPiece)
        {
            int     iBaseIndex;

            iBaseIndex     = iPos << 4;
            i64ZobristKey ^= s_pi64RndTable[iBaseIndex + ((int)eOldPiece)] ^
                             s_pi64RndTable[iBaseIndex + ((int)eNewPiece)];
            return(i64ZobristKey);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the specified piece
        /// </summary>
        /// <param name="ePiece"></param>
        /// <returns>
        /// User control expressing the piece
        /// </returns>
        public UserControl this[ChessBoard.PieceE ePiece]
        {
            get {
                UserControl userControlRetVal;
                ChessPiece  eChessPiece;

                eChessPiece         = GetChessPieceFromPiece(ePiece);
                userControlRetVal   = (eChessPiece == ChessPiece.None) ? null : LoadPiece(eChessPiece);
                return(userControlRetVal);
            }
        }
 /// <summary>
 /// Class constructor
 /// </summary>
 /// <param name="eValidPawnPromotion">  The valid pawn promotion type</param>
 public frmQueryPawnPromotionType(ChessBoard.ValidPawnPromotionE eValidPawnPromotion)
     : this()
 {
     m_eValidPawnPromotion           = eValidPawnPromotion;
     radioButtonQueen.IsEnabled      = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Queen)  != ChessBoard.ValidPawnPromotionE.None);
     radioButtonRook.IsEnabled       = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Rook)   != ChessBoard.ValidPawnPromotionE.None);
     radioButtonBishop.IsEnabled     = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Bishop) != ChessBoard.ValidPawnPromotionE.None);
     radioButtonKnight.IsEnabled     = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Knight) != ChessBoard.ValidPawnPromotionE.None);
     radioButtonPawn.IsEnabled       = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Pawn)   != ChessBoard.ValidPawnPromotionE.None);
     if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Queen)  != ChessBoard.ValidPawnPromotionE.None) {
         radioButtonQueen.IsChecked = true;
     } else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Rook)   != ChessBoard.ValidPawnPromotionE.None) {
         radioButtonRook.IsChecked  = true;
     } else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Bishop) != ChessBoard.ValidPawnPromotionE.None) {
         radioButtonBishop.IsChecked = true;
     } else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Knight) != ChessBoard.ValidPawnPromotionE.None) {
         radioButtonKnight.IsChecked = true;
     } else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Pawn)   != ChessBoard.ValidPawnPromotionE.None) {
         radioButtonPawn.IsChecked = true;
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="eValidPawnPromotion">  Possible pawn promotions in the current context</param>
 public QueryPawnPromotionTypeEventArgs(ChessBoard.ValidPawnPromotionE eValidPawnPromotion)
 {
     ValidPawnPromotion = eValidPawnPromotion; PawnPromotionType = ChessBoard.MoveTypeE.Normal;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="iPos">     Position of the square</param>
 /// <param name="ePiece">   Piece</param>
 public QueryPieceEventArgs(int iPos, ChessBoard.PieceE ePiece)
 {
     Pos = iPos; Piece = ePiece;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Show before move is done
 /// </summary>
 /// <param name="movePos">      Position of the move</param>
 /// <param name="bFlash">       true to flash the from and destination pieces</param>
 private void ShowBeforeMove(ChessBoard.MovePosS movePos, bool bFlash)
 {
     if (bFlash) {
         FlashCell(movePos.StartPos);
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="move">     Move position</param>
 public MoveSelectedEventArgs(ChessBoard.MovePosS move)
 {
     Move = move;
 }
Exemplo n.º 12
0
        /// <summary>
        /// Add a move to the stack. All redo move are discarded
        /// </summary>
        /// <param name="movePos">  New move</param>
        public void AddMove(ChessBoard.MovePosS movePos)
        {
            int     iCount;
            int     iPos;

            iCount = Count;
            iPos   = m_iPosInList + 1;
            while (iCount != iPos) {
                m_listMovePos.RemoveAt(--iCount);
            }
            m_listMovePos.Add(movePos);
            m_iPosInList = iPos;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Transform a ChessBoard piece into a ChessPiece enum
        /// </summary>
        /// <param name="ePiece"></param>
        /// <returns></returns>
        private static ChessPiece GetChessPieceFromPiece(ChessBoard.PieceE ePiece)
        {
            ChessPiece  eRetVal;

            switch (ePiece) {
            case ChessBoard.PieceE.Pawn | ChessBoard.PieceE.White:
                eRetVal = ChessPiece.White_Pawn;
                break;
            case ChessBoard.PieceE.Knight | ChessBoard.PieceE.White:
                eRetVal = ChessPiece.White_Knight;
                break;
            case ChessBoard.PieceE.Bishop | ChessBoard.PieceE.White:
                eRetVal = ChessPiece.White_Bishop;
                break;
            case ChessBoard.PieceE.Rook | ChessBoard.PieceE.White:
                eRetVal = ChessPiece.White_Rook;
                break;
            case ChessBoard.PieceE.Queen | ChessBoard.PieceE.White:
                eRetVal = ChessPiece.White_Queen;
                break;
            case ChessBoard.PieceE.King | ChessBoard.PieceE.White:
                eRetVal = ChessPiece.White_King;
                break;
            case ChessBoard.PieceE.Pawn | ChessBoard.PieceE.Black:
                eRetVal = ChessPiece.Black_Pawn;
                break;
            case ChessBoard.PieceE.Knight | ChessBoard.PieceE.Black:
                eRetVal = ChessPiece.Black_Knight;
                break;
            case ChessBoard.PieceE.Bishop | ChessBoard.PieceE.Black:
                eRetVal = ChessPiece.Black_Bishop;
                break;
            case ChessBoard.PieceE.Rook | ChessBoard.PieceE.Black:
                eRetVal = ChessPiece.Black_Rook;
                break;
            case ChessBoard.PieceE.Queen | ChessBoard.PieceE.Black:
                eRetVal = ChessPiece.Black_Queen;
                break;
            case ChessBoard.PieceE.King | ChessBoard.PieceE.Black:
                eRetVal = ChessPiece.Black_King;
                break;
            default:
                eRetVal = ChessPiece.None;
                break;
            }
            return(eRetVal);
        }
Exemplo n.º 14
0
 /// <summary>
 /// ShowHintMove:                   Show a hint on the next move to do
 /// </summary>
 /// <param name="movePos">          Move position</param>
 public void ShowHintMove(ChessBoard.MovePosS movePos)
 {
     ShowBeforeMove(movePos, true);
     m_board.DoMoveNoLog(movePos);
     ShowAfterMove(movePos, true);
     m_board.UndoMoveNoLog(movePos);
     ShowAfterMove(movePos, false);
 }
Exemplo n.º 15
0
        /// <summary>
        /// Find the best move for a player using alpha-beta pruning or minmax search
        /// </summary>
        /// <param name="searchMode">       Search mode</param>
        /// <param name="chessBoard">       Chess board to use. Null to use the base one</param>
        /// <param name="moveBest">         Best move found</param>
        /// <param name="iPermCount">       Total permutation evaluated</param>
        /// <param name="iCacheHit">        Number of moves found in the translation table cache</param>
        /// <param name="iMaxDepth">        Maximum depth evaluated</param>
        /// <returns>
        /// true if a move has been found
        /// </returns>
        public bool FindBestMove(SearchEngine.SearchMode searchMode, ChessBoard chessBoard, out ChessBoard.MovePosS moveBest, out int iPermCount, out int iCacheHit, out int iMaxDepth)
        {
            bool    bRetVal;
            bool    bUseBook;

            bUseBook = ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseBook) != 0);
            if (bUseBook && FindBookMove(searchMode, out moveBest)) {
                iPermCount = -1;
                iCacheHit  = -1;
                iMaxDepth  = 0;
                bRetVal    = true;
            } else {
                if (chessBoard == null) {
                    chessBoard = m_board;
                }
                m_dateTimeStartSearching = DateTime.Now;
                bRetVal                  = chessBoard.FindBestMove(searchMode, m_board.NextMoveColor, out moveBest, out iPermCount, out iCacheHit, out iMaxDepth);
                m_timeSpanLastSearch     = DateTime.Now - m_dateTimeStartSearching;
            }
            return(bRetVal);
        }
Exemplo n.º 16
0
 /// <summary>
 /// Set the piece in a case. Can only be used in design mode.
 /// </summary>
 public void SetCaseValue(int iPos, ChessBoard.PieceE ePiece)
 {
     if (BoardDesignMode) {
         m_board[iPos] = ePiece;
         RefreshCell(iPos);
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// ShowHint:                       Find and show a hint on the next move to do
        /// </summary>
        /// <param name="searchMode">       Search mode</param>
        /// <param name="movePos">          Move position found</param>
        /// <param name="iPermCount">       Permutation count</param>
        /// <param name="iCacheHit">        Cache hit</param>
        /// <returns>
        /// true if a hint has been shown
        /// </returns>
        public bool ShowHint(SearchEngine.SearchMode searchMode, out ChessBoard.MovePosS movePos, out int iPermCount, out int iCacheHit)
        {
            bool    bRetVal;
            int     iMaxDepth;

            if (FindBestMove(searchMode, null, out movePos, out iPermCount, out iCacheHit, out iMaxDepth)) {
                ShowHintMove(movePos);
                bRetVal = true;
            } else {
                bRetVal = false;
            }
            return(bRetVal);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Search trace
 /// </summary>
 /// <param name="iDepth">       Search depth</param>
 /// <param name="ePlayerColor"> Color who play</param>
 /// <param name="movePos">      Move position</param>
 /// <param name="iPts">         Points</param>
 void SearchEngine.ITrace.TraceSearch(int iDepth, ChessBoard.PlayerColorE ePlayerColor, ChessBoard.MovePosS movePos, int iPts)
 {
     //
 }
Exemplo n.º 19
0
 /// <summary>
 /// Class Ctor
 /// </summary>
 public ChessBoardControl()
 {
     InitializeComponent();
     m_iFlashing                 = 0;
     m_board                     = new ChessBoard(this);
     if (!m_board.ReadBook("book.bin")) {
         m_board.ReadBookFromResource("SrcChess2.Book.bin");
     }
     m_ptSelectedCell            = new IntPoint(-1, -1);
     m_bAutoSelection            = true;
     m_gameTimer                 = new GameTimer();
     m_gameTimer.Enabled         = false;
     m_gameTimer.Reset(m_board.NextMoveColor);
     m_strWhitePlayerName        = "Player 1";
     m_strBlackPlayerName        = "Player 2";
     m_eWhitePlayerType          = PgnParser.PlayerTypeE.Human;
     m_eBlackPlayerType          = PgnParser.PlayerTypeE.Human;
     InitCell();
 }
Exemplo n.º 20
0
 /// <summary>
 /// Display a message related to the MoveStateE
 /// </summary>
 /// <param name="eMoveResult">  Move result</param>
 /// <param name="eMessageMode"> Message mode</param>
 /// <returns>
 /// true if it's the end of the game. false if not
 /// </returns>
 private bool DisplayMessage(ChessBoard.MoveResultE eMoveResult, MessageModeE eMessageMode) {
     bool    bRetVal;
     string  strOpponent;
     
     if (m_ePlayingMode == PlayingModeE.PlayerAgainstComputer) {
         if (m_chessCtl.ChessBoard.NextMoveColor == m_eComputerPlayingColor) {
             strOpponent = "Computer is ";
         } else {
             strOpponent = "You are ";
         }
     } else {
         strOpponent = (m_chessCtl.ChessBoard.NextMoveColor == ChessBoard.PlayerColorE.White) ? "White player is " : "Black player is ";
     }
     switch(eMoveResult) {
     case ChessBoard.MoveResultE.NoRepeat:
         bRetVal = false;
         break;
     case ChessBoard.MoveResultE.TieNoMove:
         if (eMessageMode != MessageModeE.Silent) {
             MessageBox.Show("Draw. " + strOpponent + "unable to move.");
         }
         bRetVal = true;
         break;
     case ChessBoard.MoveResultE.TieNoMatePossible:
         if (eMessageMode != MessageModeE.Silent) {
             MessageBox.Show("Draw. Not enough pieces to make a checkmate.");
         }
         bRetVal = true;
         break;
     case ChessBoard.MoveResultE.ThreeFoldRepeat:
         if (eMessageMode != MessageModeE.Silent) {
             MessageBox.Show("Draw. 3 times the same board.");
         }
         bRetVal = true;
         break;
     case ChessBoard.MoveResultE.FiftyRuleRepeat:
         if (eMessageMode != MessageModeE.Silent) {
             MessageBox.Show("Draw. 50 moves without moving a pawn or eating a piece.");
         }
         bRetVal = true;
         break;
     case ChessBoard.MoveResultE.Check:
         if (eMessageMode == MessageModeE.Verbose) {
             MessageBox.Show(strOpponent + "in check.");
         }
         bRetVal = false;
         break;
     case ChessBoard.MoveResultE.Mate:
         if (eMessageMode != MessageModeE.Silent) {
             MessageBox.Show(strOpponent + "checkmate.");
         }
         bRetVal = true;
         break;
     default:
         bRetVal = false;
         break;
     }
     return(bRetVal);
 }
Exemplo n.º 21
0
 /// <summary>
 /// Create a new game using the specified list of moves
 /// </summary>
 /// <param name="chessBoardStarting">   Starting board or null if standard board</param>
 /// <param name="listMove">             List of moves</param>
 /// <param name="eNextMoveColor">       Color starting to play</param>
 /// <param name="strWhitePlayerName">   Name of the player playing white pieces</param>
 /// <param name="strBlackPlayerName">   Name of the player playing black pieces</param>
 /// <param name="eWhitePlayerType">     Type of player playing white pieces</param>
 /// <param name="eBlackPlayerType">     Type of player playing black pieces</param>
 /// <param name="spanPlayerWhite">      Timer for white</param>
 /// <param name="spanPlayerBlack">      Timer for black</param>
 public virtual void CreateGameFromMove(ChessBoard                   chessBoardStarting,
                                        List<ChessBoard.MovePosS>    listMove,
                                        ChessBoard.PlayerColorE      eNextMoveColor,
                                        string                       strWhitePlayerName,
                                        string                       strBlackPlayerName,
                                        PgnParser.PlayerTypeE        eWhitePlayerType,
                                        PgnParser.PlayerTypeE        eBlackPlayerType,
                                        TimeSpan                     spanPlayerWhite,
                                        TimeSpan                     spanPlayerBlack)
 {
     m_board.CreateGameFromMove(chessBoardStarting,
                                listMove,
                                eNextMoveColor);
     if (m_moveListUI != null) {
         m_moveListUI.Reset(m_board);
     }
     WhitePlayerName = strWhitePlayerName;
     BlackPlayerName = strBlackPlayerName;
     WhitePlayerType = eWhitePlayerType;
     BlackPlayerType = eBlackPlayerType;
     OnUpdateCmdState(System.EventArgs.Empty);
     m_gameTimer.ResetTo(m_board.NextMoveColor,
                         spanPlayerWhite.Ticks,
                         spanPlayerBlack.Ticks);
     m_gameTimer.Enabled = true;
     Refresh(false); // bForceRefresh
 }
        /// <summary>
        /// Set the chess piece control
        /// </summary>
        /// <param name="iPos">         Piece position</param>
        /// <param name="ePiece">       Piece</param>
        private void SetPieceControl(int iPos, ChessBoard.PieceE ePiece)
        {
            Border      border;
            Control     controlPiece;
            Label       label;

            border              = m_arrBorder[iPos];
            controlPiece        = m_pieceSet[ePiece];
            if (controlPiece != null) {
                controlPiece.Margin = new Thickness(1);
            }
            m_arrPiece[iPos]    = ePiece;
            if (controlPiece == null) { // && m_bDesignMode) {
                label                               = new Label();
                label.Content                       = " ";
                label.FontSize                      = 0.1;
                controlPiece                        = label;
                controlPiece.HorizontalAlignment    = System.Windows.HorizontalAlignment.Stretch;
                controlPiece.VerticalAlignment      = System.Windows.VerticalAlignment.Stretch;
                //controlPiece.Background             = Brushes.Red;
            }
            border.Child = controlPiece;
        }
Exemplo n.º 23
0
        /// <summary>
        /// Play the specified move
        /// </summary>
        /// <param name="movePos">      Position of the move</param>
        /// <param name="bFlash">       true to flash the from and destination pieces</param>
        /// <param name="iPermCount">   Permutation count</param>
        /// <param name="iDepth">       Maximum depth use to find the move</param>
        /// <param name="iCacheHit">    Number of permutation found in cache</param>
        /// <returns>
        /// NoRepeat, FiftyRuleRepeat, ThreeFoldRepeat, Tie, Check, Mate
        /// </returns>
        public ChessBoard.MoveResultE DoMove(ChessBoard.MovePosS movePos, bool bFlash, int iPermCount, int iDepth, int iCacheHit)
        {
            ChessBoard.MoveResultE eRetVal;

            if (m_iFlashing == 0) {
                ShowBeforeMove(movePos, bFlash);
                eRetVal = m_board.DoMove(movePos);
                ShowAfterMove(movePos, bFlash);
                if (m_moveListUI != null) {
                    m_moveListUI.NewMoveDone(iPermCount, iDepth, iCacheHit);
                }
                OnUpdateCmdState(System.EventArgs.Empty);
                m_gameTimer.PlayerColor = m_board.NextMoveColor;
                m_gameTimer.Enabled     = (eRetVal == ChessBoard.MoveResultE.NoRepeat || eRetVal == ChessBoard.MoveResultE.Check);
            } else {
                eRetVal = SrcChess2.ChessBoard.MoveResultE.NoRepeat;
            }
            return(eRetVal);
        }
Exemplo n.º 24
0
 /// <summary>
 /// Remove all moves from the list
 /// </summary>
 /// <param name="chessBoard">       Starting chess board</param>
 void ChessBoardControl.IMoveListUI.Reset(ChessBoard chessBoard) {
     m_moveViewer.Reset(chessBoard);
 }
Exemplo n.º 25
0
        /// <summary>
        /// Find a move from the opening book
        /// </summary>
        /// <param name="searchMode">       Search mode</param>
        /// <param name="move">             Found move</param>
        /// <returns>
        /// true if succeed, false if no move found in book
        /// </returns>
        public bool FindBookMove(SearchEngine.SearchMode searchMode, out ChessBoard.MovePosS move)
        {
            bool                    bRetVal;
            ChessBoard.MovePosS[]   arrMoves;

            if (!m_board.StandardInitialBoard) {
                move.OriginalPiece  = ChessBoard.PieceE.None;
                move.StartPos       = 255;
                move.EndPos         = 255;
                move.Type           = ChessBoard.MoveTypeE.Normal;
                bRetVal             = false;
            } else {
                arrMoves = MoveList;
                bRetVal  = m_board.FindBookMove(searchMode, m_board.NextMoveColor, arrMoves, out move);
            }
            return(bRetVal);
        }
Exemplo n.º 26
0
        /// <summary>
        /// Show after move is done
        /// </summary>
        /// <param name="movePos">      Position of the move</param>
        /// <param name="bFlash">       true to flash the from and destination pieces</param>
        private void ShowAfterMove(ChessBoard.MovePosS movePos, bool bFlash)
        {
            int[]       arrPosToUpdate;

            RefreshCell(movePos.StartPos);
            RefreshCell(movePos.EndPos);
            if (bFlash) {
                FlashCell(movePos.EndPos);
            }
            arrPosToUpdate = GetPosToUpdate(movePos);
            foreach (int iPos in arrPosToUpdate) {
                if (bFlash) {
                    FlashCell(iPos);
                }
                RefreshCell(iPos);
            }
        }
        /// <summary>
        /// Refresh the specified cell
        /// </summary>
        /// <param name="arrNewPieces"> New pieces value</param>
        /// <param name="iPos">         Piece position</param>
        /// <param name="bFullRefresh"> true to refresh even if its the same piece</param>
        private void RefreshCell(ChessBoard.PieceE[] arrNewPieces, int iPos, bool bFullRefresh)
        {
            ChessBoard.PieceE   ePiece;

            ePiece = arrNewPieces[iPos];
            if (ePiece != m_arrPiece[iPos] || bFullRefresh) {
                SetPieceControl(iPos, ePiece);
            }
        }
Exemplo n.º 28
0
 /// <summary>
 /// Play the computer move found by the search.
 /// </summary>
 /// <param name="bFlash">       true to flash moving position</param>
 /// <param name="eMessageMode"> Which message to show</param>
 /// <param name="move">         Best move</param>
 /// <param name="iPermCount">   Permutation count</param>
 /// <param name="iDepth">       Depth of the search</param>
 /// <param name="iCacheHit">    Number of moves found in the translation table cache</param>
 /// <param name="stat">         Playing stat. Can be null</param>
 /// <returns>
 /// true if end of game, false if not
 /// </returns>
 private bool PlayComputerMove(bool bFlash, MessageModeE eMessageMode, ChessBoard.MovePosS move, int iPermCount, int iDepth, int iCacheHit, ComputerPlayingStat stat) {
     bool                        bRetVal;
     ChessBoard.MoveResultE      eResult;
     ChessBoard.PlayerColorE     eColorToPlay;
                                 
     eColorToPlay    = m_chessCtl.NextMoveColor;
     eResult         = m_chessCtl.DoMove(move,
                                         bFlash,
                                         iPermCount,
                                         iDepth,
                                         iCacheHit);
     if (stat != null) {
         stat.m_eResult = eResult;
     }
     bRetVal = DisplayMessage(eResult, eMessageMode);
     return(bRetVal);
 }
Exemplo n.º 29
0
        /// <summary>
        /// Get additional position to update when doing or undoing a special move
        /// </summary>
        /// <param name="movePos">      Position of the move</param>
        /// <returns>
        /// Array of position to undo
        /// </returns>
        private int[] GetPosToUpdate(ChessBoard.MovePosS movePos)
        {
            List<int>       arrRetVal = new List<int>(2);

            if ((movePos.Type & ChessBoard.MoveTypeE.MoveTypeMask) == ChessBoard.MoveTypeE.Castle) {
                switch(movePos.EndPos) {
                case 1:
                    arrRetVal.Add(0);
                    arrRetVal.Add(2);
                    break;
                case 5:
                    arrRetVal.Add(7);
                    arrRetVal.Add(4);
                    break;
                case 57:
                    arrRetVal.Add(56);
                    arrRetVal.Add(58);
                    break;
                case 61:
                    arrRetVal.Add(63);
                    arrRetVal.Add(60);
                    break;
                default:
                    MessageBox.Show("Oops!");
                    break;
                }
            } else if ((movePos.Type & ChessBoard.MoveTypeE.MoveTypeMask) == ChessBoard.MoveTypeE.EnPassant) {
                arrRetVal.Add((movePos.StartPos & 56) + (movePos.EndPos & 7));
            }
            return(arrRetVal.ToArray());
        }
Exemplo n.º 30
0
        /// <summary>
        /// Set the chess piece control
        /// </summary>
        /// <param name="iBoardPos">    Board position</param>
        /// <param name="ePiece">       Piece</param>
        private void SetPieceControl(int iBoardPos, ChessBoard.PieceE ePiece)
        {
            Border      border;
            UserControl userControlPiece;

            border              = m_arrBorder[iBoardPos];
            userControlPiece    = m_pieceSet[ePiece];
            if (userControlPiece != null) {
                userControlPiece.Margin  = (border.BorderThickness.Top == 0) ? new Thickness(3) : new Thickness(1);
            }
            m_arrPiece[iBoardPos]   = ePiece;
            border.Child            = userControlPiece;
        }