I can't think! I can't think! I use Python to automatically play 2048 games

Source: Internet
Author: User
Tags xpath



recently in the toss Selenium automation , the feeling of the crawler is very interesting, most of the previously difficult to simulate the site can crawl, tossing so long, so I want to play a 2048 game automatically . ! Hey, I am not good at playing games, before playing 2048 often got very low points, often think of all "bitterly", so I intend to take 2048 surgery, through several ways, let the program help me play!



Environment : Python 3.5



Library to use : Re, random, time, selenium



2048 Web Edition : http://2048.oubk.com



This article uses the selenium, before the operation needs to install the selenium and the Chromedriver driver beforehand



Check it out first.












The first step: Browse the Web to determine your needs












Can be found that this page can register login, there are various time periods of the points leaderboard, the game is played by the keyboard key, after the game is over, will pop up such a box












So I wonder if this page will be able to log in and out automatically, and also want to convert the 4*4 block to the corresponding matrix list, and after the game is over, you can automatically click and play again.



Try it, maybe, let's have a look at the analysis.



Step two: Analyze the target site on demand



Requirement 1: Login to specified account



Go to developer mode to navigate to the "sign in" element:












The Selenium library provides a number of ways to get elements from a Web page, and individuals prefer to get them through XPath, thus copying their XPath paths



After clicking Sign in, there will be a popup window:












In the same way, get the account input box password input box login XPath path












Requirement 2: Play again



This is the tip when the game is over






Image upload failed



Retry






Similarly, get the path of the XPath to play again












Requirement _3: Get matrix



Next, we need to convert the 4*4 block to a matrix, first to any number block.












It is easy to find that the first number is the value on the block, and then the number is: column lines, after the English string, then the block is a new build or a new merge. Using regular access












Image



Requirement _4: Analog arrow key



In selenium, the key value is sent to the element by means of the Send_keys () method



Note that the arrow keys are sent to the BODY element, not the div where the 4**4 block is located.












Step three: How to play the game



Method One: Random movement



Before the game is over, move around randomly, fractions or whatever.












Method Two: Each optimal



In the game before the end, each move, all choose the highest score direction, this is more complex (can see the source at the end of the text)



Fourth step: Run the program to see the results



1). Register with JS code



Borrowed a section from the Internet with JS for post submission code, successfully implemented the registration












2). Start playing the game



I've written an analytic function, the number of passes, the average score after the game is over.












The number of times I set to 3, executes the program, it will appear at the beginning.



Want to get the source of the students can private messages to me, private key words: source code, Source!



The latest GitHub programming language rankings are out, let's take a look at the latest news












Most popular languages: JS also first, Python overtook Java












GitHub Universe is the annual event of GitHub, this October 10-12th, the GitHub Universe conference in San Francisco. At the same time, GitHub also published the 2017 annual data Report--github Octoverse report on its website.



Every year, GitHub launches the "Octoverse" report, which lists some of the year's data, including the most popular programming languages, open source projects, and so on in their websites. This year's data is a bright one, so let's take a look.



Overall data:


24 million users

1.5 million organization

67 million Repo

1.3 Million student developer

Since September 2016, 1 billion commits

Since September 2016, 25.3 million active projects

Since September 2016, 12.5 million active Issue

Since September 2016, 6.7 million developers have been added

Since September 2016, a total of 451,000 new organizations have been added to GitHub

Since September 2016, 1.2 million people have submitted their first pull Request

Since September 2016, 4.1 million people have created their first Repo

191,000 people star or fork the project without committing the code

Since September 2016, 1.3 million new pull Request, including Linus Torvald's first GitHub pull Request:https://github.com/subsurface-divelog/subsurf ace/pull/155

Combined 100,000,000 pull Request

68,800,000 Issue Closed

Excluding frames and languages, the following tags are used most

Machine-learning

Game

Ios

Api

Blog

Website

Deep-learning

Library

Arduino

Plugin

Database

Bot


Then look at



Top 15 most popular languages on GitHub












As you can see, Python has taken a second leap from last year's third in Java, compared to last year it added 40% pull Request. More and more developers are feeling "life is too short"?



Java was squeezed down to third place, but Java 9 was released shortly, and its modular nature was very popular with developers, and the supremacy was hard to shake.



The number one is still JavaScript, and it's a few blocks away from other languages. Lenovo has been the hot development of the front-end technology in recent years, so there is no big surprise.



Top 15 languages (in chronological order): JavaScript, Python, Java, Ruby, PHP, C + +, CSS, C #, Go, C, TypeScript, Shell, Swift, Scala, objective-c



Top 10 items to be forked












Top 10 Contributors












Top 10 items in Review



Top 10 items in discuss












The above four leaderboards, TensorFlow in which three have left their own names, artificial intelligence technology is more and more attention, experienced by the "AI first" in the 2016, artificial intelligence in 2017 also ushered in a greater development



Article reproduced from: Rookie Learn Python, in addition, here to recommend a learning Exchange q-un,719-139-688, just hope that we can learn to communicate together, although not guarantee that everyone is satisfied, but I will do my best to help everyone's study.









I can't think! I can't think! I use Python to automatically play 2048 games


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.