The project outline:
2048 is a simple game where you combine tiles by sliding them up, down, left, or right with the arrow keys. You can actually get a fairly high score by repeatedly sliding in an up, right, down, and left pattern over and over again. Write a program that will open the game at https://gabrielecirulli.github.io/2048/ and keep sending up, right, down, and left keystrokes to automatically play the game.
My solution:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
def main():
service = Service(r"C:\Program Files\WebDrivers\geckdriver\geckodriver.exe")
options = Options()
options.binary_location = r"C:\Program Files\Mozilla Firefox\firefox.exe"
driver = webdriver.Firefox(service=service, options=options)
driver.get("https://gabrielecirulli.github.io/2048/")
try:
cookie_elem = driver.find_element(By. XPATH, '//*[@id="ez-accept-all"]')
html_elem = driver.find_element(By.TAG_NAME, "html")
except:
print("Was not able to find an element with that name.")
cookie_elem.click()
time.sleep(1)
while True:
html_elem.send_keys(Keys.UP)
time.sleep(0.1)
html_elem.send_keys(Keys.RIGHT)
time.sleep(0.1)
html_elem.send_keys(Keys.DOWN)
time.sleep(0.1)
html_elem.send_keys(Keys.LEFT)
time.sleep(0.1)
if __name__ == '__main__':
main()