Exemple #1
0
    def __init__(self, ai_depth):
        """
        Creates a chessboard with pieces
        
        params:
        ai_depth: max number of moves to search into the future
        
        Notation:
        ------------
        000 == empty space  
        
        "b-p"   == black pawn
        "b-r"   == black rook
        "b-r"   == black rook
        "b-n"   == black knight
        "b-b"   == black bishop
        "b-q"   == black queen
        "b-k"   == black king  
        
        "w-k"   == white king

        ... etc etc you get the idea
        
        
        As soon as the chess game is initialized, the chess computer will start calculating

        """

        ChessAi.__init__(self, ai_depth)
        RulesEnforcer.__init__(self)
        #super(ChessGame, self).__init__()

        self.ai_depth = ai_depth

        #initialize the chessboard
        self.chessboard = [["0-0"] * 8 for i in range(8)]
        """Track aspects of the game"""
        #track which pieces have been taken
        self.white_taken = []
        self.black_taken = []

        #track which moves have been made in the game, key: move number, value: len 2 list of white and black move
        self.moves_made = {}

        #track the number of moves made
        self.move_count = 0

        #track whose turn it is (white always starts)
        self.current_turn = "w"

        #create pawns
        for i in range(8):
            self.chessboard[1][i] = 'b-p'
            self.chessboard[6][i] = 'w-p'

        #create rooks
        self.chessboard[0][0] = 'b-r'
        self.chessboard[0][7] = 'b-r'
        self.chessboard[7][0] = 'w-r'
        self.chessboard[7][7] = 'w-r'

        #create knights
        self.chessboard[0][1] = 'b-n'
        self.chessboard[0][6] = 'b-n'
        self.chessboard[7][1] = 'w-n'
        self.chessboard[7][6] = 'w-n'

        #create bishops
        self.chessboard[0][2] = 'b-b'
        self.chessboard[0][5] = 'b-b'
        self.chessboard[7][2] = 'w-b'
        self.chessboard[7][5] = 'w-b'

        #create queen and king
        self.chessboard[0][3] = 'b-q'
        self.chessboard[0][4] = 'b-k'
        self.chessboard[7][3] = 'w-q'
        self.chessboard[7][4] = 'w-k'

        self.game_over = False
Exemple #2
0
    def __init__(self, payload, invalidPQ):
        """
        Creates a chessboard with pieces
        
        params:
        ai_depth: max number of moves to search into the future
        
        Notation:
        ------------
        000 == empty space  
        
        "b-p"   == black pawn
        "b-r"   == black rook
        "b-r"   == black rook
        "b-n"   == black knight
        "b-b"   == black bishop
        "b-q"   == black queen
        "b-k"   == black king  
        
        "w-k"   == white king

        ... etc etc you get the idea
        
        
        As soon as the chess game is initialized, the chess computer will start calculating

        """

        ChessAi.__init__(self, payload['level'])
        RulesEnforcer.__init__(self)
        #super(ChessGame, self).__init__()

        self.ai_depth = payload['level']

        chessboard1 = [["0-0"] * 12 for i in range(12)]

        self.invalidPQ = invalidPQ

        def fill(color, code, position):
            chessboard1[position[0]][position[1]] = color + '-'
            if code == 'rook':
                chessboard1[position[0]][position[1]] += 'r'
            elif code == 'pawn':
                chessboard1[position[0]][position[1]] += 'p'
            elif code == 'knight':
                chessboard1[position[0]][position[1]] += 'n'
            elif code == 'bishop':
                chessboard1[position[0]][position[1]] += 'b'
            elif code == 'queen':
                chessboard1[position[0]][position[1]] += 'q'
            elif code == 'king':
                chessboard1[position[0]][position[1]] += 'k'

        def okPQ(i, j):
            for k in range(len(self.invalidPQ)):
                if i == self.invalidPQ[k][0] and j == self.invalidPQ[k][1]:
                    return False
            return True

        #initialize the chessboard
        self.chessboard = [["0-0"] * 8 for i in range(8)]

        y = payload['board']
        for row in y:
            for column, values in row.items(
            ) if type(row) is dict else enumerate(row):
                if values['ai'] == True:
                    fill('b', values['code'], values['position'])
                else:
                    fill('w', values['code'], values['position'])
        """Track aspects of the game"""
        #track which pieces have been taken
        self.white_taken = []
        self.black_taken = []

        #track whose turn it is (white always starts)
        self.current_turn = "b"

        maxCounter = 0
        self.finalP = 0
        self.finalQ = 0
        for p in range(4):
            for q in range(4):
                if okPQ(p, q):
                    counter = 0
                    hasAiPiece = False
                    hasKing = False
                    hasOpponentPiece = False
                    for i in range(8):
                        for j in range(8):
                            if chessboard1[i + p][j + q] != '0-0':
                                counter += 1
                            if chessboard1[i + p][j + q][0] == 'b':
                                hasAiPiece = True
                            if chessboard1[i + p][j +
                                                  q][2] == 'k' and chessboard1[
                                                      i + p][j + q][0] == 'w':
                                hasKing = True
                            if chessboard1[i + p][j + q][0] == 'w':
                                hasOpponentPiece = True
                    if counter > maxCounter and hasAiPiece and hasKing and hasOpponentPiece:
                        maxCounter = counter
                        self.finalP = p
                        self.finalQ = q

        for i in range(8):
            for j in range(8):
                self.chessboard[i][j] = chessboard1[i +
                                                    self.finalP][j +
                                                                 self.finalQ]

        self.game_over = False