Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. from selenium import webdriver
  2. import time
  3. import pyautogui
  4. import chess.pgn
  5. from io import StringIO
  6. import chess.uci
  7. import random
  8. from selenium.webdriver.support.ui import WebDriverWait
  9. from board import Board
  10.  
  11.  
  12. def find(driver1):
  13. element = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]')
  14. if element:
  15. return element
  16. else:
  17. return False
  18.  
  19. def find_first(driver1):
  20. element = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
  21. if element:
  22. return element
  23. else:
  24. return False
  25.  
  26. chrome_path = r"C:\Users\Anon\Desktop\chessbot\chromedriver.exe"
  27.  
  28. driver1 = webdriver.Chrome(chrome_path)
  29.  
  30. driver1.get("https://lichess.org/")
  31.  
  32. driver1.set_window_size(1024, 750)
  33. driver1.set_window_position(0, 0)
  34.  
  35. engine = chess.uci.popen_engine("C:/Users/Anon/Desktop/chessbot/s8.exe")
  36. engine.uci()
  37.  
  38. print("do you want to use an account? y or n")
  39. userinput = input()
  40.  
  41. if userinput == "y":
  42. sign_in_btn = driver1.find_element_by_xpath('//*[@id="top"]/a[1]')
  43. sign_in_btn.click()
  44.  
  45. username_input_box = driver1.find_element_by_xpath('//*[@id="username"]')
  46. username_input_box.send_keys("StupidAI")
  47.  
  48. pw_input_box = driver1.find_element_by_xpath('//*[@id="password"]')
  49. pw_input_box.send_keys("lWuzJaF1p3HekFUxR07R")
  50.  
  51. login_btn = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/form/ul/li[3]/button')
  52. login_btn.click()
  53.  
  54.  
  55. print("press enter to start")
  56. input()
  57.  
  58.  
  59. searching_for_game = False
  60. in_game = False
  61. first_move_made = False
  62. move_counter = 0
  63.  
  64. while True:
  65. #checking the current game state
  66. if driver1.current_url == "https://lichess.org/" and searching_for_game == False and in_game == False:
  67. zwei_min_game = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[2]/div[1]')
  68. #zwei_min_game = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[2]/div[2]/div[1]')
  69. zwei_min_game.click()
  70. searching_for_game = True
  71.  
  72. if driver1.find_elements_by_class_name('result_wrap'):
  73. searching_for_game = False
  74. in_game = False
  75. first_move_made = False
  76. move_counter = 0
  77. #input("confirm to move on")
  78.  
  79. driver1.get("https://lichess.org/")
  80. time.sleep(1)
  81. if driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]'):
  82. quick_pair_btn = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]')
  83. quick_pair_btn.click()
  84.  
  85. if driver1.current_url != "https://lichess.org/":
  86.  
  87. last_time = time.time()
  88. in_game == True
  89.  
  90. if first_move_made == False:
  91. #element = WebDriverWait(driver1, 25).until(find_first)
  92. try:
  93. initial_move = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
  94. first_move_clock = initial_move.get_attribute("class")
  95.  
  96. except:
  97. driver1.refresh()
  98. time.sleep(1)
  99.  
  100. try:
  101. driver1.switch_to_alert().accept()
  102. except:
  103. print("there was no popup")
  104.  
  105. initial_move = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
  106. first_move_clock = initial_move.get_attribute("class")
  107. print("ERROR found the myclass on second try")
  108.  
  109. if 'clock_white' in first_move_clock:
  110.  
  111. bord_letters = {'a': 250, 'b': 314, 'c': 378, 'd': 438, 'e': 505, 'f': 570, 'g': 635, 'h': 697}
  112. bord_numbers = {'1': 662, '2': 599, '3': 538, '4': 470,'5': 407, '6': 343,'7': 281, '8': 215}
  113.  
  114. print("i am white and the first move has not been made")
  115. time.sleep(1)
  116. pyautogui.click(x=bord_letters['d'], y=bord_numbers['2'])
  117. pyautogui.click(x=bord_letters['d'], y=bord_numbers['4'])
  118. first_move_made = True
  119.  
  120. else:
  121. print("i am BLACK and the first move has not been made")
  122.  
  123. bord_letters = {'h': 250, 'g': 314, 'f': 378, 'e': 438, 'd': 505, 'c': 570, 'b': 635, 'a': 697}
  124. bord_numbers = {'8': 662, '7': 599, '6': 538, '5': 470,'4': 407, '3': 343,'2': 281, '1': 215}
  125.  
  126. pyautogui.click(x=bord_letters['e'], y=bord_numbers['7'])
  127. pyautogui.click(x=bord_letters['e'], y=bord_numbers['6'])
  128. first_move_made = True
  129.  
  130.  
  131. #element = WebDriverWait(driver1, 25).until(find)
  132. try:
  133. my_classes = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]').get_attribute("class")
  134.  
  135. except:
  136. driver1.refresh()
  137. time.sleep(1)
  138.  
  139. try:
  140. driver1.switch_to_alert().accept()
  141. except:
  142. print("there was no popup")
  143.  
  144. my_classes = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]').get_attribute("class")
  145. print("ERROR found the myclass on second try")
  146.  
  147.  
  148. if 'running' in my_classes:
  149.  
  150. movelist = driver1.find_element_by_class_name('moves').text.replace('\n', ' ').split(" ")
  151. for x in range(0,len(movelist),3):
  152. movelist[x] = movelist[x] + "."
  153.  
  154. moves = ' '.join(movelist)
  155. moves = moves.replace('х', "x")
  156. pgn = StringIO(moves)
  157. game = chess.pgn.read_game(pgn)
  158. position = game.board()
  159.  
  160. for move in game.main_line():
  161. position.push(move)
  162.  
  163. board = chess.Board(position.fen())
  164. engine.position(board)
  165.  
  166. if move_counter < 5:
  167. thinktime = random.randint(90, 190)
  168. print("its the opening set movetime to very low")
  169.  
  170. elif move_counter > 5 and not move_counter > 40:
  171. thinktime = random.randint(290, 390)
  172. print("middle game")
  173.  
  174. elif move_counter > 40:
  175. thinktime = random.randint(190, 340)
  176. print("end game")
  177.  
  178. engine_move_time = time.time()
  179. best_move = engine.go(movetime=thinktime)
  180. engine_move_time = time.time()
  181.  
  182. best_move = best_move.bestmove
  183. best_move = str(best_move)
  184.  
  185. board = Board()
  186. board.make_move(best_move,bord_letters,bord_numbers)
  187. move_counter += 1
  188.  
  189. print('loop took {} seconds'.format(time.time()-last_time))
  190.  
  191. """
  192. if we want to use the move input box
  193.  
  194. inputbox = driver1.find_element_by_class_name('ready')
  195. inputbox.clear()
  196. inputbox.send_keys(realsuggestionmove)
  197. lastsuggestionmove = realsuggestionmove
  198.  
  199. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement