Exemple #1
0
    def make_hypothetical_move(start, finish, chessboard):
        """
        Make a hypothetical move, this will be used to generate the possibilities to be
        stored in the chess tree

        This method has a ton of redundant code with the make_move() method 
        so I should probably 
        
        input:
        starting coordinate: example "e4"
        ending coordinate: example "e5"
        chessboard: chessboard that you want to move
        
        output:
        "Move success" or "Move invalid"
        
        Uses the RulesEnforcer() to make sure that the move is valid
        
        """
        #deepcopy the chessboard so that it does not affect the original
        mychessboard = copy.deepcopy(chessboard[:])

        #map start and finish to gameboard coordinates
        start = RulesEnforcer.coordinate_mapper(start)
        finish = RulesEnforcer.coordinate_mapper(finish)

        #need to move alot of this logic to the rules enforcer
        start_cor0 = start[0]
        start_cor1 = start[1]

        finish_cor0 = finish[0]
        finish_cor1 = finish[1]

        #check if destination is white, black or empty
        start_color = mychessboard[start_cor0][start_cor1].split('-')[0]
        start_piece = mychessboard[start_cor0][start_cor1].split('-')[1]

        #check if destination is white, black or empty
        destination_color = mychessboard[finish_cor0][finish_cor1].split(
            '-')[0]
        destination_piece = mychessboard[finish_cor0][finish_cor1].split(
            '-')[1]

        #cannot move if starting square is empty
        if start_color == '0':
            return "Starting square is empty!"

        mypiece = mychessboard[start_cor0][start_cor1]
        mychessboard[start_cor0][start_cor1] = '0-0'
        mychessboard[finish_cor0][finish_cor1] = mypiece

        return mychessboard
Exemple #2
0
    def make_move(self, start, finish):
        """
        Make a move
        
        input:
        starting coordinate: example "e4"
        ending coordinate: example "e5"
        
        output:
        "Move success" or "Move invalid", self.chessboard is updated with the move made
        
        Uses the RulesEnforcer() to make sure that the move is valid
        
        """

        #map start and finish to gameboard coordinates
        start = RulesEnforcer.coordinate_mapper(start)
        finish = RulesEnforcer.coordinate_mapper(finish)

        #need to move alot of this logic to the rules enforcer
        start_cor0 = start[0]
        start_cor1 = start[1]

        finish_cor0 = finish[0]
        finish_cor1 = finish[1]

        #check if destination is white, black or empty
        start_color = self.chessboard[start_cor0][start_cor1].split('-')[0]
        start_piece = self.chessboard[start_cor0][start_cor1].split('-')[1]

        #check if destination is white, black or empty
        destination_color = self.chessboard[finish_cor0][finish_cor1].split(
            '-')[0]
        destination_piece = self.chessboard[finish_cor0][finish_cor1].split(
            '-')[1]

        #cannot move if starting square is empty
        if start_color == '0':
            return "Starting square is empty!"

        #cannot move the other person's piece
        if self.current_turn != start_color:
            return "Cannot move the other person's piece!"

        #cannot take your own piece
        if self.current_turn == destination_color:
            return "invalid move, cannot take your own piece!"
        elif self.current_turn != destination_color and destination_color != '0':
            if destination_piece == 'k':
                self.game_over = True
                return "game over, " + self.current_turn + " has won"
            elif self.current_turn == 'w':
                self.black_taken.append(destination_piece)
            elif self.current_turn == 'b':
                self.white_taken.append(destination_piece)
        else:
            pass

        mypiece = self.chessboard[start_cor0][start_cor1]
        self.chessboard[start_cor0][start_cor1] = '0-0'
        self.chessboard[finish_cor0][finish_cor1] = mypiece

        #if the move is a success, change the turn state
        if self.current_turn == "w":
            self.current_turn = "b"
        elif self.current_turn == "b":
            self.current_turn = "w"

        return self.chessboard