Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- #_2048.py - A program that plays the game 2048 (https://play2048.co/)
- # automatically.
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as EC
- from random import randint
- browser = webdriver.Firefox() # Open Firefox
- browser.get('https://play2048.co/') # Go to https://play2048.co/
- wait = WebDriverWait(browser, 10)
- wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".game-container)))
- def random_click(): # Function for random click
- rand_int = randint(1, 4)
- if rand_int == 1:
- Keys.UP
- elif rand_int == 2:
- Keys.RIGHT
- elif rand_int == 3:
- Keys.DOWN
- else:
- Keys.LEFT
- random_click()
- while '2048' not in board.values():
- tileContainer = browser.find_element(By.CLASS_NAME, 'tile-container') # finds the tile container
- tiles = tileContainer.find_elements(By.TAG_NAME, "div[class*='tile-position']") # finds every tile
- board = {'1-1': None, '1-2': None, '1-3': None, '1-4': None, '2-1': None,
- '2-2': None, '2-3': None, '2-4': None, '3-1': None, '3-2': None,
- '3-3': None, '3-4': None, '4-1': None, '4-2': None, '4-3': None,
- '4-4': None,
- } # creates an empty dictionary for the board which is filled right afterwards
- for tile in tiles: # fills the board
- board[tile.get_attribute('class')[26:29]] = tile.get_attribute('class')[10]
- # decision structure
Add Comment
Please, Sign In to add comment