Skip to main content
deleted 230 characters in body
Source Link
Javana
  • 451
  • 2
  • 6

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:

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()

It works, but I couldn't figure out how to make the program end on the game over screen, though the outline doesn't specify to do this and the chapter didn't go in-depth into selenium so I'm not sure what the author had in mind.

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()

It works, but I couldn't figure out how to make the program end on the game over screen, though the outline doesn't specify to do this and the chapter didn't go in-depth into selenium so I'm not sure what the author had in mind.

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()
Post Undeleted by Javana
Post Deleted by Javana
Source Link
Javana
  • 451
  • 2
  • 6

Automate the Boring Stuff Chapter 12 - 2048

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()

It works, but I couldn't figure out how to make the program end on the game over screen, though the outline doesn't specify to do this and the chapter didn't go in-depth into selenium so I'm not sure what the author had in mind.