示例#1
0
def get_move():
    fen_str = request.args.get('fen')
    print(fen_str)
    if fen_str is None:
        return "Provide fen"
    ai = Ai()
    value, move = ai.get_move(fen_str=fen_str)
    return move
示例#2
0
 def chess_game_loop(self, ai=None, color=None):
     initial_square = Square()
     drop_square = Square()
     # False for ai with white pieces
     player_turn = color
     white_maximizing = not player_turn
     value = 0
     square_under_mouse = Square(None, None, None, False)
     ai_player = Ai()
     while True:
         if ai and (not player_turn and not
                    self.board.board_text.is_game_over()):
             value, mv = ai_player.get_move(
                 white_maximizing=white_maximizing,
                 board=self.board.board_text)
             print("Val:", value)
             print("Move:", mv)
             self.ai_played_square = mv[2:4]
             self.board.board_text.push_uci(mv)
             try:
                 self.board.current_fen = self.board.board_text.fen()
             except Exception as e:
                 print(e)
                 print("game over")
             self.redo_buffer = []
             self.move_history.append(mv)
             player_turn = not player_turn
             self.color_playing = not self.color_playing
             if self.board.board_text.is_game_over():
                 self.board.board_surf.blit(
                     self.check_mate_text, self.check_mate_text_rect
                 )
             self.board.rect_board = self.board.fen_to_board()
         else:
             square_under_mouse = self.board.get_square_under_mouse()
             events = pygame.event.get()
             for e in events:
                 if e.type == pygame.QUIT:
                     self.game_init(self.screen_width, self.screen_height)
                     self.game_intro()
                 if e.type == pygame.MOUSEBUTTONDOWN:
                     if square_under_mouse.can_use:
                         initial_square.set_square(
                             square_under_mouse.fen_char,
                             square_under_mouse.x,
                             square_under_mouse.y,
                         )
                 if e.type == pygame.MOUSEBUTTONUP:
                     if drop_square.can_use:
                         if initial_square != drop_square:
                             uci, promotion = self.board.board_to_uci(
                                 initial_square, drop_square
                             )
                             legal = (
                                 chess.Move.from_uci(uci)
                                 in self.board.board_text.legal_moves
                             )
                             if legal:
                                 self.last_square = drop_square
                                 if promotion:
                                     # add extra uci notation if a player is
                                     # promoting and draw the promotion
                                     # choice menu
                                     uci = uci[:-1] + \
                                         self.board.promotion_loop(
                                         self.game_display,
                                         self.color_playing)
                                 promotion = False
                                 self.board.board_text.push_uci(uci)
                                 try:
                                     self.board.current_fen = \
                                         self.board.board_text.fen()
                                 except Exception as e:
                                     print(e)
                                     print("game over")
                                 self.last_square = uci[2:4]
                                 self.redo_buffer = []
                                 self.move_history.append(uci)
                                 self.board.rect_board = \
                                     self.board.fen_to_board()
                                 player_turn = not player_turn
                                 self.color_playing = not self.color_playing
                             if self.board.board_text.is_game_over():
                                 self.board.board_surf.blit(
                                     self.check_mate_text,
                                     self.check_mate_text_rect)
                     initial_square.can_use = False
                     drop_square.can_use = False
         self.move_displayer(self.color_playing)
         self.button(
             "Undo",
             10,
             740,
             self.screen_width // 8 + 30,
             720//8,
             self.grey,
             self.bright_green,
             action=self.undo_move,
             board=self.board,
             ai=ai
         )
         self.button(
             "Redo",
             180,
             740,
             self.screen_width // 8 + 30,
             720//8,
             self.grey,
             self.bright_green,
             action=self.redo_move,
             board=self.board,
             ai=ai
         )
         self.button(
             "Flip",
             350,
             740,
             self.screen_width // 8 + 30,
             720//8,
             self.grey,
             self.bright_green,
             action=self.flip_board,
             board=self.board
         )
         self.game_display.blit(self.board.board_surf, self.board.board_pos)
         if self.last_square and not player_turn:
             self.board.draw_last_piece(self.game_display, self.last_square)
         if ai:
             if self.ai_played_square and player_turn:
                 self.board.draw_last_piece(
                     self.game_display, self.ai_played_square)
         elif self.last_square and player_turn:
             self.board.draw_last_piece(self.game_display, self.last_square)
         self.board.draw_pieces(self.game_display)
         self.board.draw_selector(self.game_display, square_under_mouse)
         drop_square = self.board.draw_drag(
             self.game_display, initial_square)
         # draw a req square around the king if he is in check
         if self.board.board_text.is_check():
             if not self.color_playing:
                 self.board.draw_king_check(self.game_display, "k")
             else:
                 self.board.draw_king_check(self.game_display, "K")
         pygame.display.flip()
         self.clock.tick(60)