Github项目地址：点击打开

# 核心讲解

## 寻路算法

### 算法实现

[0,0,230,136,0,228,0,243,129,248,0,0,0,114,224,0,114,128,0,256,0,232,0,0,0,6,108,0,6,0,0,6,6,159,0,102,157,0,112,0,0,6,0,221,248,6,246,0,1310,6,0,0,0,0,0,0,0,138,237,243,6,157,224,0,128,158,139,0,134,0,6,0,0,113,258,6,0,6,0,0,227,6,0,3,115,28,105,6,6,0,6,133,126,0,3,108,6,133,0,216,146,0,6,6,0,0,0,106,6,0,6,0,0,6,215,255,152,6,0,6,136,6,0,102,0,6,0,6,0,28,6,6,158,0,6,116,0,113,6,6,0,0,6,6,0,6,0,6,6,6,6,133,0,258,6,6,0,6,6,0,6,0,6,0,0,154,108,0,230,142,6,6,0,6,0,27,0,0,6,0,0,118,6,5,0,6,6,0,6,135,0,221,154,248,0,234,6,6,103,6,3,136,6,0,0,6,0,257,6,6,0,6,0,1110,107,0,4,256,0,0,241,6,0,0,106,6,6,6,107,0,132,0,6,0,219,6,122,0,237,113,6,223,6,227,0,158,105,1210,0,133,129,0,27,210,158,218,6,241,6,0,1310,154,4,6,6,0,6,256,6,0,0,117,4,6,0,6,104,6,231,6,104,0,0,0,132,6,235,6,6,3,6,152,6,1311,6,239,0,235,113,6]

• 0代表空白位置
• 6代表障碍物石头
• 除了06以外的数值不是移动指令就是矿石奖励
• 数组的[0-5]是第1行数据、数组的[6-11]是第2行数据、以此类推

/**
* @desc 一维数组转二维数组
* @param {Array} arr 原数据
* @param {Number} num 每个维度的元素数量
*/
function ArrayOneToTwo(arr, num) {
let arrList = [];
arr.map((item, index) => {
if (index % num == 0) {
arrList.push([item]);
} else {
arrList[arrList.length - 1].push(item);
}
});
return arrList;
}

[[0,0,230,136,0,228],[0,243,129,248,0,0],[0,114,224,0,114,128],[0,256,0,232,0,0],[0,6,108,0,6,0],[0,6,6,159,0,102],[157,0,112,0,0,6],[0,221,248,6,246,0],[1310,6,0,0,0,0],[0,0,0,138,237,243],[6,157,224,0,128,158],[139,0,134,0,6,0],[0,113,258,6,0,6],[0,0,227,6,0,3],[115,28,105,6,6,0],[6,133,126,0,3,108],[6,133,0,216,146,0],[6,6,0,0,0,106],[6,0,6,0,0,6],[215,255,152,6,0,6],[136,6,0,102,0,6],[0,6,0,28,6,6],[158,0,6,116,0,113],[6,6,0,0,6,6],[0,6,0,6,6,6],[6,133,0,258,6,6],[0,6,6,0,6,0],[6,0,0,154,108,0],[230,142,6,6,0,6],[0,27,0,0,6,0],[0,118,6,5,0,6],[6,0,6,135,0,221],[154,248,0,234,6,6],[103,6,3,136,6,0],[0,6,0,257,6,6],[0,6,0,1110,107,0],[4,256,0,0,241,6],[0,0,106,6,6,6],[107,0,132,0,6,0],[219,6,122,0,237,113],[6,223,6,227,0,158],[105,1210,0,133,129,0],[27,210,158,218,6,241],[6,0,1310,154,4,6],[6,0,6,256,6,0],[0,117,4,6,0,6],[104,6,231,6,104,0],[0,0,132,6,235,6],[6,3,6,152,6,1311],[6,239,0,235,113,6]]

const COLUMN = 6; // 列数
const OBSTACLE = 6; // 障碍物
/**
* U 上方
* L 左方
* R 右方
* D 下方
*/
const mapsTrack = [
[3, 1, "U"],
[2, 2, "L"],
[4, 2, "D"],
[3, 3, "R"],
]; // xy坐标和相对于初始坐标([3, 2])的方向 例：目标坐标[3, 1]相较于初始坐标[3, 2]：x坐标相等，y坐标比初始坐标小1，所以目标在初始目标的上面，此时用'U'表示
const NAGETIVE_DIRECTION = {
"U": "D",
"L": "R",
"D": "U",
"R": "L",
};
/**
* @desc 计算行走轨迹
* @param {Array} maps 地图
*/
const getTarck = (maps) => {

const mapsTree = ArrayOneToTwo(maps, COLUMN);

// 过滤掉有障碍物的位置
const trackXY = mapsTrack.filter((item) => {
const x = item[0];
const y = item[1];
const xy = mapsTree[x][y];
return xy !== OBSTACLE; // 判断是否为障碍物
});

// 向目标方向移动后反方向移动回初始位置
const trackList = trackXY.map((item) => {
const direction = item[2];
const nDirection = NAGETIVE_DIRECTION[item[2]];
return [direction, nDirection];
}).flat();
return trackList;
};

["U","D","L","R","D","U","R","L"]

# 部署

## 本地部署：

\$ git clone https://github.com/yunying1/juejin_auto_sign
\$ cd ./juejin_auto_sign
\$ npm install

\$ npm run test

# 写在最后

## 更多

1. 自动刷到奖励上限
2. 刷图次数过多强制终止(防止异常情况导致死循环，毕竟云函数免费额度也是有限的)
3. 奖励过少自动刷新地图