예제 #1
0
    def _locked_pawns(fen):
        """
        A pawn is locked iff. it cannot move forward because opponent's pawn occupy the square in front and cannot move
        diagonally, i.e. make a capture, because no such capture is available, regardless of whose turn is it.
        """

        board = chess.Board(fen)
        board_opposite_turn = board.copy()
        board_opposite_turn.turn = not board.turn

        locked_pawns = set()
        for square in chess.scan_forward(board.pawns):
            color = board.piece_at(square).color
            square_in_front = square + 8 if color == chess.WHITE else square - 8
            if not (0 <= square_in_front < 64):
                continue
            piece_in_front = board.piece_at(square_in_front)
            from_mask = chess.BB_SQUARES[square]
            if (piece_in_front is not None
                    and piece_in_front.piece_type == chess.PAWN
                    and piece_in_front.color != color
                    and not list(board.generate_legal_captures(from_mask))
                    and not list(
                        board_opposite_turn.generate_legal_captures(from_mask))
                ):
                locked_pawns.add(square)
        return locked_pawns
예제 #2
0
 def get_move(self, board, player, numAgents):
     score = 0
     choice = -1
     min = 100
     max = -100
     agents = range(0,self.k)
     for col in board.get_valid_columns():
         score = 0
         state = board.copy()
         state.do_move(col,player)
         #print("starting choice")
         for agent in self.agentList:
         #for cycle in range(0,numAgents):
             #agent = random.choice(agents)
             #score = score + self.agentList[agent].calc_nn_move(state, player)
             score = score + agent.calc_nn_move(state, player)
             #print("Using agent ",agent)
         if(player==1):
             if(score < min):
                 min = score
                 choice = col
         else:
             if(score > max):
                 max = score
                 choice = col
     return choice
예제 #3
0
 def simulate_game(self, board, player):
     state = board.copy()
     currentPlayer = player
     while (not (state.check_win(currentPlayer) or state.check_tie())):
         currentPlayer = 3 - currentPlayer
         move = self.forest.get_learned_move(state, player)
         #print(currentPlayer, move)
         state.do_move(move,currentPlayer)
     return 2 * (1.5 - currentPlayer)
예제 #4
0
 def move_helper_check(self,board,player,column, calls, alpha, beta):
     state = board.copy()
     state.do_move(column, player)
     if(state.check_tie()):
         return 0.0
     if(state.check_win(player)):
         return (player - 1.5) * 2
     if(calls > 1):
         return self.static_board_eval(state,player)
     return self.move_helper_explore(state, 3 - player , calls, alpha, beta)
예제 #5
0
 def learn_move(self, board, player):
     min = 100
     max = -100
     v = 0
     choice = 1
     columns = board.get_valid_columns()
     for col in columns:
         state = board.copy()
         state.do_move(col,player)
         score = self.calc_nn_move(state, player)
         if(player==1):
             if(score < min):
                 min = score
                 choice = col
                 v = min
         else:
             if(score > max):
                 max = score
                 choice = col
                 v = max
     #print(v, choice, player)
     return choice
예제 #6
0
 def monte_carlo(self, board, player):
     min = 100
     max = -100
     choice = 1
     columns = board.get_valid_columns()
     for col in columns:
         #print("column" , col)
         score = 0
         for rep in range(0,5):
             state = board.copy()
             state.do_move(col,player)
             score = score + self.simulate_game(state, player)
         #print(score)
         if(player==1):
             if(score < min):
                 min = score
                 choice = col
         else:
             if(score > max):
                 max = score
                 choice = col
     #print(v, choice, player)
     return choice
예제 #7
0
 def get_move_minMax(self, board, player):
     state = board.copy()
     alpha = -1
     beta = 1
     max = -1
     min = 1
     maxCols = [random.randint(1,self.game_size)]
     columns = state.get_valid_columns()
     for col in columns:
         temp = self.move_helper_check(state, player, col, 1, alpha, beta)
         if(player==2):
             if(temp == max):
                 maxCols.append(col)
             if(temp > max):
                 maxCols = [col]
                 max = temp
         else:
             if(temp == min):
                 maxCols.append(col)
             if(temp < min):
                 maxCols = [col]
                 min = temp 
     return maxCols[random.randint(1,self.game_size) % len(maxCols)]
예제 #8
0
 def move_helper_explore(self, board, player, calls, alpha, beta):
     state = board.copy()
     maxi = -1
     mini = 1
     ret = 0
     columns = state.get_valid_columns()
     for col in columns:
         temp = self.move_helper_check(state, player, col, calls+1, alpha, beta)
         #print(temp, calls, player)
         if(player==1):
             if(temp < mini):
                 mini = temp
                 ret = mini
                 beta = min(beta,mini)
             if(temp < alpha):
                 return temp
         else:
             if(temp > maxi):
                 maxi = temp
                 ret = maxi
                 alpha = max(alpha,maxi)
             if(temp > beta):
                 return temp
     return ret
예제 #9
0
        def recurse(move: int, board: board.Board, depth: int,
                    current_player: int, node: tree.Node):
            """
            Recursively create a tree

            :param move: current move
            :param board: current board state
            :param depth: current depth
            :param current_player: current player
            :param node: parent node for this move
            :return:
            """
            if move is not None:
                node = ComputerController.play_node(player, board, move,
                                                    current_player, node)
                if abs(
                        node.status
                ) == board.WIN:  # if win occurs this new node is a leaf in the tree
                    return  # we never encounter board.LOSS as a state because it's impossible to lose when it's your move
            for m in board.valid_moves:
                if depth < max_depth:
                    recurse(
                        m, board.copy(), depth + 1, current_player * -1, node
                    )  # create a subtree for each valid move of the current board state
예제 #10
0
]

square = 50
pygame.init()
screen = pygame.display.set_mode((square * 9, square * 9))
clock = pygame.time.Clock()
running = True
board = board.Board(screen, square)
solver = solver.Solver(board)

pygame.draw.rect(screen, (255, 255, 255), (0, 0, square * 9, square * 9))

for row in range(1, 9):
    pygame.draw.line(screen, (0, 0, 0), (square * row, 0), (square * row, square * 9), 5 if row % 3 == 0 else 1)
for col in range(1, 9):
    pygame.draw.line(screen, (0, 0, 0), (0, square * col), (square * 9, square * col), 5 if col % 3 == 0 else 1)

board.copy(puzzle)

while running:

    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
        if e.type == pygame.KEYDOWN:
            solver.start()

    pygame.display.update()
    clock.tick(60)

solver.join()