Exemplo n.º 1
0
        public void doVerifyThreeFoldRepetition(baseBoard ourBoard)
        {
            // Play some useless knight moves. Once the same board situation has occured three
            // times, the game is a draw.
            for (int n = 0; n < 8; n++)
            {
                Assert.AreEqual(gameStatus.inProgress, ourBoard.getGameStatus(pieceColour.white), "Game declared drawn at move " + n.ToString());

                switch (n % 4)
                {
                    case 0:
                        // Play Nc3
                        ourBoard.doMove(new move(ourBoard[1, 0], ourBoard[2, 2]));
                        break;
                    case 1:
                        // Play Nc6
                        ourBoard.doMove(new move(ourBoard[1, 7], ourBoard[2, 5]));
                        break;
                    case 2:
                        // And move knights back again.
                        // Nb1
                        ourBoard.doMove(new move(ourBoard[2, 2], ourBoard[1, 0]));
                        break;
                    case 3:
                        // nb8
                        ourBoard.doMove(new move(ourBoard[2, 5], ourBoard[1, 7]));
                        break;
                    default:
                        throw new ArgumentException();
                }
            }
            Assert.AreEqual(gameStatus.drawn, ourBoard.getGameStatus(pieceColour.white));
        }
Exemplo n.º 2
0
        private void gameThread()
        {
            // Make a new AppDomain for each player.
            appDomainWhite = createAppDomain(white.assemblyPath, "Tournament game AppDomain (white player)");
            appDomainBlack = createAppDomain(black.assemblyPath, "Tournament game AppDomain (black player)");

            colToMove = pieceColour.white;

            // Make new players
            try
            {
                gameBoardWhite = white.makeNewBoard(appDomainWhite);
            }
            catch (Exception e)
            {
                throw new playerException(white, pieceColour.white, "While attempting to make board: ", e);
            }

            try
            {
                gameBoardBlack = black.makeNewBoard(appDomainBlack);
            }
            catch (Exception e)
            {
                throw new playerException(black, pieceColour.black, "While attempting to make board: ", e);
            }

            // Fill in our board HTML
            TextWriter ourTextWriter = new StringWriter();
            HtmlTextWriter ourHtmlWriter = new HtmlTextWriter(ourTextWriter);
            utils.makeTableAndEscapeContents(gameBoardWhite).RenderControl(ourHtmlWriter);
            boardRepresentation = ourTextWriter.ToString();

            // Initialise player times (ms)
            white.timeLeft = black.timeLeft = 5*60*1000;
            isRunning = true;

            while (true)
            {
                // Do some quick sanity checks to ensure that both AIs have consistant views of
                // the situation
                if (gameBoardWhite.colToMove != colToMove)
                    throw new playerException(white, pieceColour.white, "Player has incorrect the 'next player' value");

                if (gameBoardBlack.colToMove != colToMove)
                    throw new playerException(black, pieceColour.black, "Player has incorrect the 'next player' value");

                // Okay, checks are ok, so lets play a move!
                baseBoard boardToMove = colToMove == pieceColour.white ? gameBoardWhite : gameBoardBlack;
                contender player = colToMove == pieceColour.white ? white : black;

                int timeLeft = colToMove == pieceColour.white ? white.timeLeft : black.timeLeft;

                move bestMove;
                try
                {
                    moveWithTimeout mwo = new moveWithTimeout();
                    boardToMove.timeLeftMS = timeLeft;
                    lineAndScore bestLine = mwo.findBestMoveWithTimeout(boardToMove, timeLeft);
                    bestMove = bestLine.line[0];
                    moveList.Add(bestMove);
                }
                catch (Exception e)
                {
                    throw new playerException(player, colToMove, "During move search: ", e);
                }

                // Now play the move on both boards
                foreach (baseBoard thisContener in new[] { gameBoardBlack, gameBoardWhite })
                {
                    try
                    {
                        thisContener.doMove(bestMove);
                    }
                    catch (Exception e)
                    {
                        contender culprit = thisContener == gameBoardBlack ? black : white;
                        pieceColour culpritCol = thisContener == gameBoardBlack ? pieceColour.black : pieceColour.white;
                        throw new playerException(culprit, culpritCol, "While playing move as " + culpritCol + ": ", e);
                    }
                }

                pieceColour colJustMoved = colToMove;
                colToMove = colToMove == pieceColour.white ? pieceColour.black : pieceColour.white;

                // Extract a graphical representation of the board so we don't need to query it
                // while the game is running
                TextWriter ourTextWriter2 = new StringWriter();
                HtmlTextWriter ourHtmlWriter2 = new HtmlTextWriter(ourTextWriter2);
                utils.makeTableAndEscapeContents(gameBoardWhite).RenderControl(ourHtmlWriter2);
                boardRepresentation = ourTextWriter2.ToString();

                // Check that game is still in progrss
                gameStatus statusWhite;
                gameStatus statusBlack;

                try
                {
                    statusWhite = gameBoardWhite.getGameStatus(colJustMoved);
                }
                catch (Exception e)
                {
                    throw new playerException(white, pieceColour.white, "While evaluating game", e);
                }

                try
                {
                    statusBlack = gameBoardBlack.getGameStatus(colJustMoved);
                }
                catch (Exception e)
                {
                    throw new playerException(black, pieceColour.black, "While evaluating game ", e);
                }

                if (statusBlack != statusWhite)
                {
                    // This could be white or black's fault - we can't tell for sure here.
                    throw new playerException(white, pieceColour.white, "While evaluating game ", new Exception("White and Black disagree on game status"));
                }

                if (statusWhite != gameStatus.inProgress)
                {
                    // OK, the game is over!
                    if (statusWhite == gameStatus.drawn)
                    {
                        isDraw = true;
                    }
                    else
                    {
                        isDraw = false;
                        winningSide = colJustMoved;
                    }
                    break;
                }
            }

            isFinished = true;
            isRunning = false;

            gameFinished(gameBoardWhite);
        }