Advertisement
Guest User

Untitled

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