示例#1
0
 def isgameover(self, board, colour):  
     if(self.playercolor == "white"):
         if(colour > 0):
             pawn_move = -1
         else:
             pawn_move = 1
     else:
         if(colour < 0):
             pawn_move = 1                        
         else:
             pawn_move = -1
     if(colour < 0): # when satisfied want to check whether white king is locked
         for i in range(8):
             for j in range(8):
                 if(board[i][j] > 0):
                     f = PieceMap.getFun(board[i][j])
                     if(abs(board[i][j]) == 6):
                         moves = f([i,j],board,pawn_move)
                     else:
                         moves = f([i,j],board)
                     start = board[i][j]
                     for m in moves:
                         #changing the board and sending to if check and then again reversing the board
                         end = board[m[0]][m[1]]
                         board[m[0]][m[1]] = start
                         board[i][j] = 0
                         if(not self.ischeck(board,colour *-1)):
                             board[i][j] = start
                             board[m[0]][m[1]] = end
                             return False
                         else:
                             board[i][j] = start
                             board[m[0]][m[1]] = end
                             
     elif(colour > 0): # when satisfied want to check whether black king is locked
         for i in range(8):
             for j in range(8):
                 if(board[i][j] < 0):
                     f = PieceMap.getFun(board[i][j])
                     if(abs(board[i][j]) == 6):
                         moves = f([i,j],board,pawn_move)
                     else:
                         moves = f([i,j],board)
                     start = board[i][j]
                     for m in moves:
                         #changing the board and sending to if check and then again reversing the board
                         end = board[m[0]][m[1]]
                         board[m[0]][m[1]] = start
                         board[i][j] = 0
                         if(not self.ischeck(board, colour * -1)):
                             board[i][j] = start
                             board[m[0]][m[1]] = end
                             return False
                         else:
                             board[i][j] = start
                             board[m[0]][m[1]] = end
                                 
     return True        
示例#2
0
 def quiescence1(self, player, state, alpha, beta, depth):
     best = self.evolution(state, player) * player
     if(best >= beta):
         return best
     if(self.b.playercolor == "white"):
         if(player > 0):
             pawn_move = -1
         else:
             pawn_move = 1
     else:
         if(player < 0):
             pawn_move = 1                        
         else:
             pawn_move = -1
     for i in range(8):
         for j in range(8):
             if(state[i][j] * player > 0):
                 f = PieceMap.getCaptureFun(state[i][j])
                 moves = []
                 if(abs(state[i][j]) == 6):
                     moves = f([i,j],state,pawn_move)
                 elif(abs(state[i][j] != 0)):
                     moves = f([i,j],state)
                 if(depth <= 0 and len(moves) <= 0):
                     continue
                 for move in moves:
                     t = state[move[0]][move[1]]
                     state[move[0]][move[1]] = state[i][j]
                     temp = -self.quiescence1(-player, [[x for x in y] for y in state], -beta, max(alpha, best), depth-1)
                     state[move[0]][move[1]] = t
                     best = max(temp, best)
                     if(best >= beta):
                         return best
     return best
示例#3
0
    def ischeck(self, board, colour):
        if(self.playercolor == "white"):
            if(colour > 0):
                pawn_move = 1
            else:
                pawn_move = -1
        else:
            if(colour < 0):
                pawn_move = -1                        
            else:
                pawn_move = 1
        bK = wK = None
        for i in range(8):
            for j in range(8):
                if(board[i][j] == -1):
                    bK = [i,j]
                elif(board[i][j] == 1):
                    wK = [i,j]

        if(bK == None or wK == None):
            print("ennamo nadakuthu")
            return True
        
        if(colour < 0):   #when satisfied  want to check whether black king in danger
            for i in range(8):
                for j in range(8):
                    if(board[i][j] > 0):
                        f = PieceMap.getFun(board[i][j])
                        if(abs(board[i][j]) == 6):
                            moves = f([i,j],board,pawn_move)
                        else:
                            moves = f([i,j],board)
                        if(bK in moves):
                            return True
        elif(colour > 0):  #when satisfied  want to check whether white king in danger
            for i in range(8):
                for j in range(8):
                    if(board[i][j] < 0 ):
                        f = PieceMap.getFun(board[i][j])
                        if(abs(board[i][j]) == 6):
                            moves = f([i,j],board,pawn_move)
                        else:
                            moves = f([i,j],board)
                        if(wK in moves):
                            return True
        return False
示例#4
0
    def evolution2(self, board, player):
        wmobility = bmobility = piecescore = 0
        if (self.b.playercolor == "white"):
            if (player > 0):
                pawn_move = -1
            else:
                pawn_move = 1
        else:
            if (player < 0):
                pawn_move = 1
            else:
                pawn_move = -1

        MD = PA = MT = 0
        for i in range(8):
            for j in range(8):
                if (board[i][j] > 0):
                    MD += PieceMap.getPoints(board[i][j])
                    MT += PieceMap.getPoints(board[i][j])
                elif (board[i][j] < 0):
                    MD -= PieceMap.getPoints(board[i][j])
                    MT += PieceMap.getPoints(board[i][j])
                if (board[i][j] * player < 0 and abs(board[i][j]) == 6):
                    PA += 1

                if (player > 0):
                    f = PieceMap.getFun(board[i][j])
                    moves = []
                    if (abs(board[i][j]) == 6):
                        moves = f([i, j], board, pawn_move)
                    elif (abs(board[i][j] != 0)):
                        moves = f([i, j], board)
                    wmobility += len(moves)
                else:
                    f = PieceMap.getFun(board[i][j])
                    moves = []
                    if (abs(board[i][j]) == 6):
                        moves = f([i, j], board, pawn_move)
                    elif (abs(board[i][j]) != 0):
                        moves = f([i, j], board)
                    bmobility += len(moves)

                if (board[i][j] * player > 0):
                    piecescore += PieceMap.getPieceScore(
                        board[i][j], i, j, player)
        MD = abs(MD)

        MS = min(24000, MD) + ((MD * PA * (80000 - MT)) / (64000 * (PA + 1)))
        materialscore = min(31000, MS)
        mobilityscore = 0.1 * (wmobility - bmobility) * player

        return (materialscore + mobilityscore) * player
示例#5
0
    def evolution3(self, board, player):
        materialscore = wmobility = bmobility = piecescore = 0
        if (self.b.playercolor == "white"):
            if (player > 0):
                pawn_move = -1
            else:
                pawn_move = 1
        else:
            if (player < 0):
                pawn_move = 1
            else:
                pawn_move = -1

        for i in range(8):
            for j in range(8):
                if (player > 0):
                    if (board[i][j] > 0):
                        materialscore += PieceMap.getPoints(board[i][j])
                    elif (board[i][j] < 0):
                        materialscore -= PieceMap.getPoints(board[i][j])
                    f = PieceMap.getFun(board[i][j])
                    moves = []
                    if (abs(board[i][j]) == 6):
                        moves = f([i, j], board, pawn_move)
                    elif (abs(board[i][j] != 0)):
                        moves = f([i, j], board)
                    wmobility += len(moves)
                else:
                    if (board[i][j] > 0):
                        materialscore -= PieceMap.getPoints(board[i][j])
                    elif (board[i][j] < 0):
                        materialscore += PieceMap.getPoints(board[i][j])
                    f = PieceMap.getFun(board[i][j])
                    moves = []
                    if (abs(board[i][j]) == 6):
                        moves = f([i, j], board, pawn_move)
                    elif (abs(board[i][j]) != 0):
                        moves = f([i, j], board)
                    bmobility += len(moves)

                if (board[i][j] * player > 0):
                    piecescore += PieceMap.getPieceScore(
                        board[i][j], i, j, player)

        mobilityscore = 0.1 * (wmobility - bmobility) * player

        return (materialscore + mobilityscore + piecescore) * player
示例#6
0
 def capture_successor(self, colour, board): 
     allmoves = []
     if(self.playercolor == "white"):
         if(colour > 0):
             pawn_move = -1
         else:
             pawn_move = 1
     else:
         if(colour < 0):
             pawn_move = 1                        
         else:
             pawn_move = -1
             
     if(colour > 0): 
         for i in range(8):
             for j in range(8):
                 if(board[i][j] > 0):
                     start = board[i][j]
                     #f = PieceMap.getFun(board[i][j])
                     f = PieceMap.getCaptureFun(board[i][j])
                     if(abs(board[i][j]) == 6):
                         moves = f([i,j],board,pawn_move)
                     else:
                         moves = f([i,j],board)
                     '''
                     if(len(moves) <= 0):
                         f = PieceMap.getFun(board[i][j])
                         if(abs(board[i][j]) == 6):
                             moves = f([i,j],board,pawn_move)
                         else:
                             moves = f([i,j],board)
                     '''        
                     for move in moves:
                         end = board[move[0]][move[1]]
                         board[i][j] = 0
                         board[move[0]][move[1]] = start
                         if(not self.ischeck(board, colour)):
                             allmoves.append([[_i for _i in _j] for _j in board])
                         board[i][j] = start
                         board[move[0]][move[1]] = end
     elif(colour < 0):
         for i in range(8):
             for j in range(8):
                 if(board[i][j] < 0):
                     start = board[i][j]
                     f = PieceMap.getFun(board[i][j])
                     #f = PieceMap.getCaptureFun(board[i][j])
                     if(abs(board[i][j]) == 6):
                         moves = f([i,j],board,pawn_move)
                     else:
                         moves = f([i,j],board)
                     '''
                     if(len(moves) <= 0):
                         f = PieceMap.getFun(board[i][j])
                         if(abs(board[i][j]) == 6):
                             moves = f([i,j],board,pawn_move)
                         else:
                             moves = f([i,j],board) 
                     '''
                     for move in moves:
                         end = board[move[0]][move[1]]
                         board[i][j] = 0
                         board[move[0]][move[1]] = start
                         if(not self.ischeck(board, colour)):
                             allmoves.append([[_i for _i in _j] for _j in board])
                         board[i][j] = start
                         board[move[0]][move[1]] = end
                         
     return allmoves