121 lines
4.3 KiB
JavaScript
121 lines
4.3 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import boardConstructor from "@/classes/boardConstructor";
|
|
import { useGameStore } from "./game";
|
|
|
|
export const useBotStore = defineStore("bot", {
|
|
state: () => ({
|
|
items: {},
|
|
}),
|
|
actions: {
|
|
generate() {
|
|
const game = useGameStore();
|
|
const board = new boardConstructor(game.boardSize, game.getAvailableItems());
|
|
let items = board.generateItems();
|
|
this.items = board.generateBoard(items);
|
|
let playerItems = {};
|
|
Object.values(this.items).forEach((item) => {
|
|
playerItems[item.id] = { size: item.size, margin: item.margin };
|
|
});
|
|
game.opponent.id = "bot";
|
|
game.opponent.items = playerItems;
|
|
game.opponent.status = "ready";
|
|
},
|
|
async makeMove() {
|
|
const game = useGameStore();
|
|
if (game.winner != null) return
|
|
await game.sleep(1000)
|
|
let targetCell = this.getTargetCell()
|
|
console.log('%c Bot move ', 'color:#e2a520;', targetCell);
|
|
let move = await game.checkMove(targetCell, "bot");
|
|
await game.completeMove(move)
|
|
switch (move.item.type) {
|
|
case "fish":
|
|
await this.makeMove();
|
|
break;
|
|
}
|
|
},
|
|
async checkMove(targetCell) {
|
|
const game = useGameStore();
|
|
let move = {};
|
|
let targetItem = Object.values(this.items).filter((item) =>
|
|
item.cells.includes(targetCell)
|
|
)[0];
|
|
move.targetCell = targetCell;
|
|
move.player = game.turn.player;
|
|
move.opponent = game.turn.opponent;
|
|
move.create = new Date();
|
|
move.status = "move";
|
|
if (targetItem != undefined) {
|
|
move.item = {
|
|
id: targetItem.id,
|
|
type: targetItem.type,
|
|
name: targetItem.name,
|
|
};
|
|
} else {
|
|
move.item = {
|
|
id: "empty",
|
|
type: "empty",
|
|
name: "empty",
|
|
};
|
|
}
|
|
return move;
|
|
},
|
|
getTargetCell() {
|
|
return this.mediumMode();
|
|
},
|
|
easyMode() {
|
|
const game = useGameStore();
|
|
let moves = [];
|
|
game.moves.forEach((move) => {
|
|
if (move.player == "bot") moves.push(move.targetCell);
|
|
});
|
|
let board = Array.from(
|
|
Array(game.boardSize * game.boardSize).keys()
|
|
);
|
|
let availableMoves = board.filter((cell) => !moves.includes(cell));
|
|
let targetCell =
|
|
availableMoves[
|
|
Math.floor(Math.random() * availableMoves.length)
|
|
];
|
|
return targetCell;
|
|
},
|
|
mediumMode() {
|
|
const game = useGameStore();
|
|
let moves = [];
|
|
game.moves.forEach((move) => {
|
|
if (move.player == "bot") moves.push(move.targetCell);
|
|
});
|
|
let board = Array.from(
|
|
Array(game.boardSize * game.boardSize).keys()
|
|
);
|
|
let availableMoves = board.filter((cell) => !moves.includes(cell));
|
|
let marginCells = this.getMarginCells()
|
|
availableMoves = availableMoves.filter((cell) => !marginCells.includes(cell));
|
|
let targetCell =
|
|
availableMoves[
|
|
Math.floor(Math.random() * availableMoves.length)
|
|
];
|
|
return targetCell;
|
|
},
|
|
getMarginCells() {
|
|
const game = useGameStore();
|
|
let items = {}
|
|
game.moves.forEach((move) => {
|
|
if (move.player != "bot" || move.item.id == 'empty') return
|
|
if (items[move.item.id] == undefined) items[move.item.id] = 0
|
|
items[move.item.id]++
|
|
});
|
|
let marginCells = []
|
|
Object.keys(items).forEach((itemId) => {
|
|
if (game.player.items[itemId].size == items[itemId]) {
|
|
game.player.items[itemId].marginCells.forEach((cell) => {
|
|
marginCells.push(cell)
|
|
})
|
|
}
|
|
})
|
|
return marginCells
|
|
},
|
|
},
|
|
persist: true,
|
|
});
|