Advertisement
alkkofficial

Untitled

Apr 14th, 2023 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. def negamax_ab(board, alpha, beta, colour, depth=2):
  2. if depth == 0 or board.is_game_over(): # check if the depth is 0 or "terminal node"
  3. if colour == 1:
  4. move_turn = 0 # my eval accepts 0 for white and black for 1 :/
  5. else:
  6. move_turn = 1
  7. matrix_game = np.array([board_data(board)]) # game after one hot encoding
  8. matrix_game = np.concatenate(
  9. (matrix_game, np.array([[move_turn]])), axis=1
  10. ) # have to append the move turn - the AI needs to know this
  11. matrix_game = torch.tensor(matrix_game, dtype=torch.float32)
  12. score = model(matrix_game) # EVALUTATION - high score for winning (if white/black wins, high score, vice versa)
  13. score = float(score)
  14. return score * colour
  15.  
  16. child_nodes = list(board.legal_moves)
  17. # child_nodes = order_moves(child_nodes) # make an ordermove function
  18. value = -np.inf
  19. for child in child_nodes:
  20. board.push(child) # Push the current child move on the board
  21. value = max(value, -negamax_ab(board, -beta, -alpha, -colour, depth - 1))
  22. board.pop() # Pop the current child move from the board
  23.  
  24. alpha = max(alpha, value)
  25. if alpha >= beta:
  26. break
  27.  
  28. return value
  29.  
  30.  
  31. # set up chess game
  32.  
  33. NUMBER_OF_GAMES = 10
  34.  
  35.  
  36. def play_game(NUMBER_OF_GAMES):
  37. for _ in range(NUMBER_OF_GAMES):
  38. board = chess.Board()
  39. legal_moves = board.legal_moves
  40. colour = 1 # white starts first
  41. for __ in range(1): # random opening, 1 move
  42. legal_moves = list(board.legal_moves)
  43. chosen_move = np.random.randint(0, len(legal_moves))
  44. board.push(legal_moves[chosen_move])
  45. with open("ai_gamesNN.txt", "a+") as f:
  46. f.write(str(legal_moves[chosen_move]))
  47. f.write(" ")
  48. colour = colour * - 1
  49. print(str(legal_moves[chosen_move]))
  50. while not board.is_game_over():
  51. legal_moves = list(board.legal_moves)
  52. m_dict = {}
  53. for move in legal_moves:
  54. test_board = board.copy()
  55. test_board.push(move)
  56. move_score = negamax_ab(test_board, -np.inf, np.inf, -colour, 2)
  57. m_dict[str(move)] = move_score
  58. m_dict = {
  59. k: v
  60. for k, v in sorted(
  61. m_dict.items(), key=lambda item: item[1], reverse=True
  62. ) # reverse=False to find the best move with highest score
  63. }
  64. best_move = list(m_dict.keys())[0] # best move, first key
  65. print(m_dict)
  66. print(best_move)
  67. with open("ai_gamesNN.txt", "a+") as f:
  68. f.write(best_move)
  69. f.write(" ")
  70. best_move = Move.from_uci(best_move)
  71. board.push(best_move)
  72. colour = colour * -1
  73. with open("ai_gamesNN.txt", "a+") as f:
  74. f.write(board.result())
  75. f.write("\n")
  76.  
  77.  
  78. play_game(NUMBER_OF_GAMES)
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement