Skip to content

Instantly share code, notes, and snippets.

@kotoripiyopiyo

kotoripiyopiyo/play2048.py

Last active Jan 25, 2021
Embed
What would you like to do?
2048を自動でプレイする
#!/usr/bin/env python3
# 2048.py 勝手に2048を遊ぶ
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# 2048のサイトにアクセス
browser = webdriver.Chrome('/usr/local/bin/chromedriver')
browser.get('https://play2048.co/')
# ランダムに10回矢印キーを押す
arrows = ['html.send_keys(Keys.DOWN)', 'html.send_keys(Keys.UP)', 'html.send_keys(Keys.LEFT)', 'html.send_keys(Keys.RIGHT)'] # 矢印のシーケンス
html = browser.find_element_by_tag_name('html')
for i in range(10):
this_arrow = random.choice(arrows)
exec(this_arrow)
time.sleep(1) # 次の矢印を送る前にちょっと待つ
@kotoripiyopiyo

This comment has been minimized.

Copy link
Owner Author

@kotoripiyopiyo kotoripiyopiyo commented Jan 25, 2021

学び:arrowsの中をKeys.DOWNとかKeys.UPにすると、文字列として渡されてしまい、html.send_keys('Keys.DOWN')とかhtml.send_keys('Keys.UP')になってしまう。これを回避するために実行コマンドexecを使った

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment