DEV Community

WRWRuggiaman
WRWRuggiaman

Posted on

1 to 50 - MAN VS SELENIUM

With some friends we discover the game 1To50.

THE GAME

The game consists of a grid composed of 25 boxes, in every box there is a number from 1 to 25.
After clicking the box, the number inside is replaced with a number from 26 to 50.
The player should click the numbers in increasing order from 1 to 50.

Human problems

The human player has the following problems:

  1. The man has trouble to remember the order of all numbers when he is trying to play the game in a fast way.
  2. The man has difficulty to click fast the numbers.
    • Move the cursor take time
  3. After the number 26 the man should find the numbers.

The result of my friends are in the range 45 seconds and 35 seconds.
(I'm terrible, my best result is 43 seconds)

The challange
In order to defeat my friends I decided to automate the game with Selenium.

The first thing to do is open the page. (I used firefox [geckodriver]):

driver = webdriver.Firefox(executable_path=path_geckodriver)
driver.get("http://zzzscore.com/1to50/en/?ts=1565597472067#")
Enter fullscreen mode Exit fullscreen mode

After opening the page i need to find the grid where all the numbers are

grid = driver.find_element_by_id('grid')
Enter fullscreen mode Exit fullscreen mode

The last thing is make a loop to click in increasing order all the numbers inside the grid

for number in range(1, 51):
    grid.find_element_by_xpath('//div[text()={}]'.format(number)).click()
Enter fullscreen mode Exit fullscreen mode

DONE!
The final code to automate the game is:

from selenium import webdriver
path_geckodriver = r"path_where_is_the_geckodriver"

driver = webdriver.Firefox(executable_path=path_geckodriver)
driver.get("http://zzzscore.com/1to50/en/?ts=1565597472067#")
grid = driver.find_element_by_id('grid')
for number in range(1, 51):
    grid.find_element_by_xpath('//div[text()={}]'.format(number)).click()
driver.close()
Enter fullscreen mode Exit fullscreen mode

All the human problems are removed.

The final results of the software is:
5.802 seconds

35 seconds of difference(80% less time)

NEW WAY TO SEE
Do you know different way to automate the game?
Is it faster or slower?
Let's talk about.
I'm curious to know and learn new things.

Top comments (1)

Collapse
 
ycmjason profile image
YCM Jason

There is no need to use selenium. Just some simple javascript in the console will do.

(async () => {
  const getValidBoxes = () => [...document.querySelectorAll("#grid > div")].filter(d => d.style.opacity === '1')
  while (getValidBoxes().length > 0) {
    getValidBoxes().forEach(d => d.dispatchEvent(new Event('tap')))
    await new Promise(res => requestIdleCallback(res))
  }
})()

Imgur