Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 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?")
  39. userinput = input("y or n")
  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. lastposition = "lastposition"
  61. lastsuggestionmove = "lastsugmove"
  62. in_game = False
  63. first_move_made = False
  64. move_counter = 0
  65.  
  66. while True:
  67. #checking the current game state
  68. if driver1.current_url == "https://lichess.org/" and searching_for_game == False and in_game == False:
  69. print("should we look for a game?")
  70. #input()
  71.  
  72. zwei_min_game = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[2]/div[1]')
  73. #zwei_min_game = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[2]/div[2]/div[1]')
  74. zwei_min_game.click()
  75. print("joined the game queue")
  76. searching_for_game = True
  77.  
  78. if driver1.find_elements_by_class_name('result_wrap'):
  79. print("game is over")
  80. searching_for_game = False
  81. in_game = False
  82. first_move_made = False
  83. #input("confirm to move on")
  84.  
  85. driver1.get("https://lichess.org/")
  86. time.sleep(1)
  87. if driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]'):
  88. quick_pair_btn = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]')
  89. quick_pair_btn.click()
  90.  
  91. if driver1.current_url != "https://lichess.org/":
  92. in_game == True
  93.  
  94. if first_move_made == False:
  95. element = WebDriverWait(driver1, 25).until(find_first)
  96. try:
  97. initial_move = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
  98. first_move_clock = initial_move.get_attribute("class")
  99. except:
  100. driver1.refresh()
  101. driver1.implicitly_wait(3)
  102. initial_move = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
  103. first_move_clock = initial_move.get_attribute("class")
  104. print("ERROR found the myclass on second try")
  105.  
  106. if 'clock_white' in first_move_clock:
  107.  
  108. bord_letters = {'a': 250, 'b': 314, 'c': 378, 'd': 438, 'e': 505, 'f': 570, 'g': 635, 'h': 697}
  109. bord_numbers = {'1': 662, '2': 599, '3': 538, '4': 470,'5': 407, '6': 343,'7': 281, '8': 215}
  110.  
  111. print("i am white and the first move has not been made")
  112. time.sleep(1)
  113. pyautogui.click(x=bord_letters['d'], y=bord_numbers['2'])
  114. pyautogui.click(x=bord_letters['d'], y=bord_numbers['4'])
  115. first_move_made = True
  116. time.sleep(2)
  117.  
  118. else:
  119. print("i am BLACK and the first move has not been made")
  120.  
  121. bord_letters = {'h': 250, 'g': 314, 'f': 378, 'e': 438, 'd': 505, 'c': 570, 'b': 635, 'a': 697}
  122. bord_numbers = {'8': 662, '7': 599, '6': 538, '5': 470,'4': 407, '3': 343,'2': 281, '1': 215}
  123.  
  124. pyautogui.click(x=bord_letters['e'], y=bord_numbers['7'])
  125. pyautogui.click(x=bord_letters['e'], y=bord_numbers['6'])
  126. first_move_made = True
  127. time.sleep(2)
  128.  
  129.  
  130. element = WebDriverWait(driver1, 25).until(find)
  131. try:
  132. my_classes = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]').get_attribute("class")
  133.  
  134. except:
  135. driver1.refresh()
  136. driver1.implicitly_wait(3)
  137. my_classes = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]').get_attribute("class")
  138. print("ERROR found the myclass on second try")
  139.  
  140.  
  141. if 'running' in my_classes:
  142.  
  143. movelist = driver1.find_element_by_class_name('moves').text.replace('\n', ' ').split(" ")
  144. for x in range(0,len(movelist),3):
  145. movelist[x] = movelist[x] + "."
  146.  
  147. moves = ' '.join(movelist)
  148. moves = moves.replace('х', "x")
  149. pgn = StringIO(moves)
  150. game = chess.pgn.read_game(pgn)
  151. position = game.board()
  152.  
  153. for move in game.main_line():
  154. position.push(move)
  155.  
  156. board = chess.Board(position.fen())
  157. engine.position(board)
  158.  
  159. if move_counter < 5:
  160. thinktime = random.randint(50, 100)
  161. print("its the opening move set movetime to very low")
  162.  
  163. elif move_counter > 5 and not move_counter > 40:
  164. thinktime = random.randint(200, 340)
  165.  
  166. elif move_counter > 40:
  167. thinktime = random.randint(100, 240)
  168.  
  169. else:
  170. thinktime = random.randint(200, 500)
  171.  
  172.  
  173. best_move = engine.go(movetime=thinktime)
  174. best_move = best_move.bestmove
  175. best_move = str(best_move)
  176.  
  177. board = Board()
  178. board.make_move(best_move,bord_letters,bord_numbers)
  179. move_counter += 1
  180.  
  181. """
  182. if we want to use the move input box
  183.  
  184. inputbox = driver1.find_element_by_class_name('ready')
  185. inputbox.clear()
  186. inputbox.send_keys(realsuggestionmove)
  187. lastsuggestionmove = realsuggestionmove
  188.  
  189. """
  190.  
  191.  
  192.  
  193. second file
  194.  
  195.  
  196. import pyautogui
  197. import time
  198.  
  199. class Board():
  200.  
  201. """chess game board class"""
  202.  
  203. def make_move(self, best_move, bord_letters, bord_numbers):
  204. #splitting the suggestion move into an array
  205. sug = [c for c in best_move]
  206.  
  207. #first letter
  208. if "a" in sug[0]:
  209. pyautogui.moveTo(bord_letters['a'], None)
  210.  
  211. elif "b" in sug[0]:
  212. pyautogui.moveTo(bord_letters['b'], None)
  213.  
  214. elif "c" in sug[0]:
  215. pyautogui.moveTo(bord_letters['c'], None)
  216.  
  217. elif "d" in sug[0]:
  218. pyautogui.moveTo(bord_letters['d'], None)
  219.  
  220. elif "e" in sug[0]:
  221. pyautogui.moveTo(bord_letters['e'], None)
  222.  
  223. elif "f" in sug[0]:
  224. pyautogui.moveTo(bord_letters['f'], None)
  225.  
  226. elif "g" in sug[0]:
  227. pyautogui.moveTo(bord_letters['g'], None)
  228.  
  229. elif "h" in sug[0]:
  230. pyautogui.moveTo(bord_letters['h'], None)
  231.  
  232.  
  233. #first Number
  234.  
  235. if "1" in sug[1]:
  236. pyautogui.moveTo(None, bord_numbers['1'])
  237.  
  238. elif "2" in sug[1]:
  239. pyautogui.moveTo(None, bord_numbers['2'])
  240.  
  241. elif "3" in sug[1]:
  242. pyautogui.moveTo(None, bord_numbers['3'])
  243.  
  244. elif "4" in sug[1]:
  245. pyautogui.moveTo(None, bord_numbers['4'])
  246.  
  247. elif "5" in sug[1]:
  248. pyautogui.moveTo(None, bord_numbers['5'])
  249.  
  250. elif "6" in sug[1]:
  251. pyautogui.moveTo(None, bord_numbers['6'])
  252.  
  253. elif "7" in sug[1]:
  254. pyautogui.moveTo(None, bord_numbers['7'])
  255.  
  256. elif "8" in sug[1]:
  257. pyautogui.moveTo(None, bord_numbers['8'])
  258. pyautogui.click()
  259.  
  260. #second letter
  261. if "a" in sug[2]:
  262. pyautogui.moveTo(bord_letters['a'], None)
  263.  
  264. elif "b" in sug[2]:
  265. pyautogui.moveTo(bord_letters['b'], None)
  266.  
  267. elif "c" in sug[2]:
  268. pyautogui.moveTo(bord_letters['c'], None)
  269.  
  270. elif "d" in sug[2]:
  271. pyautogui.moveTo(bord_letters['d'], None)
  272.  
  273. elif "e" in sug[2]:
  274. pyautogui.moveTo(bord_letters['e'], None)
  275.  
  276. elif "f" in sug[2]:
  277. pyautogui.moveTo(bord_letters['f'], None)
  278.  
  279. elif "g" in sug[2]:
  280. pyautogui.moveTo(bord_letters['g'], None)
  281.  
  282. elif "h" in sug[2]:
  283. pyautogui.moveTo(bord_letters['h'], None)
  284.  
  285.  
  286. #second number
  287.  
  288. if "1" in sug[3]:
  289. pyautogui.moveTo(None, bord_numbers['1'])
  290.  
  291. elif "2" in sug[3]:
  292. pyautogui.moveTo(None, bord_numbers['2'])
  293.  
  294. elif "3" in sug[3]:
  295. pyautogui.moveTo(None, bord_numbers['3'])
  296.  
  297. elif "4" in sug[3]:
  298. pyautogui.moveTo(None, bord_numbers['4'])
  299.  
  300. elif "5" in sug[3]:
  301. pyautogui.moveTo(None, bord_numbers['5'])
  302.  
  303. elif "6" in sug[3]:
  304. pyautogui.moveTo(None, bord_numbers['6'])
  305.  
  306. elif "7" in sug[3]:
  307. pyautogui.moveTo(None, bord_numbers['7'])
  308.  
  309. elif "8" in sug[3]:
  310. pyautogui.moveTo(None, bord_numbers['8'])
  311. pyautogui.click()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement