//********************************************************* // /// <summary> /// Let's the computer play against itself. Can be called asynchronously by a secondary thread. /// </summary> /// <param name="bFlash"> true to flash the moving piece</param> /// <param name="searchMode"> Search mode</param> // //********************************************************* private void PlayComputerAgainstComputerAsync(bool bFlash, SearchEngine.SearchMode searchMode) { bool bEndOfGame; int iCount; PlayComputerMoveDel delPlayComputerMove = null; UnlockBoardDel delUnlockBoard = null; SetPlayingModeDel delSetPlayingMode = null; ChessBoard chessBoard; ChessBoard.MovePosS move; int iPermCount; int iCacheHit; int iMaxDepth; bool bMoveFound; bool bMultipleThread; bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch || searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch); if (bMultipleThread) { delPlayComputerMove = new PlayComputerMoveDel(this.PlayComputerMove); delUnlockBoard = new UnlockBoardDel(this.UnlockBoard); delSetPlayingMode = new SetPlayingModeDel(this.SetPlayingMode); } iCount = 0; do { chessBoard = m_chessCtl.ChessBoard.Clone(); bMoveFound = m_chessCtl.FindBestMove(searchMode, chessBoard, out move, out iPermCount, out iCacheHit, out iMaxDepth); if (bMoveFound) { if (bMultipleThread) { bEndOfGame = (bool)Invoke(delPlayComputerMove, new object[] { bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null } ); } else { bEndOfGame = PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null); } } else { bEndOfGame = DisplayMessage(m_chessCtl.ChessBoard.CheckNextMove(), MessageModeE.Verbose); } iCount++; } while (!bEndOfGame && PlayingMode == PlayingModeE.ComputerAgainstComputer && iCount < 500); if (PlayingMode != PlayingModeE.ComputerAgainstComputer) { MessageBox.Show("Automatic play canceled"); } else { if (bMultipleThread) { Invoke(delSetPlayingMode, new object[] { PlayingModeE.PlayerAgainstPlayer }); } else { SetPlayingMode(PlayingModeE.PlayerAgainstPlayer); } if (iCount >= 500) { MessageBox.Show("Tie!"); } } if (bMultipleThread) { Invoke(delUnlockBoard); } else { UnlockBoard(); } }
//********************************************************* // /// <summary> /// Show a hint. Can be called asynchronously by a secondary thread. /// </summary> /// <param name="searchMode"> Search mode</param> // //********************************************************* private void ShowHintAsync(SearchEngine.SearchMode searchMode) { ShowHintMoveDel delShowHintMove = null; UnlockBoardDel delUnlockBoard = null; ChessBoard chessBoard; ChessBoard.MovePosS move; int iPermCount; int iCacheHit; int iMaxDepth; bool bMoveFound; bool bMultipleThread; chessBoard = m_chessCtl.ChessBoard.Clone(); bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch || searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch); if (bMultipleThread) { delShowHintMove = new ShowHintMoveDel(this.ShowHintMove); delUnlockBoard = new UnlockBoardDel(this.UnlockBoard); } bMoveFound = m_chessCtl.FindBestMove(searchMode, chessBoard, out move, out iPermCount, out iCacheHit, out iMaxDepth); if (bMultipleThread) { if (bMoveFound) { Invoke(delShowHintMove, new object[] { move, iPermCount, iMaxDepth, iCacheHit } ); } Invoke(delUnlockBoard); } else { if (bMoveFound) { ShowHintMove(move, iPermCount, iMaxDepth, iCacheHit); } UnlockBoard(); } }
//********************************************************* // /// <summary> /// Computer play a move. Can be called asynchronously by a secondary thread. /// </summary> /// <param name="bFlash"> true to use flash when moving pieces</param> /// <param name="searchMode"> Search mode</param> // //********************************************************* private void PlayComputerAsync(bool bFlash, SearchEngine.SearchMode searchMode) { PlayComputerMoveDel delPlayComputerMove = null; UnlockBoardDel delUnlockBoard = null; SetPlayingModeDel delSetPlayingMode = null; ChessBoard chessBoard; ChessBoard.MovePosS move; int iPermCount; int iCacheHit; int iMaxDepth; bool bEndOfGame; bool bMoveFound; bool bMultipleThread; chessBoard = m_chessCtl.ChessBoard.Clone(); bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch || searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch); if (bMultipleThread) { delPlayComputerMove = new PlayComputerMoveDel(this.PlayComputerMove); delUnlockBoard = new UnlockBoardDel(this.UnlockBoard); delSetPlayingMode = new SetPlayingModeDel(this.SetPlayingMode); } bMoveFound = m_chessCtl.FindBestMove(searchMode, chessBoard, out move, out iPermCount, out iCacheHit, out iMaxDepth); if (bMoveFound) { if (bMultipleThread) { bEndOfGame = (bool)Invoke(delPlayComputerMove, new object[] { bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null } ); } else { bEndOfGame = PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null); } } else { bEndOfGame = DisplayMessage(m_chessCtl.ChessBoard.CheckNextMove(), MessageModeE.CallEndGame); } if (bMultipleThread) { Invoke(delUnlockBoard); if (bEndOfGame) { Invoke(delSetPlayingMode, new object[] { PlayingModeE.PlayerAgainstPlayer } ); } } else { UnlockBoard(); if (bEndOfGame) { SetPlayingMode(PlayingModeE.PlayerAgainstPlayer); } } }
//********************************************************* // /// <summary> /// Tests the computer playing against itself. Can be called asynchronously by a secondary thread. /// </summary> /// <param name="iGameCount"> Number of games to play.</param> /// <param name="searchMode"> Search mode</param> // //********************************************************* private void TestComputerAgainstComputerAsync(int iGameCount, SearchEngine.SearchMode searchMode) { int iCount; PlayComputerMoveDel delPlayComputerMove = null; ResetBoardDel delResetBoard = null; UnlockBoardDel delUnlockBoard = null; SetPlayingModeDel delSetPlayingMode = null; TestShowResultDel delShowResultDel = null; ChessBoard chessBoard; ChessBoard.MovePosS move; int iPermCount; int iCacheHit; int iMaxDepth; int iGameIndex; int iMethod1Win = 0; int iMethod2Win = 0; DateTime dateTime; bool bMoveFound; bool bMultipleThread; bool bEven; bool bEndOfGame; IBoardEvaluation boardEvaluation1; IBoardEvaluation boardEvaluation2; ComputerPlayingStat stat; stat = new ComputerPlayingStat(); bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch || searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch); boardEvaluation1 = searchMode.m_boardEvaluationWhite; boardEvaluation2 = searchMode.m_boardEvaluationBlack; if (bMultipleThread) { delPlayComputerMove = new PlayComputerMoveDel(this.PlayComputerMove); delUnlockBoard = new UnlockBoardDel(this.UnlockBoard); delSetPlayingMode = new SetPlayingModeDel(this.SetPlayingMode); delResetBoard = new ResetBoardDel(this.ResetBoard); delShowResultDel = new TestShowResultDel(this.TestShowResult); } iGameIndex = 0; while (iGameIndex < iGameCount && !stat.m_bUserCancel) { bEven = ((iGameIndex & 1) == 0); searchMode.m_boardEvaluationWhite = bEven ? boardEvaluation1 : boardEvaluation2; searchMode.m_boardEvaluationBlack = bEven ? boardEvaluation2 : boardEvaluation1; if (bMultipleThread) { Invoke(delResetBoard); } else { ResetBoard(); } iCount = 0; do { chessBoard = m_chessCtl.ChessBoard.Clone(); dateTime = DateTime.Now; bMoveFound = m_chessCtl.FindBestMove(searchMode, chessBoard, out move, out iPermCount, out iCacheHit, out iMaxDepth); if (bMoveFound) { if ((m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.White && bEven) || (m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.Black && !bEven)) { stat.m_timeSpanMethod1 += DateTime.Now - dateTime; stat.m_iMethod1MoveCount++; } else { stat.m_timeSpanMethod2 += DateTime.Now - dateTime; stat.m_iMethod2MoveCount++; } if (bMultipleThread) { bEndOfGame = (bool)Invoke(delPlayComputerMove, new object[] { false, MessageModeE.Silent, move, iPermCount, iMaxDepth, iCacheHit, stat } ); } else { bEndOfGame = PlayComputerMove(false, MessageModeE.Silent, move, iPermCount, iMaxDepth, iCacheHit, stat); } } else { bEndOfGame = true; } iCount++; } while (!bEndOfGame && PlayingMode == PlayingModeE.ComputerAgainstComputer && iCount < 250); if (PlayingMode != PlayingModeE.ComputerAgainstComputer) { stat.m_bUserCancel = true; } else if (iCount < 250) { if (stat.m_eResult == ChessBoard.MoveResultE.Mate) { if ((m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.Black && bEven) || (m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.White && !bEven)) { iMethod1Win++; } else { iMethod2Win++; } } } iGameIndex++; } searchMode.m_boardEvaluationWhite = boardEvaluation1; searchMode.m_boardEvaluationBlack = boardEvaluation2; if (bMultipleThread) { Invoke(delShowResultDel, new object[] { iGameIndex, searchMode, stat, iMethod1Win, iMethod2Win }); Invoke(delSetPlayingMode, new object[] { PlayingModeE.PlayerAgainstPlayer }); Invoke(delUnlockBoard); } else { TestShowResult(iGameIndex, searchMode, stat, iMethod1Win, iMethod2Win); SetPlayingMode(PlayingModeE.PlayerAgainstPlayer); UnlockBoard(); } }