boardgame-core/src/samples/boop/commands/graduate.ts

41 lines
1.5 KiB
TypeScript

import {BoopGame} from "@/samples/boop/types-extensions";
import {PlayerType} from "@/samples/boop/types";
import {findPartAtPosition, findPartInRegion, getLineCandidates} from "@/samples/boop/utils";
import {WIN_LENGTH} from "@/samples/boop/constants";
import {moveToRegion} from "@/core/region";
/**
* 检查并执行小猫升级(三个小猫连线变成猫)
*/
export async function checkGraduates(game: BoopGame){
const toUpgrade = new Set<string>();
for(const line of getLineCandidates()){
let whites = 0;
let blacks = 0;
for(const [row, col] of line){
const part = findPartAtPosition(game, row, col);
if (part?.player === 'white') whites++;
else if(part?.player === 'black') blacks++;
}
const player = whites >= WIN_LENGTH ? 'white' : blacks >= WIN_LENGTH ? 'black' : null;
if(!player) continue;
for(const [row, col] of line){
const part = findPartAtPosition(game, row, col);
part && toUpgrade.add(part.id);
}
}
await game.produceAsync(state => {
for(const partId of toUpgrade){
const part = state.pieces[partId];
const [row, col] = part.position;
const player = part.player;
moveToRegion(part, state.regions.board, null);
const newPart = findPartInRegion(state, '', 'cat', player);
moveToRegion(newPart || part, null, state.regions[player], [row, col]);
}
});
}