Created
May 20, 2014 17:54
-
-
Save pierre-haessig/61d17633940f8edddee1 to your computer and use it in GitHub Desktop.
A simple code to interact in Python with the "real" 2048 game in web browser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Pierre Haessig — May 2014 | |
"""A simple code to interact in Python with the "real" 2048 game in web browser. | |
""" | |
from __future__ import print_function | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
def connect_2048(): | |
url = "http://gabrielecirulli.github.io/2048/" | |
print('Opening Firefox...') | |
driver = webdriver.Firefox() | |
print('Opening URL "{}"'.format()) | |
driver.get(url) | |
print(driver.title) | |
return driver | |
def find_game_container(driver): | |
tile_container = driver.find_element_by_class_name('tile-container') | |
return tile_container | |
def parse_classname(s): | |
''' | |
ex: s = 'tile tile-4 tile-position-1-2 tile-new' | |
ou 'tile tile-4 tile-position-1-4' | |
'tile tile-4 tile-position-3-4 tile-merged' | |
Returns: ((row, col), value, flag) | |
''' | |
flag = None | |
fields = s.replace('-', ' ').split(' ') | |
# ex: [u'tile', u'tile', u'4', u'tile', u'position', u'1', u'2', u'tile', u'new'] | |
value = int(fields[2]) | |
row = int(fields[6]) | |
col = int(fields[5]) | |
if len(fields) == 9: | |
flag = fields[8] | |
return ((row, col), value, flag) | |
def get_grid(tile_container): | |
tiles = tile_container.find_elements_by_class_name('tile') | |
#print(tiles) | |
# information can be retrieved from tile class names: | |
tile_classes = [t.get_attribute('class') for t in tiles] | |
#print('\n'.join(tile_classes)) | |
# a list of tiles (row, col), value, flag) | |
tiles_data = [parse_classname(s) for s in tile_classes] | |
# Build the grid | |
grid = [ | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0]] | |
for ((row, col), value, flag) in tiles_data: | |
if flag == 'merged': | |
continue # discard, use the "underlying tiles" addition instead | |
grid[row-1][col-1] += value | |
return grid | |
def print_grid(grid): | |
for r in grid: | |
r = ['{:4d}'.format(c) for c in r] | |
print(' '.join(r)) | |
if __name__ == '__main__': | |
# A test session: | |
try: | |
driver = connect_2048() | |
tile_container = find_game_container(driver) | |
grid = get_grid(tile_container) | |
print('Starting grid:') | |
print_grid(grid) | |
print('') | |
# Do some actions: | |
tile_container.send_keys(Keys.DOWN) | |
tile_container.send_keys(Keys.RIGHT) | |
grid = get_grid(tile_container) | |
print('Modified grid:') | |
print_grid(grid) | |
finally: | |
raw_input('Press enter to close the browser...') | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment