Esempio n. 1
0
    def test_san_promotion_ambiguity(self):
        """Testing Sittuyin promotion SAN parser"""

        board = SittuyinBoard(setup=FEN6)
        print(board)
        self.assertNotEqual(parseAN(board, 'd4c3f'), parseSAN(board, 'c3=f'))
        self.assertEqual(parseAN(board, 'b4c3f'), parseSAN(board, 'c3=f'))

        board = SittuyinBoard(setup=FEN7)
        print(board)
        self.assertNotEqual(parseAN(board, 'f5e6f'), parseSAN(board, 'e6=f'))
        self.assertEqual(parseAN(board, 'd5e6f'), parseSAN(board, 'e6=f'))
Esempio n. 2
0
    def test_validate(self):
        """Testing validate move in Sittuyin variant"""

        board = SittuyinBoard(setup=FEN0)
        print(board)
        # no promotion if we have Met (queen)
        self.assertTrue(validate(board, parseAN(board, 'f4f3')))
        self.assertTrue(validate(board, parseAN(board, 'b2b1')))
        self.assertTrue(validate(board, parseAN(board, 'b2c1')))

        self.assertFalse(validate(board, parseAN(board, 'b2b2f')))
        self.assertFalse(validate(board, parseAN(board, 'b2a1f')))
        self.assertFalse(validate(board, parseAN(board, 'f4f3f')))
        self.assertFalse(validate(board, parseAN(board, 'b2b1f')))
        self.assertFalse(validate(board, parseAN(board, 'b2c1f')))

        board = SittuyinBoard(setup=FEN1)
        print(board)
        # but (optional) promotion if we don't have Met (queen)
        self.assertTrue(validate(board, parseAN(board, 'b2b2f')))
        self.assertTrue(validate(board, parseSAN(board, 'b2=f')))
        self.assertEqual(parseAN(board, 'b2b2f'), parseSAN(board, 'b2=f'))
        self.assertTrue(validate(board, parseAN(board, 'b2a1f')))
        self.assertTrue(validate(board, parseSAN(board, 'a1=f')))
        self.assertEqual(parseAN(board, 'b2a1f'), parseSAN(board, 'a1=f'))
        self.assertTrue(validate(board, parseAN(board, 'b2c1')))
        self.assertTrue(validate(board, parseAN(board, 'f4f3')))

        self.assertFalse(validate(board, parseAN(board, 'f4f3f')))
        self.assertFalse(validate(board, parseAN(board, 'b2b2')))
        self.assertFalse(validate(board, parseAN(board, 'b2b1f')))
        self.assertFalse(validate(board, parseAN(board, 'b2c1f')))
Esempio n. 3
0
    def test_validate(self):
        """Testing validate move in Sittuyin variant"""

        board = SittuyinBoard(setup=FEN0)
        print(board)
        self.assertTrue(validate(board, parseAN(board, 'f4f3')))
        self.assertTrue(validate(board, parseAN(board, 'b2b1')))
        self.assertTrue(validate(board, parseAN(board, 'b2c1')))

        # no promotion if we have Met (queen)
        self.assertFalse(validate(board, parseAN(board, 'b2b2f')))
        self.assertFalse(validate(board, parseAN(board, 'b2a1f')))
        self.assertFalse(validate(board, parseAN(board, 'f4f3f')))
        self.assertFalse(validate(board, parseAN(board, 'b2b1f')))
        self.assertFalse(validate(board, parseAN(board, 'b2c1f')))

        board = SittuyinBoard(setup=FEN1)
        print(board)
        # but (optional) promotion if we don't have Met (queen)
        self.assertFalse(validate(board, parseAN(board, 'b2b2f')))
        self.assertFalse(validate(board, parseSAN(board, 'b2=f')))
        self.assertEqual(parseAN(board, 'b2b2f'), parseSAN(board, 'b2=f'))
        self.assertTrue(validate(board, parseAN(board, 'b2a1f')))
        self.assertTrue(validate(board, parseSAN(board, 'a1=f')))
        self.assertEqual(parseAN(board, 'b2a1f'), parseSAN(board, 'a1=f'))
        self.assertTrue(validate(board, parseAN(board, 'b2c1')))
        self.assertTrue(validate(board, parseAN(board, 'f4f3')))

        self.assertFalse(validate(board, parseAN(board, 'f4f3f')))
        self.assertFalse(validate(board, parseAN(board, 'b2b2')))
        self.assertFalse(validate(board, parseAN(board, 'b2b1f')))
        self.assertFalse(validate(board, parseAN(board, 'b2c1f')))

        board = SittuyinBoard(setup=FEN2)
        print(board)
        # simple pawn move can give check
        self.assertTrue(validate(board, parseAN(board, 'd6d7')))
        # pawn can promote in place
        self.assertTrue(validate(board, parseAN(board, 'd6d6f')))
        # pawn promotion move can't give check
        self.assertFalse(validate(board, parseAN(board, 'd6c7f')))
        self.assertFalse(validate(board, parseAN(board, 'd6e7f')))

        board = SittuyinBoard(setup=FEN3)
        print(board)
        self.assertTrue(validate(board, parseAN(board, 'd6d7')))
        # last pawn being enywhere can promote
        self.assertTrue(validate(board, parseAN(board, 'd6d6f')))
        self.assertTrue(validate(board, parseAN(board, 'd6c7f')))
        self.assertTrue(validate(board, parseAN(board, 'd6c5f')))
        self.assertTrue(validate(board, parseAN(board, 'd6e5f')))
        # pawn promotion move can't give check
        self.assertFalse(validate(board, parseAN(board, 'd6e7f')))
Esempio n. 4
0
    def __parseLine (self, line):
        if not self.connected: return
        parts = line.split()
        if not parts: return
        
        #---------------------------------------------------------- Initializing
        if parts[0] == "id":
            self.ids[parts[1]] = " ".join(parts[2:])
            if parts[1] == "name":
                self.setName(self.ids["name"])
            return
        
        if parts[0] == "uciok":
            #self.emit("readyForOptions")
            return
        
        if parts[0] == "readyok":
            #self.emit("readyForMoves")
            return
        
        #------------------------------------------------------- Options parsing
        if parts[0] == "option":
            dic = {}
            last = 1
            varlist = []
            for i in xrange (2, len(parts)+1):
                if i == len(parts) or parts[i] in OPTKEYS:
                    key = parts[last]
                    value = " ".join(parts[last+1:i])
                    if "type" in dic and dic["type"] in TYPEDIC:
                        value = TYPEDIC[dic["type"]](value)
                        
                    if key == "var":
                        varlist.append(value)
                    elif key == "type" and value == "string":
                        dic[key] = "text"
                    else:
                        dic[key] = value
                        
                    last = i
            if varlist:
                dic["choices"] = varlist
            
            self.options[dic["name"]] = dic
            return
        
        #---------------------------------------------------------------- A Move
        if self.mode == NORMAL and parts[0] == "bestmove":
            with self.moveLock:
                self.needBestmove = False
                self.__sendQueuedGo()
                
                if self.ignoreNext:
                    log.debug("__parseLine: line='%s' self.ignoreNext==True, returning\n" % \
                        line.strip(), self.defname)
                    self.ignoreNext = False
                    self.readyForStop = True
                    return
                
                if not self.waitingForMove:
                    log.warn("__parseLine: self.waitingForMove==False, ignoring move=%s\n" % \
                        parts[1], self.defname)
                    self.pondermove = None
                    return
                self.waitingForMove = False

                try:
                    move = parseAN(self.board, parts[1])
                except ParsingError, e:
                    self.end(WHITEWON if self.board.color == BLACK else BLACKWON, WON_ADJUDICATION)
                    return
                
                if not validate(self.board, move):
                    # This is critical. To avoid game stalls, we need to resign on
                    # behalf of the engine.
                    log.error("__parseLine: move=%s didn't validate, putting 'del' in returnQueue. self.board=%s\n" % \
                        (repr(move), self.board), self.defname)
                    self.end(WHITEWON if self.board.color == BLACK else BLACKWON, WON_ADJUDICATION)
                    return
                
                self._recordMove(self.board.move(move), move, self.board)
                log.debug("__parseLine: applied move=%s to self.board=%s\n" % \
                    (move, self.board), self.defname)
                
                if self.ponderOn:
                    self.pondermove = None
                    # An engine may send an empty ponder line, simply to clear.
                    if len(parts) == 4:
                        # Engines don't always check for everything in their
                        # ponders. Hence we need to validate.
                        # But in some cases, what they send may not even be
                        # correct AN - specially in the case of promotion.
                        try:
                            pondermove = parseAN(self.board, parts[3])
                        except ParsingError:
                            pass
                        else:
                            if validate(self.board, pondermove):
                                self.pondermove = pondermove
                                self._startPonder()
                
                self.returnQueue.put(move)
                log.debug("__parseLine: put move=%s into self.returnQueue=%s\n" % \
                    (move, self.returnQueue.queue), self.defname)
                return