Beispiel #1
0
 def pin_piece(self, pinned_piece, dir):
     """A black piece is pinned between bishop and king in direction dir.
 Pin that piece by filtering any moves that are not in the bishop's pinned_movelist.
 """
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     for i in range(2):
         if self.in_path[i] == 0 or self.alive[i] == 0:
             continue
         self.pinned_movelist[i].clear()
         if dir == "UL":
             row = self.row[i]
             col = self.col[i]
             while row >= 0 and col >= 0:
                 if (row, col) == Black_.get_pos("K"):
                     Black_.filter(pinned_piece, self.pinned_movelist[i])
                     return
                 self.pinned_movelist[i].append((row, col))
                 row -= 1
                 col -= 1
         elif dir == "UR":
             row = self.row[i]
             col = self.col[i]
             while row >= 0 and col <= 7:
                 if (row, col) == Black_.get_pos("K"):
                     Black_.filter(pinned_piece, self.pinned_movelist[i])
                     return
                 self.pinned_movelist[i].append((row, col))
                 row -= 1
                 col += 1
         elif dir == "LR":
             row = self.row[i]
             col = self.col[i]
             while row <= 7 and col <= 7:
                 if (row, col) == Black_.get_pos("K"):
                     Black_.filter(pinned_piece, self.pinned_movelist[i])
                     return
                 self.pinned_movelist[i].append((row, col))
                 row += 1
                 col += 1
         elif dir == "LL":
             row = self.row[i]
             col = self.col[i]
             while row <= 7 and col >= 0:
                 if (row, col) == Black_.get_pos("K"):
                     Black_.filter(pinned_piece, self.pinned_movelist[i])
                     return
                 self.pinned_movelist[i].append((row, col))
                 row += 1
                 col -= 1
Beispiel #2
0
 def king_in_path(self):
     """Determine if king is in path of bishop. Used for pinning.
 Returns "UL", "UR", "LL", "LR" for where the king's position is relative
 to the bishop (upper left, upper right, lower left, lower right).
 Also sets the in_path flag for the bishop to 1.
 """
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     for i in range(2):
         if self.alive[i] == 0:
             continue
         self.in_path[i] = 0
         row = self.row[i]
         col = self.col[i]
         while row >= 0 and col >= 0:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = 1
                 return "UL"
             row -= 1
             col -= 1
         row = self.row[i]
         col = self.col[i]
         while row >= 0 and col <= 7:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = 1
                 return "UR"
             row -= 1
             col += 1
         row = self.row[i]
         col = self.col[i]
         while row <= 7 and col <= 7:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = 1
                 return "LR"
             row += 1
             col += 1
         row = self.row[i]
         col = self.col[i]
         while row <= 7 and col >= 0:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = 1
                 return "LL"
             row += 1
             col -= 1
Beispiel #3
0
 def get_pinned_piece(self, dir):
     "A piece has been pinned in direction dir. Get that piece."
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     for i in range(2):
         if self.in_path[i] == 0 or self.alive[i] == 0:
             continue
         if dir == "UL":
             row = self.row[i]
             col = self.col[i]
             while row >= 0 and col >= 0:
                 if Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     return Black_.get_piece(row, col)
                 row -= 1
                 col -= 1
         elif dir == "UR":
             row = self.row[i]
             col = self.col[i]
             while row >= 0 and col <= 7:
                 if Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     return Black_.get_piece(row, col)
                 row -= 1
                 col += 1
         elif dir == "LR":
             row = self.row[i]
             col = self.col[i]
             while row <= 7 and col <= 7:
                 if Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     return Black_.get_piece(row, col)
                 row += 1
                 col += 1
         elif dir == "LL":
             row = self.row[i]
             col = self.col[i]
             while row <= 7 and col >= 0:
                 if Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     return Black_.get_piece(row, col)
                 row += 1
                 col -= 1
Beispiel #4
0
 def build_check_movelist(self):
     """Black king has been checked by a bishop. Build and return a movelist going
 in the direction of the black king.
 """
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     black_king_row = Black_.get_pos("K")[0]
     black_king_col = Black_.get_pos("K")[1]
     movelist = []
     for i in range(2):
         if White.checker != "B" + str(i):
             continue
         row = self.row[i]
         col = self.col[i]
         # king in lower right (relative to bishop)
         if black_king_row > self.row[i] and black_king_col > self.col[i]:
             while row <= 7 and col <= 7:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row += 1
                 col += 1
         # lower left
         elif black_king_row > self.row[i] and black_king_col < self.col[i]:
             while row <= 7 and col >= 0:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row += 1
                 col -= 1
         # upper left
         elif black_king_row < self.row[i] and black_king_col < self.col[i]:
             while row >= 0 and col >= 0:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row -= 1
                 col -= 1
         # upper right
         elif black_king_row < self.row[i] and black_king_col > self.col[i]:
             while row >= 0 and col <= 7:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row -= 1
                 col += 1
Beispiel #5
0
 def update_movelist(self):
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     for i in range(8):
         self.movelist[i].clear()
         self.hit_movelist[i].clear()
         if self.alive[i] == 0:
             continue
         # normal move (1 forward)
         if self.row[i] - 1 >= 0:
             if Black.blocks[self.row[i]-1][self.col[i]] == 0 and \
             White.blocks[self.row[i]-1][self.col[i]] == 0:
                 self.movelist[i].append((self.row[i] - 1, self.col[i]))
         # opponent on left
         if self.row[i] - 1 >= 0 and self.col[i] - 1 >= 0:
             self.hit_movelist[i].append((self.row[i] - 1, self.col[i] - 1))
             if Black.blocks[self.row[i] - 1][self.col[i] - 1] == 1:
                 self.movelist[i].append((self.row[i] - 1, self.col[i] - 1))
         # opponent on right
         if self.row[i] - 1 >= 0 and self.col[i] + 1 <= 7:
             self.hit_movelist[i].append((self.row[i] - 1, self.col[i] + 1))
             if Black.blocks[self.row[i] - 1][self.col[i] + 1] == 1:
                 self.movelist[i].append((self.row[i] - 1, self.col[i] + 1))
         # initial double-step
         if self.row[i] == 6 and Black.blocks[self.row[i]-1][self.col[i]] == 0 and \
         White.blocks[self.row[i]-1][self.col[i]] == 0 and \
         Black.blocks[self.row[i]-2][self.col[i]] == 0 and \
         White.blocks[self.row[i]-2][self.col[i]] == 0:
             self.movelist[i].append((self.row[i] - 2, self.col[i]))
         # en passant
         if self.row[i] == 3:
             for k in range(len(Black.en_passant)):
                 if Black.en_passant[k] == 1:
                     pos = Black_.get_pos("P" + str(k))
                     if abs(pos[1] - self.col[i]) == 1:
                         self.movelist[i].append((pos[0] - 1, pos[1]))
Beispiel #6
0
    def update_movelist(self):
        # We are importing inside this function because we cannot import at the top (since
        # Black_Funcs class does not know about the black pieces at that point yet)
        from pieces.black_funcs import Black_Funcs
        Black_ = Black_Funcs(None)

        for i in range(2):
            self.movelist[i].clear()
            self.protecting_movelist[i].clear()
            if self.alive[i] == 0:
                continue
            row = self.row[i]
            col = self.col[i]
            # diagonal pointing to top-left corner of screen
            while row - 1 >= 0 and col - 1 >= 0:
                if White.blocks[row - 1][col - 1] == 1:
                    self.protecting_movelist[i].append((row - 1, col - 1))
                    break
                if Black.blocks[row - 1][col - 1] == 1:
                    blackking_pos = Black_.get_pos("K")
                    if blackking_pos[0] == row - 1 and blackking_pos[
                            1] == col - 1:
                        self.movelist[i].append((row - 1, col - 1))
                        self.movelist[i].append((row - 2, col - 2))
                        break
                    self.movelist[i].append((row - 1, col - 1))
                    break
                row -= 1
                col -= 1
                self.movelist[i].append((row, col))
            row = self.row[i]
            col = self.col[i]
            # diagonal to top-right corner of screen
            while row - 1 >= 0 and col + 1 <= 7:
                if White.blocks[row - 1][col + 1] == 1:
                    self.protecting_movelist[i].append((row - 1, col + 1))
                    break
                if Black.blocks[row - 1][col + 1] == 1:
                    blackking_pos = Black_.get_pos("K")
                    if blackking_pos[0] == row - 1 and blackking_pos[
                            1] == col + 1:
                        self.movelist[i].append((row - 1, col + 1))
                        self.movelist[i].append((row - 2, col + 2))
                        break
                    self.movelist[i].append((row - 1, col + 1))
                    break
                row -= 1
                col += 1
                self.movelist[i].append((row, col))
            row = self.row[i]
            col = self.col[i]
            # diagonal to bottom-right corner of screen
            while row + 1 <= 7 and col + 1 <= 7:
                if White.blocks[row + 1][col + 1] == 1:
                    self.protecting_movelist[i].append((row + 1, col + 1))
                    break
                if Black.blocks[row + 1][col + 1] == 1:
                    blackking_pos = Black_.get_pos("K")
                    if blackking_pos[0] == row + 1 and blackking_pos[
                            1] == col + 1:
                        self.movelist[i].append((row + 1, col + 1))
                        self.movelist[i].append((row + 2, col + 2))
                        break
                    self.movelist[i].append((row + 1, col + 1))
                    break
                row += 1
                col += 1
                self.movelist[i].append((row, col))
            row = self.row[i]
            col = self.col[i]
            # diagonal to bottom-left corner of screen
            while row + 1 <= 7 and col - 1 >= 0:
                if White.blocks[row + 1][col - 1] == 1:
                    self.protecting_movelist[i].append((row + 1, col - 1))
                    break
                if Black.blocks[row + 1][col - 1] == 1:
                    blackking_pos = Black_.get_pos("K")
                    if blackking_pos[0] == row + 1 and blackking_pos[
                            1] == col - 1:
                        self.movelist[i].append((row + 1, col - 1))
                        self.movelist[i].append((row + 2, col - 2))
                        break
                    self.movelist[i].append((row + 1, col - 1))
                    break
                row += 1
                col -= 1
                self.movelist[i].append((row, col))
Beispiel #7
0
 def num_pieces(self, dir):
     """Determine the number of black pieces between bishop and black
 king in direction dir.
 """
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     for i in range(2):
         if self.in_path[i] == 0 or self.alive[i] == 0:
             continue
         if dir == "UL":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             while row >= 0 and col >= 0:
                 if White.blocks[row][col] == 1 and num_blocks > 0:
                     return -1
                 elif Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     num += 1
                 elif (row, col) == Black_.get_pos("K"):
                     return num
                 row -= 1
                 col -= 1
                 num_blocks += 1
         elif dir == "UR":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             while row >= 0 and col <= 7:
                 if White.blocks[row][col] == 1 and num_blocks > 0:
                     return -1
                 elif Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     num += 1
                 elif (row, col) == Black_.get_pos("K"):
                     return num
                 row -= 1
                 col += 1
                 num_blocks += 1
         elif dir == "LR":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             while row <= 7 and col <= 7:
                 if White.blocks[row][col] == 1 and num_blocks > 0:
                     return -1
                 elif Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     num += 1
                 elif (row, col) == Black_.get_pos("K"):
                     return num
                 row += 1
                 col += 1
                 num_blocks += 1
         elif dir == "LL":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             while row <= 7 and col >= 0:
                 if White.blocks[row][col] == 1 and num_blocks > 0:
                     return -1
                 elif Black.blocks[row][col] == 1 and (
                         row, col) != Black_.get_pos("K"):
                     num += 1
                 elif (row, col) == Black_.get_pos("K"):
                     return num
                 row += 1
                 col -= 1
                 num_blocks += 1
Beispiel #8
0
 def build_check_movelist(self):
     """Black king has been checked by queen. Build and return a movelist going
 in the direction of the black king.
 """
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     black_king_row = Black_.get_pos("K")[0]
     black_king_col = Black_.get_pos("K")[1]
     movelist = []
     for i in range(White.num_queens):
         if White.checker != "Q" + str(i):
             continue
         row = self.row[i]
         col = self.col[i]
         # * rook part *
         # king is above queen
         if black_king_row < self.row[i] and black_king_col == self.col[i]:
             while row >= 0:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row -= 1
         # below
         elif black_king_row > self.row[i] and black_king_col == self.col[i]:
             while row <= 7:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row += 1
         # left
         elif black_king_col < self.col[i] and black_king_row == self.row[i]:
             while col >= 0:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 col -= 1
         # right
         elif black_king_col > self.col[i] and black_king_row == self.row[i]:
             while col <= 7:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 col += 1
         # * bishop part *
         # lower right
         elif black_king_row > self.row[i] and black_king_col > self.col[i]:
             while row <= 7 and col <= 7:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row += 1
                 col += 1
         # lower left
         elif black_king_row > self.row[i] and black_king_col < self.col[i]:
             while row <= 7 and col >= 0:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row += 1
                 col -= 1
         # upper left
         elif black_king_row < self.row[i] and black_king_col < self.col[i]:
             while row >= 0 and col >= 0:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row -= 1
                 col -= 1
         # upper right
         elif black_king_row < self.row[i] and black_king_col > self.col[i]:
             while row >= 0 and col <= 7:
                 if (row, col) == (black_king_row, black_king_col):
                     return movelist
                 movelist.append((row, col))
                 row -= 1
                 col += 1
Beispiel #9
0
 def update_movelist(self):
     "Queen movelist is the sum of Bishop and Rook moves."
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     for i in range(White.num_queens):
         if self.alive[i] == 0:
             continue
         # Rook part
         self.movelist[i].clear()
         self.protecting_movelist[i].clear()
         row = self.row[i]
         col = self.col[i]
         while row - 1 >= 0:
             if White.blocks[row - 1][col] == 1:
                 self.protecting_movelist[i].append((row - 1, col))
                 break
             if Black.blocks[row - 1][col] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row - 1 and blackking_pos[1] == col:
                     self.movelist[i].append((row - 1, col))
                     self.movelist[i].append((row - 2, col))
                     break
                 self.movelist[i].append((row - 1, col))
                 break
             row -= 1
             self.movelist[i].append((row, col))
         row = self.row[i]
         while row + 1 <= 7:
             if White.blocks[row + 1][col] == 1:
                 self.protecting_movelist[i].append((row + 1, col))
                 break
             if Black.blocks[row + 1][col] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row + 1 and blackking_pos[1] == col:
                     self.movelist[i].append((row + 1, col))
                     self.movelist[i].append((row + 2, col))
                     break
                 self.movelist[i].append((row + 1, col))
                 break
             row += 1
             self.movelist[i].append((row, col))
         row = self.row[i]
         while col - 1 >= 0:
             if White.blocks[row][col - 1] == 1:
                 self.protecting_movelist[i].append((row, col - 1))
                 break
             if Black.blocks[row][col - 1] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row and blackking_pos[1] == col - 1:
                     self.movelist[i].append((row, col - 1))
                     self.movelist[i].append((row, col - 2))
                     break
                 self.movelist[i].append((row, col - 1))
                 break
             col -= 1
             self.movelist[i].append((row, col))
         col = self.col[i]
         while col + 1 <= 7:
             if White.blocks[row][col + 1] == 1:
                 self.protecting_movelist[i].append((row, col + 1))
                 break
             if Black.blocks[row][col + 1] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row and blackking_pos[1] == col + 1:
                     self.movelist[i].append((row, col + 1))
                     self.movelist[i].append((row, col + 2))
                     break
                 self.movelist[i].append((row, col + 1))
                 break
             col += 1
             self.movelist[i].append((row, col))
         # Bishop part
         row = self.row[i]
         col = self.col[i]
         while row - 1 >= 0 and col - 1 >= 0:
             if White.blocks[row - 1][col - 1] == 1:
                 self.protecting_movelist[i].append((row - 1, col - 1))
                 break
             if Black.blocks[row - 1][col - 1] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row - 1 and blackking_pos[
                         1] == col - 1:
                     self.movelist[i].append((row - 1, col - 1))
                     self.movelist[i].append((row - 2, col - 2))
                     break
                 self.movelist[i].append((row - 1, col - 1))
                 break
             row -= 1
             col -= 1
             self.movelist[i].append((row, col))
         row = self.row[i]
         col = self.col[i]
         while row - 1 >= 0 and col + 1 <= 7:
             if White.blocks[row - 1][col + 1] == 1:
                 self.protecting_movelist[i].append((row - 1, col + 1))
                 break
             if Black.blocks[row - 1][col + 1] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row - 1 and blackking_pos[
                         1] == col + 1:
                     self.movelist[i].append((row - 1, col + 1))
                     self.movelist[i].append((row - 2, col + 2))
                     break
                 self.movelist[i].append((row - 1, col + 1))
                 break
             row -= 1
             col += 1
             self.movelist[i].append((row, col))
         row = self.row[i]
         col = self.col[i]
         while row + 1 <= 7 and col + 1 <= 7:
             if White.blocks[row + 1][col + 1] == 1:
                 self.protecting_movelist[i].append((row + 1, col + 1))
                 break
             if Black.blocks[row + 1][col + 1] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row + 1 and blackking_pos[
                         1] == col + 1:
                     self.movelist[i].append((row + 1, col + 1))
                     self.movelist[i].append((row + 2, col + 2))
                     break
                 self.movelist[i].append((row + 1, col + 1))
                 break
             row += 1
             col += 1
             self.movelist[i].append((row, col))
         row = self.row[i]
         col = self.col[i]
         while row + 1 <= 7 and col - 1 >= 0:
             if White.blocks[row + 1][col - 1] == 1:
                 self.protecting_movelist[i].append((row + 1, col - 1))
                 break
             if Black.blocks[row + 1][col - 1] == 1:
                 blackking_pos = Black_.get_pos("K")
                 if blackking_pos[0] == row + 1 and blackking_pos[
                         1] == col - 1:
                     self.movelist[i].append((row + 1, col - 1))
                     self.movelist[i].append((row + 2, col - 2))
                     break
                 self.movelist[i].append((row + 1, col - 1))
                 break
             row += 1
             col -= 1
             self.movelist[i].append((row, col))
Beispiel #10
0
 def king_in_path(self):
     """Determine if king is in path of queen. Used for pinning.
 Assigns a list of directions to self.in_path for where the king's
 position is relative to the queen (see self.dir).
 """
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     for i in range(White.num_queens):
         if self.alive[i] == 0:
             continue
         self.in_path[i] = 0
         col = self.col[i]
         row = self.row[i]
         # Rook part
         while col >= 0:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "L"
             col -= 1
         col = self.col[i]
         row = self.row[i]
         while col <= 7:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "R"
             col += 1
         col = self.col[i]
         row = self.row[i]
         while row >= 0:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "U"
             row -= 1
         col = self.col[i]
         row = self.row[i]
         while row <= 7:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "D"
             row += 1
         # Bishop part
         row = self.row[i]
         col = self.col[i]
         while row >= 0 and col >= 0:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "UL"
             row -= 1
             col -= 1
         row = self.row[i]
         col = self.col[i]
         while row >= 0 and col <= 7:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "UR"
             row -= 1
             col += 1
         row = self.row[i]
         col = self.col[i]
         while row <= 7 and col <= 7:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "LR"
             row += 1
             col += 1
         row = self.row[i]
         col = self.col[i]
         while row <= 7 and col >= 0:
             if (row, col) == Black_.get_pos("K"):
                 self.in_path[i] = "LL"
             row += 1
             col -= 1