<template>
<div>
<div>
<input v-model="wh" name="wh" type="number" />
<button @click="reset">重置</button>
</div>
<canvas
id="checkerboard"
:width="wh"
:height="wh"
@click="handlerClick"
></canvas>
</div>
</template>
<script>
export default {
data() {
return {
wh: 999,
interval: 0,
init_X_Y: 10,
r: 12,
points: [],
exist_point: [],
operate: "user",
direction: [
"up",
"down",
"left",
"right",
"left_up",
"right_down",
"left_down",
"right_up",
],
state: 0,
};
},
methods: {
handlerClick(e) {
if (this.state != 0) return;
if (this.operate == "computer") return;
for (let item of this.points) {
if (
Math.abs(e.offsetX - item.x) < this.r &&
Math.abs(e.offsetY - item.y) < this.r &&
!item.exist
) {
let canvas = document.getElementById("checkerboard");
let g2 = canvas.getContext("2d");
g2.beginPath();
g2.fillStyle = "white";
g2.arc(item.x, item.y, this.r, 0, 2 * Math.PI);
g2.fill();
this.operate = "computer";
item.exist = true;
item.operate = "user";
this.win(item, "user");
if (this.state == 0) this.computerMspaint();
}
}
},
computerClick() {
this.exist_point;
this.points;
},
mspaint() {
let interval = this.interval;
let init_X_Y = this.init_X_Y;
let r = this.r;
let canvas = document.getElementById("checkerboard");
let g2 = canvas.getContext("2d");
for (let i = 0; i < 16; i++) {
let line_X_Y = init_X_Y + i * interval;
g2.beginPath();
g2.strokeStyle = "black";
g2.lineWidth = 2;
g2.moveTo(line_X_Y, 10);
g2.lineTo(line_X_Y, this.wh - 10);
g2.closePath();
g2.stroke();
g2.beginPath();
g2.strokeStyle = "black";
g2.lineWidth = 2;
g2.moveTo(10, line_X_Y);
g2.lineTo(this.wh - 10, line_X_Y);
g2.closePath();
g2.stroke();
}
},
getAllPoint() {
this.points = [];
for (let i = 0; i < 16; i++) {
let x = i * this.interval + this.init_X_Y;
for (let j = 0; j < 16; j++) {
let y = j * this.interval + this.init_X_Y;
this.points.push({ x, y, exist: false });
}
}
},
checkPoint() {
let maxWeight = 0;
let final = {};
let lr, ud, lu_rd, ld_ru;
for (let item of this.pointDiff) {
let weight = 0;
let userWeight = this.direction.map((e) => {
let arr = this.bearingFor(item, e, "user");
let decrement = 0;
if (arr >= 2) {
let arrLeft = this.bearingFor(arr[0], e, "computer").length > 0;
let arrRight = this.bearingFor(arr[arr.length - 1], e, "user").length;
if (arrLeft > 0) decrement += 20;
if (arrRight > 0) decrement += 20;
}
return this.getCount(arr.length, decrement);
});
ud = userWeight[0] + userWeight[1];
lr = userWeight[2] + userWeight[3];
lu_rd = userWeight[4] + userWeight[5];
ld_ru = userWeight[6] + userWeight[7];
if (ud > maxWeight) {
maxWeight = ud;
final = item;
} else if (lr > maxWeight) {
maxWeight = lr;
final = item;
} else if (lu_rd > maxWeight) {
maxWeight = lu_rd;
final = item;
} else if (ld_ru > maxWeight) {
maxWeight = ld_ru;
final = item;
}
}
for (let item of this.pointDiff) {
let weight = 0;
let userWeight = this.direction.map((e) => {
let arr = this.bearingFor(item, e, "computer");
let decrement = 0;
if (arr >= 2) {
let arrLeft = this.bearingFor(arr[0], e, "user").length > 0;
let arrRight = this.bearingFor(arr[arr.length - 1], e, "user").length;
if (arrLeft > 0) decrement += 20;
if (arrRight > 0) decrement += 20;
}
return this.getCount(arr.length, decrement);
});
ud = userWeight[0] + userWeight[1];
lr = userWeight[2] + userWeight[3];
lu_rd = userWeight[4] + userWeight[5];
ld_ru = userWeight[6] + userWeight[7];
if (ud >= maxWeight) {
maxWeight = ud;
final = item;
} else if (lr >= maxWeight) {
maxWeight = lr;
final = item;
} else if (lu_rd >= maxWeight) {
maxWeight = lu_rd;
final = item;
} else if (ld_ru >= maxWeight) {
maxWeight = ld_ru;
final = item;
}
}
return final;
},
computerMspaint() {
let final = this.checkPoint();
let canvas = document.getElementById("checkerboard");
let g2 = canvas.getContext("2d");
g2.beginPath();
g2.fillStyle = "black";
g2.arc(final.x, final.y, this.r, 0, 2 * Math.PI);
g2.fill();
this.points.forEach((e) => {
if (e.x == final.x && e.y == final.y) {
e.exist = true;
e.operate = "computer";
}
});
this.operate = "user";
this.win(final, "computer");
},
userNearbyPoint() {
let userPoint = this.points.filter((e) => {
return e.operate == "user";
});
let arr = [];
for (let item of userPoint) {
let right = this.points.find((e) => {
return (
e.x == item.x + this.interval && e.y == item.y && e.exist == false
);
});
if (right) arr.push(right);
let right_up = this.points.find((e) => {
return (
e.x == item.x + this.interval &&
e.y == item.y - this.interval &&
!e.exist
);
});
if (right_up) arr.push(right_up);
let right_down = this.points.find((e) => {
return (
e.x == item.x + this.interval &&
e.y == item.y + this.interval &&
!e.exist
);
});
if (right_down) arr.push(right_down);
let left = this.points.find((e) => {
return e.x == item.x - this.interval && e.y == item.y && !e.exist;
});
if (left) arr.push(left);
let left_up = this.points.find((e) => {
return (
e.x == item.x - this.interval &&
e.y == item.y - this.interval &&
!e.exist
);
});
if (left_up) arr.push(left_up);
let left_down = this.points.find((e) => {
return (
e.x == item.x - this.interval &&
e.y == item.y + this.interval &&
!e.exist
);
});
if (left_down) arr.push(left_down);
let up = this.points.find((e) => {
return e.x == item.x && e.y == item.y - this.interval && !e.exist;
});
if (up) arr.push(up);
let down = this.points.find((e) => {
return e.x == item.x && e.y == item.y + this.interval && !e.exist;
});
if (down) arr.push(down);
}
return arr;
},
bearingFor(item, direction, operate) {
let obj = [];
let dirPoint = {};
switch (direction) {
case "up":
dirPoint = this.points.find((e) => {
return (
e.x == item.x &&
e.y == item.y - this.interval &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [dirPoint, ...this.bearingFor(dirPoint, "up", operate)];
}
break;
case "down":
dirPoint = this.points.find((e) => {
return (
e.x == item.x &&
e.y == item.y + this.interval &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [dirPoint, ...this.bearingFor(dirPoint, "down", operate)];
}
break;
case "left":
dirPoint = this.points.find((e) => {
return (
e.x == item.x - this.interval &&
e.y == item.y &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [dirPoint, ...this.bearingFor(dirPoint, "left", operate)];
}
break;
case "right":
dirPoint = this.points.find((e) => {
return (
e.x == item.x + this.interval &&
e.y == item.y &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [dirPoint, ...this.bearingFor(dirPoint, "right", operate)];
}
break;
case "right_up":
dirPoint = this.points.find((e) => {
return (
e.x == item.x + this.interval &&
e.y == item.y - this.interval &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [dirPoint, ...this.bearingFor(dirPoint, "right_up", operate)];
}
break;
case "right_down":
dirPoint = this.points.find((e) => {
return (
e.x == item.x + this.interval &&
e.y == item.y + this.interval &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [
dirPoint,
...this.bearingFor(dirPoint, "right_down", operate),
];
}
break;
case "left_up":
dirPoint = this.points.find((e) => {
return (
e.x == item.x - this.interval &&
e.y == item.y - this.interval &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [dirPoint, ...this.bearingFor(dirPoint, "left_up", operate)];
}
break;
case "left_down":
dirPoint = this.points.find((e) => {
return (
e.x == item.x - this.interval &&
e.y == item.y + this.interval &&
e.exist &&
e.operate == operate
);
});
if (dirPoint != null) {
obj = [
dirPoint,
...this.bearingFor(dirPoint, "left_down", operate),
];
}
break;
}
return obj;
},
getCount(len, decrement = 0) {
if (len == 3) return 50 - decrement;
if (len == 4) return 70 - decrement;
if (len >= 5) return 999 - decrement;
return len;
},
win(item, flag) {
let winArr = [];
this.direction.forEach((e) => {
let arr = this.bearingFor(item, e, flag);
if (arr.length == 4) {
if (flag == "user") this.state = 1;
else this.state = 2;
winArr = arr;
let canvas = document.getElementById("checkerboard");
let g2 = canvas.getContext("2d");
g2.beginPath();
g2.strokeStyle = "#fc011a";
g2.lineCap = "round";
g2.lineWidth = 8;
g2.moveTo(item.x, item.y);
g2.lineTo(arr[arr.length - 1].x, arr[arr.length - 1].y);
g2.closePath();
g2.stroke();
}
});
},
reset() {
let canvas = document.getElementById("checkerboard");
let g2 = canvas.getContext("2d");
g2.clearRect(0, 0, this.wh, this.wh);
this.mspaint();
this.getAllPoint();
this.state = 0;
},
},
mounted() {
this.mspaint();
this.getAllPoint();
},
watch: {
operate: {
handler(newVal, oldVal) {
if (newVal == "computer") {
setTimeout(() => {}, 200);
}
},
immediate: true,
},
state(newVal) {
if (newVal != 0) newVal == 1 ? alert("用户胜利") : alert("电脑胜利");
},
wh: {
handler(newVal) {
this.interval = (newVal - this.init_X_Y*2) / 15;
setTimeout(() => {
this.reset();
}, 100);
},
immediate: true,
},
},
computed: {
pointDiff() {
return this.points.filter((e) => !e.exist);
},
userPoint() {
return this.points.filter((e) => e.operate == "user");
},
computerPoint() {
return this.points.filter((e) => e.operate == "computer");
},
},
};
</script>
<style>
#checkerboard {
background-color: #e4caa6;
}
</style>