Beispiel #1
0
 def pv_to_san(self):
     if(self.san_arr == None):
         return ""
     else:
         try:
             pv_san = []
             board = Bitboard(self.san_arr[0])
             moves = self.san_arr[1]
             for uci in moves:
                 move = Move.from_uci(uci)
                 if(move in board.pseudo_legal_moves):
                     pv_san.append(board.san(move))
                     board.push(move)
             if(len(pv_san) > 0):
                 s = ""
                 white_moves = True
                 move_no = (self.no_game_halfmoves//2)+1
                 if(self.no_game_halfmoves % 2 == 1):
                     white_moves = False
                     s += str(move_no)+". ... "
                     move_no += 1
                 for san in pv_san:
                     if(white_moves):
                         s += " "+str(move_no)+". "+san
                         move_no +=1
                     else:
                         s += " "+san
                     white_moves = not white_moves
                 return s
             else:
                 return ""
         except ValueError:
             return ""
Beispiel #2
0
 def deep_copy_board_pos(self,board):
     fresh = Bitboard()
     for i in range(0,8):
         for j in range(0,8):
             piece = board.piece_at(j*8+i)
             if(piece):
                 sym = piece.symbol()
                 fresh.set_piece_at(j*8+i,Piece.from_symbol(sym))
             else:
                 fresh.remove_piece_at(j*8+i)
     return fresh
Beispiel #3
0
    def tap(self, keycode, character, press):
        # If 'n' is pressed then next move is played by allmighty SF
        if character == 'n' and press:
            print '---------------------------'
            try:
                pgn_text = get_pgn()
                game = pgn.loads(pgn_text)[0]
                if game.white == me: side = Player.WHITE
                else: side = Player.BLACK 
            except:
                print 'Issue with PGN:', pgn_text
                return

            print 'Moves: ', game.moves

            board = Bitboard()
            for move in game.moves:
                if move != '*':
                    board.push_san(move)

            fen_pos = board.fen()
            print 'FEN: ', fen_pos

            ## Set fen position to stockfish, find the best move

            stock.setposition_fen(fen_pos)
            move_dict = stock.bestmove()
            print 'move_dict: ', move_dict

            move = move_dict['move']

            ## Try to play the best move
            try:
                make_move(move, side)
            except:
                print 'Problem playing move!'

        # By pressing number from 1-9 you can set how long SF could possibly
        # think on a particular move (in seconds)
        elif character in DIGITS and press:
            print 'pressed: ', character
            stock.movetime = int(character) * 1000
Beispiel #4
0
    def __init__(self, board = None, parent = None):
        super(QWidget, self).__init__()
        policy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        self.setSizePolicy(policy)

        if(board == None):
            board = Bitboard()
            board.castling_rights = CASTLING_NONE

        self.board = board

        self.parent = parent

        self.borderWidth = 12
        self.pieceImages = PieceImages()

        self.selected_xy = None
        self.pcs = [['P','R','B','N','Q','K'],['p','r','b','n','q','k']]


        self.initUI()
Beispiel #5
0
    def __init__(self, board=None, parent=None):
        super(DialogEnterPosition, self).__init__(parent)
        #self.resize(600, 400)

        self.setWindowTitle(self.trUtf8("Enter Position"))

        self.displayBoard = DisplayBoard(self.deep_copy_board_pos(board),self)

        # create a copy of the current board
        if(board):
            # create a deepcopy of current board
            self.current = self.deep_copy_board_pos(board)
        else:
            self.current = Bitboard()

        self.cbWhiteShort = QCheckBox(self.trUtf8("White O-O"))
        self.cbWhiteLong = QCheckBox(self.trUtf8("White O-O-O"))
        self.cbBlackShort = QCheckBox(self.trUtf8("Black O-O"))
        self.cbBlackLong = QCheckBox(self.trUtf8("Black O-O-O"))
        grpBox_castle = QGroupBox(self.trUtf8("Castling Rights"))
        vbox_castle = QVBoxLayout()
        vbox_castle.addWidget(self.cbWhiteShort)
        vbox_castle.addWidget(self.cbWhiteLong)
        vbox_castle.addWidget(self.cbBlackShort)
        vbox_castle.addWidget(self.cbBlackLong)
        vbox_castle.addStretch(1)
        grpBox_castle.setLayout(vbox_castle)

        self.rbWhite = QRadioButton(self.trUtf8("White To Move"))
        self.rbBlack = QRadioButton(self.trUtf8("Black To Move"))
        grpBox_turn = QGroupBox(self.trUtf8("Turn"))
        vbox_radio = QVBoxLayout()
        vbox_radio.addWidget(self.rbWhite)
        vbox_radio.addWidget(self.rbBlack)
        vbox_radio.addStretch(1)
        grpBox_turn.setLayout(vbox_radio)

        self.buttonInit = QPushButton(self.trUtf8("Initial Position"))
        self.buttonClear = QPushButton(self.trUtf8("Clear Board"))
        self.buttonCurrent = QPushButton(self.trUtf8("Current Position"))

        vbox_config = QVBoxLayout()
        vbox_config.addWidget(grpBox_castle)
        vbox_config.addWidget(grpBox_turn)
        vbox_config.addStretch(1)
        vbox_config.addWidget(self.buttonInit)
        vbox_config.addWidget(self.buttonClear)
        vbox_config.addWidget(self.buttonCurrent)

        hbox = QHBoxLayout()
        hbox.addWidget(self.displayBoard)
        hbox.addLayout(vbox_config)

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok| QDialogButtonBox.Cancel)
        vbox.addWidget(self.buttonBox)

        self.setLayout(vbox)

        self.connect(self.buttonBox, SIGNAL("accepted()"),
                 self, SLOT("accept()"))
        self.connect(self.buttonBox, SIGNAL("rejected()"),
                 self, SLOT("reject()"))

        self.cbWhiteShort.toggled.connect(self.set_castling_rights)
        self.cbWhiteLong.toggled.connect(self.set_castling_rights)
        self.cbBlackShort.toggled.connect(self.set_castling_rights)
        self.cbBlackLong.toggled.connect(self.set_castling_rights)

        self.rbWhite.toggle()
        self.rbWhite.toggled.connect(self.set_turn)
        self.rbBlack.toggled.connect(self.set_turn)

        self.buttonInit.clicked.connect(self.initial_position)
        self.buttonClear.clicked.connect(self.clear_board)
        self.buttonCurrent.clicked.connect(self.set_current)

        # reset who's current turn it is and the current
        # castling rights of the position
        self.set_castling_rights()
        self.set_turn()
Beispiel #6
0
class DialogEnterPosition(QDialog):

    def __init__(self, board=None, parent=None):
        super(DialogEnterPosition, self).__init__(parent)
        #self.resize(600, 400)

        self.setWindowTitle(self.trUtf8("Enter Position"))

        self.displayBoard = DisplayBoard(self.deep_copy_board_pos(board),self)

        # create a copy of the current board
        if(board):
            # create a deepcopy of current board
            self.current = self.deep_copy_board_pos(board)
        else:
            self.current = Bitboard()

        self.cbWhiteShort = QCheckBox(self.trUtf8("White O-O"))
        self.cbWhiteLong = QCheckBox(self.trUtf8("White O-O-O"))
        self.cbBlackShort = QCheckBox(self.trUtf8("Black O-O"))
        self.cbBlackLong = QCheckBox(self.trUtf8("Black O-O-O"))
        grpBox_castle = QGroupBox(self.trUtf8("Castling Rights"))
        vbox_castle = QVBoxLayout()
        vbox_castle.addWidget(self.cbWhiteShort)
        vbox_castle.addWidget(self.cbWhiteLong)
        vbox_castle.addWidget(self.cbBlackShort)
        vbox_castle.addWidget(self.cbBlackLong)
        vbox_castle.addStretch(1)
        grpBox_castle.setLayout(vbox_castle)

        self.rbWhite = QRadioButton(self.trUtf8("White To Move"))
        self.rbBlack = QRadioButton(self.trUtf8("Black To Move"))
        grpBox_turn = QGroupBox(self.trUtf8("Turn"))
        vbox_radio = QVBoxLayout()
        vbox_radio.addWidget(self.rbWhite)
        vbox_radio.addWidget(self.rbBlack)
        vbox_radio.addStretch(1)
        grpBox_turn.setLayout(vbox_radio)

        self.buttonInit = QPushButton(self.trUtf8("Initial Position"))
        self.buttonClear = QPushButton(self.trUtf8("Clear Board"))
        self.buttonCurrent = QPushButton(self.trUtf8("Current Position"))

        vbox_config = QVBoxLayout()
        vbox_config.addWidget(grpBox_castle)
        vbox_config.addWidget(grpBox_turn)
        vbox_config.addStretch(1)
        vbox_config.addWidget(self.buttonInit)
        vbox_config.addWidget(self.buttonClear)
        vbox_config.addWidget(self.buttonCurrent)

        hbox = QHBoxLayout()
        hbox.addWidget(self.displayBoard)
        hbox.addLayout(vbox_config)

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok| QDialogButtonBox.Cancel)
        vbox.addWidget(self.buttonBox)

        self.setLayout(vbox)

        self.connect(self.buttonBox, SIGNAL("accepted()"),
                 self, SLOT("accept()"))
        self.connect(self.buttonBox, SIGNAL("rejected()"),
                 self, SLOT("reject()"))

        self.cbWhiteShort.toggled.connect(self.set_castling_rights)
        self.cbWhiteLong.toggled.connect(self.set_castling_rights)
        self.cbBlackShort.toggled.connect(self.set_castling_rights)
        self.cbBlackLong.toggled.connect(self.set_castling_rights)

        self.rbWhite.toggle()
        self.rbWhite.toggled.connect(self.set_turn)
        self.rbBlack.toggled.connect(self.set_turn)

        self.buttonInit.clicked.connect(self.initial_position)
        self.buttonClear.clicked.connect(self.clear_board)
        self.buttonCurrent.clicked.connect(self.set_current)

        # reset who's current turn it is and the current
        # castling rights of the position
        self.set_castling_rights()
        self.set_turn()

    def set_castling_rights(self):
        self.displayBoard.board.castling_rights = CASTLING
        #if(not self.cbWhiteShort.isChecked() or not self.cbWhiteLong.isChecked()):
        #    self.displayBoard.board.castling_rights &= ~CASTLING_WHITE
        #if(not self.cbBlackShort.isChecked() or not self.cbBlackLong.isChecked()):
        #    self.displayBoard.board.castling_rights &= ~CASTLING_BLACK
        if(not self.cbWhiteShort.isChecked()):
            self.displayBoard.board.castling_rights &= ~CASTLING_WHITE_KINGSIDE
        if(not self.cbWhiteLong.isChecked()):
            self.displayBoard.board.castling_rights &= ~CASTLING_WHITE_QUEENSIDE
        if(not self.cbBlackShort.isChecked()):
            self.displayBoard.board.castling_rights &= ~CASTLING_BLACK_KINGSIDE
        if(not self.cbBlackLong.isChecked()):
            self.displayBoard.board.castling_rights &= ~CASTLING_BLACK_QUEENSIDE
        if(self.displayBoard.board.status() == 0):
            self.enable_ok_button()
        else:
            self.disable_ok_button()

    def set_turn(self):
        if(self.rbWhite.isChecked()):
            self.displayBoard.board.turn = WHITE
        else:
            self.displayBoard.board.turn = BLACK
        if(self.displayBoard.board.status() == 0):
            self.enable_ok_button()
        else:
            self.disable_ok_button()

    def clear_board(self):
        self.displayBoard.board.clear()
        self.set_castling_rights()
        self.set_turn()
        self.update()

    def initial_position(self):
        self.displayBoard.board.reset()
        self.set_castling_rights()
        self.set_turn()
        self.update()

    def set_current(self):
        fen = self.current.fen()
        board = Bitboard(fen)
        self.displayBoard.board = board
        self.set_castling_rights()
        self.set_turn()
        self.update()

    # creates a deep copy of the given
    # board into a clean new board, resetting
    # all move histories, castling rights, etc.
    def deep_copy_board_pos(self,board):
        fresh = Bitboard()
        for i in range(0,8):
            for j in range(0,8):
                piece = board.piece_at(j*8+i)
                if(piece):
                    sym = piece.symbol()
                    fresh.set_piece_at(j*8+i,Piece.from_symbol(sym))
                else:
                    fresh.remove_piece_at(j*8+i)
        return fresh

    def enable_ok_button(self):
        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True)
        self.update()

    def disable_ok_button(self):
        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
        self.update()