Tooth uncle tutorial is easy to understand

I wrote a Lianliankan app with AutoJS, and then I wrote a lianliankan script,

Results show

Let’s take a look at lianliankan script production ideas

Collect the original picture information

  1. Coordinates of the upper left and lower right corner of the game area
  2. The number of rows in the game
  3. The original image of the grid

Adjust data structure

I define the little Cell as a Cell class, and then hang all the properties on it for easy call

function Cell(row, column) {
  this.row = row;
  this.column = column;
  this.state = Cell.state.NORMAL;
  this.width = 0;
  this.height = 0;
  this.centerX = 0;
  this.centerY = 0;
  this.left = 0;
  this.top = 0;
  this.right = 0;
  this.bottom = 0;
  this.canvas = null;
  this.number = null;
  this.originalImg = null;
  this.img = null;
  this.paint = paint;
}
Copy the code

Determine whether the cell is connected

This is the focus of this tutorial, and there are plenty of methods available online that you can use to write your own scripts.

Now let’s see how this script decides

I fall into three categories of connectivity

Three types of connectivity are graphically displayed

The first: two cells next to each other

Second: the space around one cell has an intersection with the space around another cell

The third: A set of Spaces around A cell, A,

The set of Spaces around the other cell, B,

The whitespace around set B, set C,

Set A and set C intersect

A couple of little questions

1 How to determine whether a cell is empty

Celll has a property called originalImg, which saves images from the grid at the beginning of the game,

After the screenshot is taken, compare the current image with the original image. If not, the grid will be empty

2. How to judge whether two grid pictures are the same

I’m using multi-point colorimetry, where I take color data from multiple points in one image and then go to another image;

If you can find multiple points that match the criteria, it means the two graphs are the same.

function isSameTwoImg(img1, img2) {
  // Find a fixed number of points in the middle of the image, and then look for more colors in the larger image
  let colorDataList = getColorDataList(img1);
  / / / Images and color - Images (https://pro.autojs.org/docs/#/zh-cn/images?id=imagesfindmulticolorsimg-firstcolor-colors-options)
  let firstColor = colorDataList[0] [2];
  let colors = colorDataList.slice(1);
  try {
    let p = images.findMultiColors(img2, firstColor, colors);
    return p;
  } catch (error) {
    log("error", error); }}function getColorDataList(img) {
  let count = 25;
  let row = Math.sqrt(count);
  let column = Math.sqrt(count);
  let padding = 10;
  let width = img.getWidth();
  let height = img.getHeight();
  let contentWidth = width - padding * 2;
  let contentHeight = height - padding * 2;
  let unitWidth = contentWidth / (row - 1);
  let unitHeight = contentHeight / (column - 1);
  let colorDataList = [];
  let rowIndex = 0;
  let columnIndex = 0;
  let firstPoint;
  for (let i = 0; i < count; i++) {
    let x = padding + unitWidth * rowIndex;
    let y = padding + unitHeight * columnIndex;
    if (i === 0) {
      firstPoint = {
        x: x,
        y: y,
      };
    }
    let color = images.pixel(img, x, y);
    let relativeX = x - firstPoint.x;
    let relativeY = y - firstPoint.y;
    colorDataList.push([relativeX, relativeY, color]);
    rowIndex++;
    if (rowIndex === row) {
      rowIndex = 0; columnIndex++; }}return colorDataList;
}
Copy the code

How can debug scripts be visualized

When debugging, I add a suspension window and paint the color above the grid that matches the conditions

Cell.prototype.renderDebug = function (color) {
  color = color || "#33f44336";
  paint.setColor(colors.parseColor(color));
  paint.setStrokeWidth(1);
  // paint.setStyle(Paint.Style.STROKE);
  paint.setStyle(Paint.Style.FILL);
  paint.setXfermode(null);
  let left = this.left;
  let top = this.top;
  let right = this.right;
  let bottom = this.bottom;
  let rect = new RectF(left, top, right, bottom);
  this.canvas.drawRect(rect, paint);
  // this.canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
};
Copy the code
function renderTwoCell(emptyCell, currentCell, cellList) {
  let emptyCellRow = emptyCell.row;
  let emptyCellColumn = emptyCell.column;
  let currentCellRow = currentCell.row;
  let currentCellColumn = currentCell.column;
  if (emptyCellRow === currentCellRow) {
    if (emptyCellColumn < currentCellColumn) {
      let len = currentCellColumn - emptyCellColumn;
      for (let i = 0; i < len; i++) {
        letcell = cellList[emptyCellRow][emptyCellColumn + i]; cell.renderDebug(); }}else {
      let len = emptyCellColumn - currentCellColumn;
      for (let i = 0; i < len; i++) {
        letcell = cellList[emptyCellRow][emptyCellColumn - i]; cell.renderDebug(); }}}else if (emptyCellColumn === currentCellColumn) {
    if (emptyCellRow < currentCellRow) {
      let len = currentCellRow - emptyCellRow;
      for (let i = 0; i < len; i++) {
        letcell = cellList[emptyCellRow + i][emptyCellColumn]; cell.renderDebug(); }}else {
      let len = emptyCellRow - currentCellRow;
      for (let i = 0; i < len; i++) {
        letcell = cellList[emptyCellRow - i][emptyCellColumn]; cell.renderDebug(); }}}}Copy the code

You can also print the status of the grid

function printCellData(cellList) { let str = ""; for (var i = 0; i < cellList.length; i++) { let cellRow = cellList[i]; for (var j = 0; j < cellRow.length; j++) { let cell = cellRow[j]; str += cell.isEmpty() ? "X" : "O"; } str += "\n"; }}Copy the code
XOOOOXOX
XOOOOXOX
XOOOOXXX
XOOOOXXX
XOOOOOOX
XOOOOOOX
XXXXXXXX
Copy the code

4 AutoJS8 and 9 modules are exported in different formats

autojs8

module.exports = {
  viewImg: viewImg,
  isSameTwoImg: isSameTwoImg,
};
Copy the code

autojs9

module.exports = {
  viewImg,
  isSameTwoImg,
};
Copy the code

note

If you want to use scripts, you need to modify the config

left, top, right, bottom
Copy the code

Because mobile phones are different, the display area of the game is different

The environment

Phone: Mi 11 Pro

Android version: 12

Autojs version: 9.1.3

Quotes.

Ideas are the most important, other Baidu, Bing, StackOverflow, Github, Android docs, AutoJS docs, and last but not least, ask in the group

The statement

This tutorial is intended for learning purposes only and is not intended for any other use

Wechat official account tooth Uncle tutorial