Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. from selenium import webdriver
  2. import time
  3. import pyautogui
  4.  
  5. chrome_path = r"C:\Users\Anon\Desktop\chessbot\chromedriver.exe"
  6.  
  7. driver2 = webdriver.Chrome(chrome_path)
  8. driver3 = webdriver.Chrome(chrome_path)
  9. driver1 = webdriver.Chrome(chrome_path)
  10.  
  11.  
  12. driver2.get("http://analysis.cpuchess.com/")
  13. driver3.get("https://lichess.org/analysis")
  14. driver1.get("https://lichess.org/")
  15.  
  16. driver1.set_window_size(1024, 750)
  17. driver1.set_window_position(0, 0)
  18.  
  19. driver2.set_window_size(990, 770)
  20. driver2.set_window_position(1024, 0)
  21.  
  22. driver3.set_window_size(1200, 970)
  23. driver3.set_window_position(2001, 0)
  24.  
  25. print("do you want to use an account?")
  26. userinput = input("y or n")
  27.  
  28. if userinput == "y":
  29. sign_in_btn = driver1.find_element_by_xpath('//*[@id="top"]/a[1]')
  30. sign_in_btn.click()
  31.  
  32. username_input_box = driver1.find_element_by_xpath('//*[@id="username"]')
  33. username_input_box.send_keys("StupidAI")
  34.  
  35. pw_input_box = driver1.find_element_by_xpath('//*[@id="password"]')
  36. pw_input_box.send_keys("lWuzJaF1p3HekFUxR07R")
  37.  
  38. login_btn = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/form/ul/li[3]/button')
  39. login_btn.click()
  40.  
  41. print("activating the engine")
  42.  
  43. enable_engine_btn = driver3.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div/div[2]/div[1]/div/label')
  44. enable_engine_btn.click()
  45.  
  46. print("reposition the windows as you like and press enter")
  47. input()
  48.  
  49.  
  50. searching_for_game = False
  51. lastposition = "lastposition"
  52. lastsuggestionmove = "lastsugmove"
  53. in_game = False
  54. first_move_made = False
  55.  
  56. while True:
  57. #checking the current game state
  58. if driver1.current_url == "https://lichess.org/" and searching_for_game == False and in_game == False:
  59. print("we are not playing we will look for a game")
  60. drei_min_game = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[2]/div[2]/div[1]')
  61. drei_min_game.click()
  62. print("joined the game queue")
  63. searching_for_game = True
  64.  
  65. if driver1.find_elements_by_class_name('result_wrap'):
  66. print("game is over")
  67. searching_for_game = False
  68. in_game = False
  69. first_move_made = False
  70. driver1.get("https://lichess.org/")
  71. time.sleep(2)
  72. if driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]'):
  73. quick_pair_btn = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]')
  74. quick_pair_btn.click()
  75.  
  76. if driver1.current_url != "https://lichess.org/":
  77. in_game == True
  78. print("we are in a game")
  79.  
  80. my_clock = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]')
  81. my_classes = my_clock.get_attribute("class")
  82.  
  83. if first_move_made == False:
  84.  
  85. initial_move = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
  86. whos_move = initial_move.get_attribute("class")
  87. if 'clock_white' in whos_move:
  88.  
  89. bord_letters = {'a': 250, 'b': 314, 'c': 378, 'd': 438, 'e': 505, 'f': 570, 'g': 635, 'h': 697}
  90. bord_numbers = {'1': 662, '2': 599, '3': 538, '4': 470,'5': 407, '6': 343,'7': 281, '8': 215}
  91.  
  92. print("i am white and the first move has not been made")
  93. pyautogui.click(x=bord_letters['d'], y=bord_numbers['2'])
  94. pyautogui.click(x=bord_letters['d'], y=bord_numbers['4'])
  95. first_move_made = True
  96. else:
  97. print("i am BLACK and the first move has not been made")
  98.  
  99. bord_letters = {'h': 250, 'g': 314, 'f': 378, 'e': 438, 'd': 505, 'c': 570, 'b': 635, 'a': 697}
  100. bord_numbers = {'8': 662, '7': 599, '6': 538, '5': 470,'4': 407, '3': 343,'2': 281, '1': 215}
  101.  
  102. pyautogui.moveTo(bord_letters['e'], None)
  103. pyautogui.moveTo(None, bord_numbers['7'])
  104. pyautogui.click()
  105.  
  106. pyautogui.moveTo(bord_letters['e'], None)
  107. pyautogui.moveTo(None, bord_numbers['6'])
  108. pyautogui.click()
  109. first_move_made = True
  110.  
  111. if 'running' in my_classes:
  112. print("its my turn")
  113. print("finding all moves made")
  114. moves = driver1.find_element_by_class_name('moves')
  115. moves = moves.text
  116. moves = moves.replace('\n', ' ')
  117. movelist = moves.split(" ")
  118. for x in range(0,len(movelist),3):
  119. movelist[x] = movelist[x] + "."
  120.  
  121. moves = ' '.join(movelist)
  122. #inputing our moves into the engine
  123. pgn = driver3.find_element_by_xpath('//*[@id="lichess"]/div/div[2]/div[1]/div/div/textarea')
  124. pgn.click()
  125. pgn.send_keys(moves)
  126.  
  127. import_pgn = driver3.find_element_by_xpath('//*[@id="lichess"]/div/div[2]/div[1]/div/div/div/button')
  128. import_pgn.click()
  129.  
  130. time.sleep(2)
  131. suggestedmove = driver3.find_element_by_class_name("pv")
  132. rawsuggestionmove = suggestedmove.get_attribute('outerHTML')
  133. realsuggestionmove = rawsuggestionmove[26:30]
  134. #splitting the suggestion move into an array
  135. sug = [c for c in realsuggestionmove]
  136.  
  137. # we probably dont want to start all over again but this is an easy fix also this does not solve the situation
  138. #where there the suggestion is right and the same move this would cause a crash
  139. #best thing probably is getting the fen string from lichess and checking it against ours if it matches we accept the same move else we dont
  140. if lastsuggestionmove == realsuggestionmove:
  141. print("the last rawsuggestionmove is the same as the current which is not good its very unlikely that the same move gets suggested twice")
  142. continue
  143.  
  144. """
  145. if we want to use the move input box
  146.  
  147. inputbox = driver1.find_element_by_class_name('ready')
  148. inputbox.clear()
  149. inputbox.send_keys(realsuggestionmove)
  150. lastsuggestionmove = realsuggestionmove
  151.  
  152. """
  153.  
  154. #first letter
  155. if "a" in sug[0]:
  156. pyautogui.moveTo(bord_letters['a'], None)
  157.  
  158. elif "b" in sug[0]:
  159. pyautogui.moveTo(bord_letters['b'], None)
  160.  
  161. elif "c" in sug[0]:
  162. pyautogui.moveTo(bord_letters['c'], None)
  163.  
  164. elif "d" in sug[0]:
  165. pyautogui.moveTo(bord_letters['d'], None)
  166.  
  167. elif "e" in sug[0]:
  168. pyautogui.moveTo(bord_letters['e'], None)
  169.  
  170. elif "f" in sug[0]:
  171. pyautogui.moveTo(bord_letters['f'], None)
  172.  
  173. elif "g" in sug[0]:
  174. pyautogui.moveTo(bord_letters['g'], None)
  175.  
  176. elif "h" in sug[0]:
  177. pyautogui.moveTo(bord_letters['h'], None)
  178.  
  179.  
  180. #first Number
  181.  
  182. if "1" in sug[1]:
  183. pyautogui.moveTo(None, bord_numbers['1'])
  184.  
  185. elif "2" in sug[1]:
  186. pyautogui.moveTo(None, bord_numbers['2'])
  187.  
  188. elif "3" in sug[1]:
  189. pyautogui.moveTo(None, bord_numbers['3'])
  190.  
  191. elif "4" in sug[1]:
  192. pyautogui.moveTo(None, bord_numbers['4'])
  193.  
  194. elif "5" in sug[1]:
  195. pyautogui.moveTo(None, bord_numbers['5'])
  196.  
  197. elif "6" in sug[1]:
  198. pyautogui.moveTo(None, bord_numbers['6'])
  199.  
  200. elif "7" in sug[1]:
  201. pyautogui.moveTo(None, bord_numbers['7'])
  202.  
  203. elif "8" in sug[1]:
  204. pyautogui.moveTo(None, bord_numbers['8'])
  205.  
  206.  
  207. print("we are at the right place and now clicking")
  208. pyautogui.click()
  209.  
  210. #second letter
  211. if "a" in sug[2]:
  212. pyautogui.moveTo(bord_letters['a'], None)
  213.  
  214. elif "b" in sug[2]:
  215. pyautogui.moveTo(bord_letters['b'], None)
  216.  
  217. elif "c" in sug[2]:
  218. pyautogui.moveTo(bord_letters['c'], None)
  219.  
  220. elif "d" in sug[2]:
  221. pyautogui.moveTo(bord_letters['d'], None)
  222.  
  223. elif "e" in sug[2]:
  224. pyautogui.moveTo(bord_letters['e'], None)
  225.  
  226. elif "f" in sug[2]:
  227. pyautogui.moveTo(bord_letters['f'], None)
  228.  
  229. elif "g" in sug[2]:
  230. pyautogui.moveTo(bord_letters['g'], None)
  231.  
  232. elif "h" in sug[2]:
  233. pyautogui.moveTo(bord_letters['h'], None)
  234.  
  235.  
  236. #second number
  237.  
  238. if "1" in sug[3]:
  239. pyautogui.moveTo(None, bord_numbers['1'])
  240.  
  241. elif "2" in sug[3]:
  242. pyautogui.moveTo(None, bord_numbers['2'])
  243.  
  244. elif "3" in sug[3]:
  245. pyautogui.moveTo(None, bord_numbers['3'])
  246.  
  247. elif "4" in sug[3]:
  248. pyautogui.moveTo(None, bord_numbers['4'])
  249.  
  250. elif "5" in sug[3]:
  251. pyautogui.moveTo(None, bord_numbers['5'])
  252.  
  253. elif "6" in sug[3]:
  254. pyautogui.moveTo(None, bord_numbers['6'])
  255.  
  256. elif "7" in sug[3]:
  257. pyautogui.moveTo(None, bord_numbers['7'])
  258.  
  259. elif "8" in sug[3]:
  260. pyautogui.moveTo(None, bord_numbers['8'])
  261.  
  262. print("we are at the right place and now clicking")
  263. pyautogui.click()
  264.  
  265.  
  266. else:
  267. print("not my turn")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement