Example #1
0
 def filter_king_positions(self):
     "Kings can never be next to each other."
     from pieces.white_funcs import White_Funcs
     White_ = White_Funcs()
     whiteking_row = White_.get_pos("K")[0]
     whiteking_col = White_.get_pos("K")[1]
     avoid_moves = [(whiteking_row - 1, whiteking_col - 1),
                    (whiteking_row - 1, whiteking_col),
                    (whiteking_row - 1, whiteking_col + 1),
                    (whiteking_row, whiteking_col - 1),
                    (whiteking_row, whiteking_col + 1),
                    (whiteking_row + 1, whiteking_col - 1),
                    (whiteking_row + 1, whiteking_col),
                    (whiteking_row + 1, whiteking_col + 1)]
     for i in avoid_moves:
         if i in self.movelist:
             self.movelist.remove(i)
Example #2
0
 def build_check_movelist(self):
     """White king has been checked by a bishop. Build and return a movelist going
 in the direction of the white king.
 """
     from pieces.white_funcs import White_Funcs
     White_ = White_Funcs()
     white_king_row = White_.get_pos("K")[0]
     white_king_col = White_.get_pos("K")[1]
     movelist = []
     for i in range(2):
         if Black.checker != "B" + str(i):
             continue
         row = self.row[i]
         col = self.col[i]
         # king in lower right (relative to bishop)
         if white_king_row > self.row[i] and white_king_col > self.col[i]:
             while row <= 7 and col <= 7:
                 if (row, col) == (white_king_row, white_king_col):
                     return movelist
                 movelist.append((row, col))
                 row += 1
                 col += 1
         # lower left
         elif white_king_row > self.row[i] and white_king_col < self.col[i]:
             while row <= 7 and col >= 0:
                 if (row, col) == (white_king_row, white_king_col):
                     return movelist
                 movelist.append((row, col))
                 row += 1
                 col -= 1
         # upper left
         elif white_king_row < self.row[i] and white_king_col < self.col[i]:
             while row >= 0 and col >= 0:
                 if (row, col) == (white_king_row, white_king_col):
                     return movelist
                 movelist.append((row, col))
                 row -= 1
                 col -= 1
         # upper right
         elif white_king_row < self.row[i] and white_king_col > self.col[i]:
             while row >= 0 and col <= 7:
                 if (row, col) == (white_king_row, white_king_col):
                     return movelist
                 movelist.append((row, col))
                 row -= 1
                 col += 1
Example #3
0
 def get_pinned_piece(self, dir):
     "A piece has been pinned in direction dir. Get that piece."
     from pieces.white_funcs import White_Funcs
     White_ = White_Funcs()
     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 White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     return White_.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 White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     return White_.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 White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     return White_.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 White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     return White_.get_piece(row, col)
                 row += 1
                 col -= 1
Example #4
0
 def update_movelist(self):
   from pieces.white_funcs import White_Funcs
   White_ = White_Funcs()
   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 <= 7:
       if White.blocks[self.row[i]+1][self.col[i]] == 0 and \
       Black.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 <= 7 and self.col[i]-1 >= 0:
       self.hit_movelist[i].append((self.row[i]+1, self.col[i]-1))
       if White.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 <= 7 and self.col[i]+1 <= 7:
       self.hit_movelist[i].append((self.row[i]+1, self.col[i]+1))
       if White.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] == 1 and White.blocks[self.row[i]+1][self.col[i]] == 0 and \
     Black.blocks[self.row[i]+1][self.col[i]] == 0 and \
     White.blocks[self.row[i]+2][self.col[i]] == 0 and \
     Black.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] == 4:
       for k in range(len(White.en_passant)):
         if White.en_passant[k] == 1:
           pos = White_.get_pos("P" + str(k))
           if abs(pos[1] - self.col[i]) == 1:
             self.movelist[i].append((pos[0] + 1, pos[1]))
Example #5
0
 def check_insuffience_mat(self):
     """A draw by insufficient material occurs if both sides have any of:
 - A lone king
 - A king and bishop
 - A king and knight
 - A king and 2 knights
 and no pawns.
 """
     from pieces.black_funcs import Black_Funcs
     Black_ = Black_Funcs(None)
     from pieces.white_funcs import White_Funcs
     White_ = White_Funcs()
     if (Black_.lone_king() or Black_.one_bishop() or Black_.two_knights() or \
     Black_.one_knight()) and (White_.lone_king() or White_.one_bishop() or \
     White_.two_knights() or White_.one_knight()):
         return True
     return False
Example #6
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.white_funcs import White_Funcs
     White_ = White_Funcs()
     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) == White_.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) == White_.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) == White_.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) == White_.get_pos("K"):
                 self.in_path[i] = 1
                 return "LL"
             row += 1
             col -= 1
Example #7
0
 def king_in_path(self):
     """Determine if king is in path of rook. Used for pinning.
 Returns "L", "R", "U", "D" for where the king's position is relative
 to the rook (left, right, up, down).
 Also sets the in_path flag for the rook to 1.
 """
     from pieces.white_funcs import White_Funcs
     White_ = White_Funcs()
     for i in range(2):
         if self.alive[i] == 0:
             continue
         self.in_path[i] = 0
         col = self.col[i]
         row = self.row[i]
         while col >= 0:
             if (row, col) == White_.get_pos("K"):
                 self.in_path[i] = "L"
             col -= 1
         col = self.col[i]
         row = self.row[i]
         while col <= 7:
             if (row, col) == White_.get_pos("K"):
                 self.in_path[i] = "R"
             col += 1
         col = self.col[i]
         row = self.row[i]
         while row >= 0:
             if (row, col) == White_.get_pos("K"):
                 self.in_path[i] = "U"
             row -= 1
         col = self.col[i]
         row = self.row[i]
         while row <= 7:
             if (row, col) == White_.get_pos("K"):
                 self.in_path[i] = "D"
             row += 1
Example #8
0
    def update_movelist(self):
        from pieces.white_funcs import White_Funcs
        White_ = White_Funcs()

        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]
            # up
            while row - 1 >= 0:
                if Black.blocks[row - 1][col] == 1:
                    self.protecting_movelist[i].append((row - 1, col))
                    break
                if White.blocks[row - 1][col] == 1:
                    whiteking_pos = White_.get_pos("K")
                    if whiteking_pos[0] == row - 1 and whiteking_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]
            # down
            while row + 1 <= 7:
                if Black.blocks[row + 1][col] == 1:
                    self.protecting_movelist[i].append((row + 1, col))
                    break
                if White.blocks[row + 1][col] == 1:
                    whiteking_pos = White_.get_pos("K")
                    if whiteking_pos[0] == row + 1 and whiteking_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]
            # left
            while col - 1 >= 0:
                if Black.blocks[row][col - 1] == 1:
                    self.protecting_movelist[i].append((row, col - 1))
                    break
                if White.blocks[row][col - 1] == 1:
                    whiteking_pos = White_.get_pos("K")
                    if whiteking_pos[0] == row and whiteking_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]
            # right
            while col + 1 <= 7:
                if Black.blocks[row][col + 1] == 1:
                    self.protecting_movelist[i].append((row, col + 1))
                    break
                if White.blocks[row][col + 1] == 1:
                    whiteking_pos = White_.get_pos("K")
                    if whiteking_pos[0] == row and whiteking_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))
Example #9
0
 def build_check_movelist(self):
   """White king has been checked by queen. Build and return a movelist going
   in the direction of the white king.
   """
   from pieces.white_funcs import White_Funcs
   White_ = White_Funcs()
   white_king_row = White_.get_pos("K")[0]
   white_king_col = White_.get_pos("K")[1]
   movelist = []
   for i in range(Black.num_queens):
     if Black.checker != "Q" + str(i):
       continue
     row = self.row[i]
     col = self.col[i]
     # * rook part *
     # king is above queen
     if white_king_row < self.row[i] and white_king_col == self.col[i]:
       while row >= 0:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         row -= 1
     # below
     elif white_king_row > self.row[i] and white_king_col == self.col[i]:
       while row <= 7:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         row += 1
     # left
     elif white_king_col < self.col[i] and white_king_row == self.row[i]:
       while col >= 0:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         col -= 1
     # right
     elif white_king_col > self.col[i] and white_king_row == self.row[i]:
       while col <= 7:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         col += 1
     # * bishop part *
     # lower right
     elif white_king_row > self.row[i] and white_king_col > self.col[i]:
       while row <= 7 and col <= 7:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         row += 1
         col += 1
     # lower left
     elif white_king_row > self.row[i] and white_king_col < self.col[i]:
       while row <= 7 and col >= 0:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         row += 1
         col -= 1
     # upper left
     elif white_king_row < self.row[i] and white_king_col < self.col[i]:
       while row >= 0 and col >= 0:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         row -= 1
         col -= 1
     # upper right
     elif white_king_row < self.row[i] and white_king_col > self.col[i]:
       while row >= 0 and col <= 7:
         if (row, col) == (white_king_row, white_king_col):
           return movelist
         movelist.append((row, col))
         row -= 1
         col += 1
Example #10
0
 def pin_piece(self, pinned_piece, dir):
   """A white piece is pinned between queen and king in direction dir.
   Pin that piece by filtering any moves that are not in the queen's pinned_movelist.
   """
   from pieces.white_funcs import White_Funcs
   White_ = White_Funcs()
   for i in range(Black.num_queens):
     if self.in_path[i] == 0 or self.alive[i] == 0:
       continue
     self.pinned_movelist[i].clear()
     # Rook part
     if dir == "L":
       row = self.row[i]
       col = self.col[i]
       while col >= 0:
         if (row, col) == White_.get_pos("K"):
           White_.filter(pinned_piece, self.pinned_movelist[i])
           return
         self.pinned_movelist[i].append((row, col))
         col -= 1
     elif dir == "R":
       row = self.row[i]
       col = self.col[i]
       while col <= 7:
         if (row, col) == White_.get_pos("K"):
           White_.filter(pinned_piece, self.pinned_movelist[i])
           return
         self.pinned_movelist[i].append((row, col))
         col += 1
     elif dir == "U":
       row = self.row[i]
       col = self.col[i]
       while row >= 0:
         if (row, col) == White_.get_pos("K"):
           White_.filter(pinned_piece, self.pinned_movelist[i])
           return
         self.pinned_movelist[i].append((row, col))
         row -= 1
     elif dir == "D":
       row = self.row[i]
       col = self.col[i]
       while row <= 7:
         if (row, col) == White_.get_pos("K"):
           White_.filter(pinned_piece, self.pinned_movelist[i])
           return
         self.pinned_movelist[i].append((row, col))
         row += 1
     # Bishop part
     elif dir == "UL":
       row = self.row[i]
       col = self.col[i]
       while row >= 0 and col >= 0:
         if (row, col) == White_.get_pos("K"):
           White_.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) == White_.get_pos("K"):
           White_.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) == White_.get_pos("K"):
           White_.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) == White_.get_pos("K"):
           White_.filter(pinned_piece, self.pinned_movelist[i])
           return
         self.pinned_movelist[i].append((row, col))
         row += 1
         col -= 1
Example #11
0
 def update_movelist(self):
   "Queen movelist is the sum of Bishop and Rook moves."
   from pieces.white_funcs import White_Funcs
   White_ = White_Funcs()
   for i in range(Black.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 Black.blocks[row-1][col] == 1:
         self.protecting_movelist[i].append((row-1, col))
         break
       if White.blocks[row-1][col] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row-1 and whiteking_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 Black.blocks[row+1][col] == 1:
         self.protecting_movelist[i].append((row+1, col))
         break
       if White.blocks[row+1][col] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row+1 and whiteking_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 Black.blocks[row][col-1] == 1:
         self.protecting_movelist[i].append((row, col-1))
         break
       if White.blocks[row][col-1] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row and whiteking_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 Black.blocks[row][col+1] == 1:
         self.protecting_movelist[i].append((row, col+1))
         break
       if White.blocks[row][col+1] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row and whiteking_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 Black.blocks[row-1][col-1] == 1:
         self.protecting_movelist[i].append((row-1, col-1))
         break
       if White.blocks[row-1][col-1] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row-1 and whiteking_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 Black.blocks[row-1][col+1] == 1:
         self.protecting_movelist[i].append((row-1, col+1))
         break
       if White.blocks[row-1][col+1] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row-1 and whiteking_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 Black.blocks[row+1][col+1] == 1:
         self.protecting_movelist[i].append((row+1, col+1))
         break
       if White.blocks[row+1][col+1] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row+1 and whiteking_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 Black.blocks[row+1][col-1] == 1:
         self.protecting_movelist[i].append((row+1, col-1))
         break
       if White.blocks[row+1][col-1] == 1:
         whiteking_pos = White_.get_pos("K")
         if whiteking_pos[0] == row+1 and whiteking_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))
Example #12
0
 def num_pieces(self, dir):
   """Determine the number of white pieces between queen and black
   king in direction dir.
   """
   from pieces.white_funcs import White_Funcs
   White_ = White_Funcs()
   for i in range(Black.num_queens):
     if self.in_path[i] == 0 or self.alive[i] == 0:
       continue
     # Rook part
     if dir == "L":
       row = self.row[i]
       col = self.col[i]
       num = 0
       num_blocks = 0
       while col >= 0:
         if Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.get_pos("K"):
           return num
         col -= 1
         num_blocks += 1
     elif dir == "R":
       row = self.row[i]
       col = self.col[i]
       num = 0
       num_blocks = 0
       while col <= 7:
         if Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.get_pos("K"):
           return num
         col += 1
         num_blocks += 1
     elif dir == "U":
       row = self.row[i]
       col = self.col[i]
       num = 0
       num_blocks = 0
       while row >= 0:
         if Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.get_pos("K"):
           return num
         row -= 1
         num_blocks += 1
     elif dir == "D":
       row = self.row[i]
       col = self.col[i]
       num = 0
       num_blocks = 0
       while row <= 7:
         if Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.get_pos("K"):
           return num
         row += 1
         num_blocks += 1
     # Bishop part
     elif dir == "UL":
       row = self.row[i]
       col = self.col[i]
       num = 0
       num_blocks = 0
       while row >= 0 and col >= 0:
         if Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.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 Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.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 Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.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 Black.blocks[row][col] == 1 and num_blocks > 0:
           return -1
         elif White.blocks[row][col] == 1 and (row, col) != White_.get_pos("K"):
           num += 1
         elif (row, col) == White_.get_pos("K"):
           return num
         row += 1
         col -= 1
         num_blocks += 1
Example #13
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.white_funcs import White_Funcs
   White_ = White_Funcs()
   for i in range(Black.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) ==  White_.get_pos("K"):
         self.in_path[i] = "L"
       col -= 1
     col = self.col[i]
     row = self.row[i]
     while col <= 7:
       if (row, col) ==  White_.get_pos("K"):
         self.in_path[i] = "R"
       col += 1
     col = self.col[i]
     row = self.row[i]
     while row >= 0:
       if (row, col) ==  White_.get_pos("K"):
         self.in_path[i] = "U"
       row -= 1
     col = self.col[i]
     row = self.row[i]
     while row <= 7:
       if (row, col) ==  White_.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) ==  White_.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) ==  White_.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) ==  White_.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) ==  White_.get_pos("K"):
         self.in_path[i] = "LL"
       row += 1
       col -= 1
Example #14
0
    def update_movelist(self):
        from pieces.white_funcs import White_Funcs
        White_ = White_Funcs()

        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 Black.blocks[row - 1][col - 1] == 1:
                    self.protecting_movelist[i].append((row - 1, col - 1))
                    break
                if White.blocks[row - 1][col - 1] == 1:
                    blackking_pos = White_.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 Black.blocks[row - 1][col + 1] == 1:
                    self.protecting_movelist[i].append((row - 1, col + 1))
                    break
                if White.blocks[row - 1][col + 1] == 1:
                    blackking_pos = White_.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 Black.blocks[row + 1][col + 1] == 1:
                    self.protecting_movelist[i].append((row + 1, col + 1))
                    break
                if White.blocks[row + 1][col + 1] == 1:
                    blackking_pos = White_.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 Black.blocks[row + 1][col - 1] == 1:
                    self.protecting_movelist[i].append((row + 1, col - 1))
                    break
                if White.blocks[row + 1][col - 1] == 1:
                    blackking_pos = White_.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))
Example #15
0
WhitePawn = White_Pawn()
WhiteRook = White_Rook()
WhiteQueen = White_Queen()
WhiteKnight = White_Knight()

from pieces.black.blackking import Black_King
from pieces.white.whiteking import White_King
BlackKing = Black_King()
WhiteKing = White_King()

from board import Game_Board
Board = Game_Board()

from pieces.white_funcs import White_Funcs
from pieces.black_funcs import Black_Funcs
White_ = White_Funcs()
Black_ = Black_Funcs(White_)

from ai_helper import Helper
helper = Helper()
from black_ai import Black_AI
BlackAI = Black_AI()
from white_ai import White_AI
WhiteAI = White_AI()


def init():
    if env == "" or env == None:
        return
    elif env == "only_pawns0":
        Black_.destroy("Q0")
Example #16
0
    def undo(self):
        "Undo the last move played. Used for AI move generation in tree traversal."
        from pieces.black_funcs import Black_Funcs
        Black_ = Black_Funcs(None)
        from pieces.white_funcs import White_Funcs
        White_ = White_Funcs()
        if len(self.moves["piece"]) == 0:
            print("Nothing to undo!")
            return
        if (Black.ai or White.ai) and self.freeze and len(
                self.moves["piece"]) <= self.total_moves:
            print("AI undo'ing too far") if verbose else None
            return
        if (self.checkmate or self.stalemate
                or self.insufficient_mat) and self.freeze == False:
            self.checkmate = False
            self.stalemate = False
            self.insufficient_mat = False
        (row, col) = (self.moves["pos"][-1][0], self.moves["pos"][-1][1])
        piece = self.moves["piece"][-1]
        # * Black *
        # move back
        if self.moves["color"][-1] == "B":
            if piece == "K":
                BlackKing.Move(row, col)
            elif piece == "CK":
                BlackKing.Move(0, 4)
                BlackRook.Move(1, 0, 7)
                Black.castled = 0
                BlackRook.moved[1] = 0
                BlackKing.moved = 0
            elif piece == "CQ":
                BlackKing.Move(0, 4)
                BlackRook.Move(0, 0, 0)
                Black.castled = 0
                BlackRook.moved[0] = 0
                BlackKing.moved = 0
            for i in range(Black.num_queens):
                if piece == "Q" + str(i):
                    BlackQueen.Move(i, row, col)
            for i in range(8):
                if piece == "P" + str(i):
                    if row == 6:
                        # use black queen pos since it was promoted to q
                        Black.blocks[BlackQueen.row[-1]][
                            BlackQueen.col[-1]] = 0
                        Black_.unpromote(i, row, col)
                    else:
                        BlackPawn.Move(i, row, col)
            for i in range(2):
                if piece == "B" + str(i):
                    BlackBishop.Move(i, row, col)
                elif piece == "R" + str(i):
                    BlackRook.Move(i, row, col)
                elif piece == "N" + str(i):
                    BlackKnight.Move(i, row, col)
            # revive if piece killed
            if self.moves["killed"][-1] == True:
                if self.killed["piece"][-1] == "K":
                    WhiteKing.alive = 1
                    WhiteKing.Move(self.killed["pos"][-1][0],
                                   self.killed["pos"][-1][1])
                for i in range(White.num_queens):
                    if self.killed["piece"][-1] == "Q" + str(i):
                        WhiteQueen.alive[i] = 1
                        WhiteQueen.Move(i, self.killed["pos"][-1][0],
                                        self.killed["pos"][-1][1])
                for i in range(8):
                    if self.killed["piece"][-1] == "P" + str(i):
                        WhitePawn.alive[i] = 1
                        WhitePawn.Move(i, self.killed["pos"][-1][0],
                                       self.killed["pos"][-1][1])
                for i in range(2):
                    if self.killed["piece"][-1] == "B" + str(i):
                        WhiteBishop.alive[i] = 1
                        WhiteBishop.Move(i, self.killed["pos"][-1][0],
                                         self.killed["pos"][-1][1])
                    elif self.killed["piece"][-1] == "N" + str(i):
                        WhiteKnight.alive[i] = 1
                        WhiteKnight.Move(i, self.killed["pos"][-1][0],
                                         self.killed["pos"][-1][1])
                    elif self.killed["piece"][-1] == "R" + str(i):
                        WhiteRook.alive[i] = 1
                        WhiteRook.Move(i, self.killed["pos"][-1][0],
                                       self.killed["pos"][-1][1])
                self.killed["piece"].pop(-1)
                self.killed["pos"].pop(-1)
            self.update_all_moves()
            Black_.check_white_pin()
            if Black_.check_white_check():
                White_.update_check_movelists()
            Black_.reset_enpassant()
            self.moves["color"].pop(-1)
            self.moves["piece"].pop(-1)
            self.moves["pos"].pop(-1)
            self.moves["killed"].pop(-1)

        # * White *
        # move back
        elif self.moves["color"][-1] == "W":
            if piece == "K":
                WhiteKing.Move(row, col)
            elif piece == "CK":
                WhiteKing.Move(7, 4)
                WhiteRook.Move(1, 7, 7)
                White.castled = 0
                WhiteRook.moved[1] = 0
                WhiteKing.moved = 0
            elif piece == "CQ":
                WhiteKing.Move(7, 4)
                WhiteRook.Move(0, 7, 0)
                White.castled = 0
                WhiteRook.moved[0] = 0
                WhiteKing.moved = 0
            for i in range(White.num_queens):
                if piece == "Q" + str(i):
                    WhiteQueen.Move(i, row, col)
            for i in range(8):
                if piece == "P" + str(i):
                    if row == 1:
                        # use white queen pos since it was promoted to q
                        White.blocks[WhiteQueen.row[-1]][
                            WhiteQueen.col[-1]] = 0
                        White_.unpromote(i, row, col)
                    else:
                        WhitePawn.Move(i, row, col)
            for i in range(2):
                if piece == "B" + str(i):
                    WhiteBishop.Move(i, row, col)
                elif piece == "R" + str(i):
                    WhiteRook.Move(i, row, col)
                elif piece == "N" + str(i):
                    WhiteKnight.Move(i, row, col)
            # revive if piece killed
            if self.moves["killed"][-1] == True:
                if self.killed["piece"][-1] == "K":
                    BlackKing.alive = 1
                    BlackKing.Move(self.killed["pos"][-1][0],
                                   self.killed["pos"][-1][1])
                for i in range(Black.num_queens):
                    if self.killed["piece"][-1] == "Q" + str(i):
                        BlackQueen.alive[i] = 1
                        BlackQueen.Move(i, self.killed["pos"][-1][0],
                                        self.killed["pos"][-1][1])
                for i in range(8):
                    if self.killed["piece"][-1] == "P" + str(i):
                        BlackPawn.alive[i] = 1
                        BlackPawn.Move(i, self.killed["pos"][-1][0],
                                       self.killed["pos"][-1][1])
                for i in range(2):
                    if self.killed["piece"][-1] == "B" + str(i):
                        BlackBishop.alive[i] = 1
                        BlackBishop.Move(i, self.killed["pos"][-1][0],
                                         self.killed["pos"][-1][1])
                    elif self.killed["piece"][-1] == "N" + str(i):
                        BlackKnight.alive[i] = 1
                        BlackKnight.Move(i, self.killed["pos"][-1][0],
                                         self.killed["pos"][-1][1])
                    elif self.killed["piece"][-1] == "R" + str(i):
                        BlackRook.alive[i] = 1
                        BlackRook.Move(i, self.killed["pos"][-1][0],
                                       self.killed["pos"][-1][1])
                self.killed["piece"].pop(-1)
                self.killed["pos"].pop(-1)
            self.update_all_moves()
            White_.check_black_pin()
            if White_.check_black_check():
                Black_.update_check_movelists()
            White_.reset_enpassant()
            self.moves["color"].pop(-1)
            self.moves["piece"].pop(-1)
            self.moves["pos"].pop(-1)
            self.moves["killed"].pop(-1)
        if not self.freeze:
            self.total_moves -= 1
Example #17
0
 def num_pieces(self, dir):
     """Determine the number of black pieces between rook and white
 king in direction dir.
 """
     from pieces.white_funcs import White_Funcs
     White_ = White_Funcs()
     for i in range(2):
         if self.in_path[i] == 0 or self.alive[i] == 0:
             continue
         if dir == "L":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             black_found = 0
             while col >= 0:
                 if Black.blocks[row][col] == 1 and num_blocks > 0:
                     black_found = 1
                     break
                 elif White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     num += 1
                 elif (row, col) == White_.get_pos("K"):
                     return num
                 col -= 1
                 num_blocks += 1
             if black_found:
                 continue
         elif dir == "R":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             black_found = 0
             while col <= 7:
                 if Black.blocks[row][col] == 1 and num_blocks > 0:
                     black_found = 1
                     break
                 elif White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     num += 1
                 elif (row, col) == White_.get_pos("K"):
                     return num
                 col += 1
                 num_blocks += 1
             if black_found:
                 continue
         elif dir == "U":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             black_found = 0
             while row >= 0:
                 if Black.blocks[row][col] == 1 and num_blocks > 0:
                     black_found = 1
                     break
                 elif White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     num += 1
                 elif (row, col) == White_.get_pos("K"):
                     return num
                 row -= 1
                 num_blocks += 1
             if black_found:
                 continue
         elif dir == "D":
             row = self.row[i]
             col = self.col[i]
             num = 0
             num_blocks = 0
             black_found = 0
             while row <= 7:
                 if Black.blocks[row][col] == 1 and num_blocks > 0:
                     black_found = 1
                     break
                 elif White.blocks[row][col] == 1 and (
                         row, col) != White_.get_pos("K"):
                     num += 1
                 elif (row, col) == White_.get_pos("K"):
                     return num
                 row += 1
                 num_blocks += 1
             if black_found:
                 continue