-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_based.py
More file actions
65 lines (47 loc) · 1.78 KB
/
Copy pathweb_based.py
File metadata and controls
65 lines (47 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import os
import re
import time
#first get the page open and running
def openGame():
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
#give full access to it so we can get containers
user_data_dir = os.path.expanduser("~/chrome_selenium_profile")
chrome_options.add_argument(f"--user-data-dir={user_data_dir}")
#go on website
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://2048game.com/")
return driver
def getContents(driver):
#you want to clear it after every move
grid = [[0 for _ in range(4)] for _ in range(4)]
tile_container = driver.find_element(By.CLASS_NAME, "tile-container")
tiles = tile_container.find_elements(By.CLASS_NAME, "tile")
for tile in tiles:
classes = tile.get_attribute("class") # The full class string
value_match = re.search(r'tile-(\d+)', classes)
if value_match:
tile_value = int(value_match.group(1))
position_match = re.search(r'tile-position-(\d+)-(\d+)', classes)
if position_match:
row = int(position_match.group(1))
col =int(position_match.group(2))
grid[col - 1][row - 1] = tile_value
for x in grid:
print(x)
print("--------------")
return grid
def decision(grid, driver):
body = driver.find_element(By.TAG_NAME, "body")
body.send_keys(Keys.ARROW_DOWN)
time.sleep(5)
if __name__ == "__main__":
instance = openGame()
grid = getContents(instance)
for x in range(3):
decision(grid, instance)
getContents(instance)