- tryMoveFollowup(move, cb) {
- // Warning: at this stage, the move is played
- if (move.vanish.length == 2 && move.vanish[1].c == 'a') {
- // effect, or bonus/malus
- const endType = move.vanish[1].p;
- switch (endType) {
- case V.EGG:
- this.applyRandomBonus(move, cb);
- break;
- case V.BANANA:
- case V.BOMB: {
- const dest =
- this.getRandomSquare([m.end.x, m.end.y],
- endType == V.BANANA
- ? [[1, 1], [1, -1], [-1, 1], [-1, -1]]
- : [[1, 0], [-1, 0], [0, 1], [0, -1]]);
- cb(this.getBasicMove([move.end.x, move.end.y], dest));
- break;
- }
- case V.MUSHROOM: {
- let step = [move.end.x - move.start.x, move.end.y - move.start.y];
- if ([0, 1].some(i => step[i] >= 2 && step[1-i] != 1)) {
- // Slider, multi-squares: normalize step
- for (let j of [0, 1])
- step[j] = step[j] / Math.abs(step[j]) || 0;
- }
- const nextSquare = [move.end.x + step[0], move.end.y + step[1]];
- if (this.onBoard(nextSquare[0], nextSquare[1])) {
- if (
- this.board[nextSquare[0]][nextSquare[1]] != "" &&
- this.getColor(nextSquare[0], nextSquare[1]) != 'a'
- ) {
- // (try to) jump
- const afterSquare =
- [nextSquare[0] + step[0], nextSquare[1] + step[1]];
- if (
- this.onBoard(afterSquare[0], afterSquare[1]) &&
- this.getColor(afterSquare[0], afterSquare[1]) != this.turn
- ) {
- cb(this.getBasicMove([move.end.x, move.end.y], afterSquare));
- }
- }
- else if (!['b', 'r', 'q'].includes(move.vanish[0].p))
- // Take another step forward if not slider move
- cb(this.getBasicMove([move.end.x, move.end.y], nextSquare));
- }
- break;
+ // Helper to set and apply banana/bomb effect
+ getRandomSquare([x, y], steps, freeSquare) {
+ let validSteps = steps.filter(s => this.onBoard(x + s[0], y + s[1]));
+ if (freeSquare) {
+ // Square to put banana/bomb cannot be occupied by a piece
+ validSteps = validSteps.filter(s => {
+ return ["", 'a'].includes(this.getColor(x + s[0], y + s[1]))
+ });
+ }
+ if (validSteps.length == 0)
+ return null;
+ const step = validSteps[Random.randInt(validSteps.length)];
+ return [x + step[0], y + step[1]];
+ }
+
+ getEggEffect(move) {
+ const getRandomPiece = (c) => {
+ let bagOfPieces = [];
+ for (let i=0; i<this.size.x; i++) {
+ for (let j=0; j<this.size.y; j++) {
+ if (this.getColor(i, j) == c && this.getPiece(i, j) != 'k')
+ bagOfPieces.push([i, j]);