applyEffects method

BishopState applyEffects({
  1. required Iterable<ActionEffect> effects,
  2. required BoardSize size,
  3. Zobrist? zobrist,
})

Implementation

BishopState applyEffects({
  required Iterable<ActionEffect> effects,
  required BoardSize size,
  Zobrist? zobrist,
}) {
  if (invalidMove) return this;
  if (effects.isEmpty) return this;
  int hash = this.hash;
  List<int> board = [...this.board];
  List<int> pieces = [...this.pieces];
  GameResult? result = this.result;
  List<Hand>? hands = this.hands != null
      ? List.generate(this.hands!.length, (i) => List.from(this.hands![i]))
      : null;

  for (ActionEffect effect in effects) {
    if (effect is EffectModifySquare) {
      if (zobrist != null) {
        hash ^= zobrist.table[effect.square][effect.content.piece];
      }
      bool capture = board[effect.square].isNotEmpty;
      if (capture) {
        int piece = board[effect.square].piece;
        if (zobrist != null) hash ^= zobrist.table[effect.square][piece];
        pieces[piece]--;
      }

      board[effect.square] = effect.content;
      if (effect.content.isNotEmpty) {
        pieces[effect.content.piece]++;
      }
    } else if (effect is EffectSetCustomState) {
      board[effect.variable + size.h] = effect.value << Bishop.flagsStartBit;
    } else if (effect is EffectSetGameResult) {
      result = effect.result;
    } else if (effect is EffectAddToHand) {
      if (hands != null) {
        hands[effect.player].add(effect.piece);
        pieces[makePiece(effect.piece, effect.player)]++;
      }
    } else if (effect is EffectRemoveFromHand) {
      if (hands != null) {
        if (hands[effect.player].contains(effect.piece)) {
          hands[effect.player].remove(effect.piece);
          pieces[makePiece(effect.piece, effect.player)]--;
        }
      }
    }
  }

  return copyWith(
    board: board,
    hash: hash,
    result: result,
    pieces: pieces,
    hands: hands,
  );
}