Пример #1
0
    def test_is_optional_game_end(self):
        result = sf.is_optional_game_end("capablanca", CAPA, [])
        self.assertFalse(result[0])

        # sittuyin stalemate due to optional promotion
        result = sf.is_optional_game_end("sittuyin",
                                         "1k4PK/3r4/8/8/8/8/8/8[] w - - 0 1",
                                         [])
        self.assertTrue(result[0])
        self.assertEqual(result[1], sf.VALUE_DRAW)
Пример #2
0
 def is_optional_game_end(self):
     return sf.is_optional_game_end(
         self.variant,
         self.initial_fen,
         self.move_stack,
         self.chess960,
         self.count_started,
     )
Пример #3
0
 def is_game_end(self):
     with self.lock:
         game_end = False
         if self.clock_times[(len(self.moves) - 1) % 2] <= 0:
             # time loss
             logging.warning('Engine {} loses on time.'.format((len(self.moves) - 1) % 2 + 1))
             result = 1
             game_end = True
         elif self.moves and not self.is_legal():
             # last move was illegal
             result = 1
             game_end = True
             logging.error('Illegal move: {}'.format(self.moves[-1]))
         elif not sf.legal_moves(self.variant, self.get_start_fen(), self.moves):
             game_end = True
             result = sf.game_result(self.variant, self.get_start_fen(), self.moves)
         else:
             game_end, result = sf.is_optional_game_end(self.variant, self.get_start_fen(), self.moves)
         if game_end:
             self.result = int(math.copysign(1, result if len(self.moves) % 2 == 0 else -result)) if result else result
         return game_end
Пример #4
0
 def test_is_optional_game_end(self):
     result = sf.is_optional_game_end("capablanca", CAPA, [])
     self.assertFalse(result[0])
Пример #5
0
    def runMatches(self,
                   variant: Variant,
                   matchCount=100,
                   debug=False,
                   variantPath: str = "") -> MatchData:
        """

        :param variant: The variant we want to run matches of.
        :param matchCount: The number of matches to run
        :param debug: If true, print debug statements
        :param variantPath: A path to the .ini file that contains the variant. If none is provided, one will be created.
        :return:
        """

        matchData = MatchData(variant)

        if variantPath == "" and not variant.builtIn:
            variantPath = "variant-{0}.ini".format(variant.name)
            with open(variantPath, "w") as ini:
                ini.write(variant.getFairyStockfishINI())

        pyffish.set_option("VariantPath", variantPath)

        for e in self.engines:
            # Set the engines to the variant we are running.
            e.setVariant(variant, variantPath)

        # This is the root of our MCT

        for matchNo in range(matchCount):

            match = Match(variant, (matchNo + 1))

            for e in self.engines:
                e.newgame()

            # Go through a MCTS opening

            stop = False
            curNode: MonteCarloTreeNode = matchData.MCTRoot

            while not stop:

                match.curNode = curNode
                stop, curNode = curNode.selectBestChild()

                if curNode is None:
                    # Checkmate or stalemate.

                    if pyffish.game_result(variant.name, variant.startingFEN,
                                           match.moves) == 0:
                        # Stalemate
                        match.markDraw()
                    elif i % 2 == 0:
                        match.markBlackVictory()
                    else:
                        match.markWhiteVictory()
                    break

                move = curNode.move
                match.addMove(move)

            # This is the loop that goes through the moves in any individual game
            while True:
                # Don't let too many moves happen!
                if len(match.moves) >= 1000:
                    match.markDraw()
                    break

                legal_moves = pyffish.legal_moves(variant.name,
                                                  variant.startingFEN,
                                                  match.moves)

                if len(legal_moves) == 0 and pyffish.game_result(
                        variant.name, variant.startingFEN, match.moves) == 0:
                    match.markDraw()
                    break
                elif pyffish.is_optional_game_end(variant.name,
                                                  variant.startingFEN,
                                                  match.moves)[0]:
                    match.markDraw()
                    break

                active_engine = self.engines[len(match.moves) % 2]
                inactive_engine = self.engines[(len(match.moves) + 1) % 2]

                active_engine.setposition(match.moves)
                moveDict = active_engine.bestmove()
                bestMove: str = moveDict["move"]
                ponder: str = moveDict["ponder"]
                info: str = moveDict["info"]
                match.addMove(bestMove)

                if debug:
                    print("{0}, move {1}, info {2}".format(
                        len(match.moves), bestMove, info))

                # If the engine found mate, then we can stop running through the steps.
                # TODO: This needs to be replaced with calls to pyffish.
                if 'mate' in moveDict.keys():
                    mateNum: int = moveDict['mate']
                    if mateNum in (1, 0):
                        # Somebody has checkmate, so find out who is the winner
                        if mateNum > 0:
                            winning = active_engine
                        else:
                            winning = inactive_engine

                        if winning == self.whiteEngine:
                            match.markWhiteVictory()
                        else:
                            match.markBlackVictory()
                        break
                # Add the matches we just played to the match data.
            matchData.addMatch(match)
            print(match)
        return matchData