32 Star 116 Fork 30

AvenirTech 未来科技/Honinbo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test.js 9.09 KB
一键复制 编辑 原始数据 按行查看 历史
吉法师 提交于 2021-10-05 23:41 . 重建仓库
//测试一些东西
let ifBlack = true;
let tracks = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
async function main() {
let killed = kill(0, 2, null, true, null, null, null, null);
console.log('killed = ', killed);
}
function kill(x, y, dead, first, life, checked, tempIsBlack, array) {
//遍历过的不要重复
if (tracks?.[x]?.[y] === undefined|| tracks[x][y] !== 0) {
return dead;
} else {
tracks[x][y] = 1;
}
if(board[x] == undefined || board[x][y] == undefined) {
return dead;
}
dead = dead || {
w: {
isKilled: false,
dead: [],
},
a: {
isKilled: false,
dead: [],
},
s: {
isKilled: false,
dead: [],
},
d: {
isKilled: false,
dead: [],
},
};
let isBlack = tempIsBlack || ifBlack;
//四个方向
let up = {
x,
y: y++,
};
let down = {
x,
y: y--,
};
let left = {
x: x--,
y,
};
let right = {
x: x++,
y,
};
console.log('left = ', left)
console.log('right = ', right);
console.log('board[x][y] :>> ', board[x][y]);
// 有气就不算死棋 没有气则直接把自己放入死字数组 开始遍历相连的黑棋
if (first) {
//去遍历各个位置的棋
if (board?.[up.x]?.[up.y] === isBlack ? -1 : 1) {
dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "w", true, "w"));
}
if (board?.[down.x]?.[down.y] === isBlack ? -1 : 1) {
dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "a", true, "a"));
}
if (board?.[left.x]?.[left.y] === isBlack ? -1 : 1) {
dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "s", true, "s"));
}
if (board?.[right.x]?.[right.y] === isBlack ? -1 : 1) {
dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "d", true, "d"));
}
} else {
//第二次遍历了
if (checked === "w") {
//周围一圈没气 这个棋就是死棋
if (
board?.[up.x]?.[up.y] !== 0 &&
board?.[left.x]?.[left.y] !== 0 &&
board?.[right.x]?.[right.y] !== 0
) {
dead[array].dead.push({ x, y });
dead[array].isKilled = true;
} else {
dead[array].isKilled = false;
if ((board?.[up.x]?.[up.y] == isBlack) === true ? -1 : 1) {
dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "w", null,array));
}
if ((board?.[left.x]?.[left.y] == isBlack) === true ? -1 : 1) {
dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "a", null,array));
}
if ((board?.[right.x]?.[right.y] == isBlack) === true ? -1 : 1) {
dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "d", null,array));
}
}
} else if (checked === "a") {
if (
board?.[up.x]?.[up.y] !== 0 &&
board?.[down.x]?.[down.y] !== 0 &&
board?.[left.x]?.[left.y] !== 0
) {
dead[array].dead.push({ x, y });
dead[array].isKilled = true;
} else {
dead[array].isKilled = false;
if ((board?.[up.x]?.[up.y] == isBlack) === true ? -1 : 1) {
dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "u", null,array));
}
if ((board?.[left.x]?.[left.y] == isBlack) === true ? -1 : 1) {
dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "a", null,array));
}
if ((board?.[down.x]?.[down.y] == isBlack) === true ? -1 : 1) {
dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "s", null,array));
}
}
} else if (checked === "s") {
if (
board?.[down.x]?.[down.y] !== 0 &&
board?.[left.x]?.[left.y] !== 0 &&
board?.[right.x]?.[right.y] !== 0
) {
dead[array].isKilled = true;
dead[array].dead.push({ x, y });
} else {
console.log('dead :>> ', dead);
console.log('array :>> ', array);
dead[array].isKilled = false;
if ((board?.[left.x]?.[left.y] === isBlack) ? -1 : 1) {
dead = concatKill(dead,kill(left.x, left.y, dead, false, null, "a", null,array));
}
if ((board?.[down.x]?.[down.y] === isBlack) ? -1 : 1) {
dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "s", null,array));
}
if ((board?.[right.x]?.[right.y] === isBlack) ? -1 : 1) {
dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "d", null,array));
}
}
} else if (checked === "d") {
if (
board?.[up.x]?.[up.y] !== 0 &&
board?.[down.x]?.[down.y] !== 0 &&
board?.[right.x]?.[right.y] !== 0
) {
dead[array].dead.push({ x, y });
dead[array].isKilled = true;
} else {
dead[array].isKilled = false;
if ((board?.[up.x]?.[up.y] === isBlack) ? -1 : 1) {
dead = concatKill(dead,kill(up.x, up.y, dead, false, null, "w", null,array));
}
if ((board?.[down.x]?.[down.y] === isBlack) ? -1 : 1) {
dead = concatKill(dead,kill(down.x, down.y, dead, false, null, "s", null,array));
}
if ((board?.[right.x]?.[right.y] === isBlack) ? -1 : 1) {
dead = concatKill(dead,kill(right.x, right.y, dead, false, null, "a", null,array));
}
}
}
console.log('lastdead :>> ', dead);
return dead;
}
}
function concatKill(srcKill,destKill) {
return{
w: {
isKilled: srcKill.w.isKilled,
dead: srcKill.w.dead.concat(destKill.w.dead),
},
a: {
isKilled: srcKill.a.isKilled,
dead: srcKill.a.dead.concat(destKill.a.dead),
},
s: {
isKilled: srcKill.s.isKilled,
dead: srcKill.s.dead.concat(destKill.s.dead),
},
d: {
isKilled: srcKill.d.isKilled,
dead: srcKill.d.dead.concat(destKill.d.dead),
dead: [],
},
}
}
main();
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/onlyyyy/honinbo.git
[email protected]:onlyyyy/honinbo.git
onlyyyy
honinbo
Honinbo
product

搜索帮助

0d507c66 1850385 C8b1a773 1850385