Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- import time
- import pyautogui
- import chess.pgn
- from io import StringIO
- import chess.uci
- import random
- from selenium.webdriver.support.ui import WebDriverWait
- from board import Board
- def find(driver1):
- element = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]')
- if element:
- return element
- else:
- return False
- def find_first(driver1):
- element = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
- if element:
- return element
- else:
- return False
- chrome_path = r"C:\Users\Anon\Desktop\chessbot\chromedriver.exe"
- driver1 = webdriver.Chrome(chrome_path)
- driver1.get("https://lichess.org/")
- driver1.set_window_size(1024, 750)
- driver1.set_window_position(0, 0)
- engine = chess.uci.popen_engine("C:/Users/Anon/Desktop/chessbot/s8.exe")
- engine.uci()
- print("do you want to use an account? y or n")
- userinput = input()
- if userinput == "y":
- sign_in_btn = driver1.find_element_by_xpath('//*[@id="top"]/a[1]')
- sign_in_btn.click()
- username_input_box = driver1.find_element_by_xpath('//*[@id="username"]')
- username_input_box.send_keys("StupidAI")
- pw_input_box = driver1.find_element_by_xpath('//*[@id="password"]')
- pw_input_box.send_keys("lWuzJaF1p3HekFUxR07R")
- login_btn = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/form/ul/li[3]/button')
- login_btn.click()
- print("press enter to start")
- input()
- searching_for_game = False
- in_game = False
- first_move_made = False
- move_counter = 0
- while True:
- #checking the current game state
- if driver1.current_url == "https://lichess.org/" and searching_for_game == False and in_game == False:
- zwei_min_game = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[2]/div[1]')
- #zwei_min_game = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[2]/div[2]/div[1]')
- zwei_min_game.click()
- searching_for_game = True
- if driver1.find_elements_by_class_name('result_wrap'):
- searching_for_game = False
- in_game = False
- first_move_made = False
- move_counter = 0
- #input("confirm to move on")
- driver1.get("https://lichess.org/")
- time.sleep(1)
- if driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]'):
- quick_pair_btn = driver1.find_element_by_xpath('//*[@id="hooks_wrap"]/div[1]/a[1]')
- quick_pair_btn.click()
- if driver1.current_url != "https://lichess.org/":
- last_time = time.time()
- in_game == True
- if first_move_made == False:
- #element = WebDriverWait(driver1, 25).until(find_first)
- try:
- initial_move = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
- first_move_clock = initial_move.get_attribute("class")
- except:
- driver1.refresh()
- time.sleep(1)
- try:
- driver1.switch_to_alert().accept()
- except:
- print("there was no popup")
- initial_move = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[4]')
- first_move_clock = initial_move.get_attribute("class")
- print("ERROR found the myclass on second try")
- if 'clock_white' in first_move_clock:
- bord_letters = {'a': 250, 'b': 314, 'c': 378, 'd': 438, 'e': 505, 'f': 570, 'g': 635, 'h': 697}
- bord_numbers = {'1': 662, '2': 599, '3': 538, '4': 470,'5': 407, '6': 343,'7': 281, '8': 215}
- print("i am white and the first move has not been made")
- time.sleep(1)
- pyautogui.click(x=bord_letters['d'], y=bord_numbers['2'])
- pyautogui.click(x=bord_letters['d'], y=bord_numbers['4'])
- first_move_made = True
- else:
- print("i am BLACK and the first move has not been made")
- bord_letters = {'h': 250, 'g': 314, 'f': 378, 'e': 438, 'd': 505, 'c': 570, 'b': 635, 'a': 697}
- bord_numbers = {'8': 662, '7': 599, '6': 538, '5': 470,'4': 407, '3': 343,'2': 281, '1': 215}
- pyautogui.click(x=bord_letters['e'], y=bord_numbers['7'])
- pyautogui.click(x=bord_letters['e'], y=bord_numbers['6'])
- first_move_made = True
- #element = WebDriverWait(driver1, 25).until(find)
- try:
- my_classes = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]').get_attribute("class")
- except:
- driver1.refresh()
- time.sleep(1)
- try:
- driver1.switch_to_alert().accept()
- except:
- print("there was no popup")
- my_classes = driver1.find_element_by_xpath('//*[@id="lichess"]/div/div[1]/div[2]/div[2]/div[3]').get_attribute("class")
- print("ERROR found the myclass on second try")
- if 'running' in my_classes:
- movelist = driver1.find_element_by_class_name('moves').text.replace('\n', ' ').split(" ")
- for x in range(0,len(movelist),3):
- movelist[x] = movelist[x] + "."
- moves = ' '.join(movelist)
- moves = moves.replace('х', "x")
- pgn = StringIO(moves)
- game = chess.pgn.read_game(pgn)
- position = game.board()
- for move in game.main_line():
- position.push(move)
- board = chess.Board(position.fen())
- engine.position(board)
- if move_counter < 5:
- thinktime = random.randint(90, 190)
- print("its the opening set movetime to very low")
- elif move_counter > 5 and not move_counter > 40:
- thinktime = random.randint(290, 390)
- print("middle game")
- elif move_counter > 40:
- thinktime = random.randint(190, 340)
- print("end game")
- engine_move_time = time.time()
- best_move = engine.go(movetime=thinktime)
- engine_move_time = time.time()
- best_move = best_move.bestmove
- best_move = str(best_move)
- board = Board()
- board.make_move(best_move,bord_letters,bord_numbers)
- move_counter += 1
- print('loop took {} seconds'.format(time.time()-last_time))
- """
- if we want to use the move input box
- inputbox = driver1.find_element_by_class_name('ready')
- inputbox.clear()
- inputbox.send_keys(realsuggestionmove)
- lastsuggestionmove = realsuggestionmove
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement