So there is this game 2048. You can play it here
I wrote a python script which "plays" the game on the website by clicking the arrow keys.
I would like to know what can be improved. Is it a good idea to check the Try again button with exceptions? Is there a better way?
Currently it takes alot of tries to get a high score for the bot. Can the logic be improved?
play_2048.py
"""
Opens the webpage of the 2048 game and plays it repeadly.
Needs chromedriver in the directory of the script. Download it here:
https://sites.google.com/a/chromium.org/chromedriver/downloads
"""
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def play_2048():
"""
Main lopp of script. First opens the browser to the game page.
Then pushes the arrow keys to play the game.
If a game over occurs the try again button on the page is klicked
to repeat the game
"""
browser = webdriver.Chrome()
browser.get('https://play2048.co/')
html = browser.find_element_by_tag_name('html')
while True:
html.send_keys(Keys.RIGHT)
html.send_keys(Keys.UP)
html.send_keys(Keys.LEFT)
html.send_keys(Keys.DOWN)
try:
retry = browser.find_element_by_link_text('Try again')
except:
continue
else:
retry.click()
if __name__ == '__main__':
play_2048()