Introduction

I made a Python Script to Automate a Sudoku Game on Android after watching Engineer Man’s Videos on Youtube doing the same for different games.

The script can be divided into 5 parts

  1. Connecting to an Android device using ADB, and getting the screenshot of the game from it
  2. Using Pillow to process the screenshot for pytesseract
  3. Using pytesseract to extract the Sudoku Game Grid to a 2D List in Python.
  4. Solving the Sudoku Game
  5. Sending the solved input to your Android Device using Python

Out of the 5, I will be focusing mostly on 2,3 & 5 as 1 & 4 are topics that have been extensively covered.

Link to the game I automated: https://play.google.com/store/apps/details?id=com.quarzo.sudoku

The complete code is available on the following repository:

Github: haideralipunjabi/sudoku_automate

You can also watch the script in action:


Libraries Used


Tutorial

1 (a). Using ADB to Connect to your Device

Most of the tutorials on internet use Wired ADB, which discourages many people from using this method. I will be using Wireless ADB, which isn’t very difficult to setup.

  1. Go to your Phone Settings > System > Developer Options (This might vary in different phones, so if it is not the same in your’s, look it up on the internet)
  2. Turn on Android Debugging and ADB over Network.
  3. Note the IP Address and Port shown under ADB over Network
  4. Install ADB on your computer
  5. Go to your command-line / command prompt and enter
  6. adb connect :
  7. Use the IP Address and Port from Step 3
  8. When connecting for the first time, you will need to authorize the connection on your phone.
  9. Your device should be connected to your PC over WiFi.

1 (b). Using ADB with Python (pure-python-adb)

You can define the following function to connect to the first ADB device connected to your computer using Python

from ppadb.client import Client

def connect_device():
    adb = Client(host='127.0.0.1',port=5037)
    devices = adb.devices()
    if len(devices) == 0:
        print("No Devices Attached")
        quit()
    return devices[0]

We will be using this function later to return an instance of ppadb.device.Device which will be used to take a screenshot, and send input to your device.

1 c. Taking a Screenshot and saving it

pure-python-adb makes it very easy to capture a screenshot of your device. The screencap function is all that you need to get the screenshot. Use Pythons File IO to save it to screen.png

def take_screenshot(device):
    image = device.screencap()
    with open('screen.png', 'wb') as f:
        f.write(image)

#python #androi #automation

Automating Android Games with Python & Pytesseract: Sudoku
77.00 GEEK