Skip to content

Instantly share code, notes, and snippets.

@stringertheory
Last active June 26, 2023 14:00
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stringertheory/9537244 to your computer and use it in GitHub Desktop.
Save stringertheory/9537244 to your computer and use it in GitHub Desktop.
A quicky python script to simulate random pushes in the Threes game at threesjs.com, as described in http://datascopeanalytics.com/what-we-think/2014/03/13/are-you-really-any-good-at-threes
# standard library
import sys
import random
import time
# need to pip install selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
# wait this long before hitting arrow keys
wait_time = 0.05
# choose from these
possible_keys = [
Keys.ARROW_UP,
Keys.ARROW_DOWN,
Keys.ARROW_LEFT,
Keys.ARROW_RIGHT,
]
# open up a browser to the game on the web
wd = webdriver.Firefox()
wd.get('http://threesjs.com')
# get new game element
new_game = wd.find_element_by_xpath("//div[@id='new-game']")
# keep playing games and recording the score
score = None
while True:
# break and get score if game is over
try:
score_element = wd.find_element_by_xpath("//div[@class='score']")
except NoSuchElementException:
pass
else:
time.sleep(wait_time)
# get score and print it
score = int(score_element.text)
print score
sys.stdout.flush()
# start a new game
new_game.click()
# wait for some time for move to take place
time.sleep(wait_time)
# choose a random direction to push
new_game.send_keys(random.choice(possible_keys))
wd.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment