How to automatically hang up 2048 games

Source: Internet
Author: User
Preface

2048 games have you played? Https://gabrielecirulli.github.io/2048/can play online

People's energy is always limited, it is impossible to play around the night, but the machine can; do an auto play 2048 game small function, familiar with the use of selenium

Analysis

2048 the essence of the game is through four key, to synthesize the number, in fact, the process of a single, boring (do not pay attention to the thinking of people), the machine is good at doing this.

The use of selenium can open the browser, send keyboard instructions, such as a series of operations;

Games will have game over when the selenium send four directional key instructions is the norm, then solve the game over problem is a special treatment

Label

1) score:<div class= "score-container" >0</div>

2) Game over: <div class= "game-message" ><p>game over!</p></div>

    Note: In normal game state,,<p> value is empty, game over! is displayed at the end, according to this feature to determine whether the game is over

3) Try again: <a class= "Retry-button" >try again</a>

    Note: When the game is over, you need to find the button and click it to resume the game.

Environment

1) Windows 7

2) This is a simple feature that is written directly under Python idle

3) using the Firefox browser, need to install the driver, you can download (), I was directly under the System32

source code

def play2048 (): From selenium import Webdriverfrom Selenium.webdriver.common.keys Import keysimport Time # opens Firefox and accesses the 2048 game interface bs = Webdriver. Firefox () bs.get (' https://gabrielecirulli.github.io/2048/') HTML = bs.find_element_by_tag_name (' HTML ') while True: Print (' Send Up,right,down,left ') Html.send_keys (keys.up) time.sleep (0.3) Html.send_keys (keys.right) time.sleep (0.3) Html.send_keys (Keys.down) time.sleep (0.3) Html.send_keys (keys.left) Time.sleep (0.3) # Determine if the game is over in every four directions Game_over = Bs.find_element_by_css_selector ('. Game-message>p ') if game_over.text = = ' Game over! ': score = Bs.find_ Element_by_class_name (' Score-container ') #当前得分print (' Game over, score is%s '% score.text) print (' Wait 3 seconds, try Ag Ain ') Time.sleep (3) # After the game is over, wait 3 seconds, automatically click try again restart Try_again = Bs.find_element_by_class_name (' Retry-button ') try_a Gain.click () 

Run

In Python idle, call play2048 (), the program automatically executes the following steps:

1) Open Firefox

2) in the currently open Firefox window, visit https://gabrielecirulli.github.io/2048/

3) Wait for the page to load and start sending four directional arrows

4) When game over, auto try again

5) infinite loop steps 3 and 4

Interested can try, or a bit of meaning ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.