// File: /workspace/github_cpp_makefile_star2_O2/IncredibleHannes[P]QMUL-ChessGame/src/Model/Board.cpp
// Function: Board::applyMove(Move*)
bool Board::applyMove(Move *move) {
  Chessman *currentChessman = getChessman(move->getOrigin());
  bool isCheck = this->isCheck();

  if (currentChessman != nullptr
      && currentChessman->isMoveValid(*this, *move)
      && currentChessman->getColour() == currentColour) {
    Board::move(move);
    // check casteling
    Position *rookTarget = nullptr;
    Position *rookOrigin = nullptr;
    if (currentChessman->getType() == Chessman::FigureType::King
        && move->getOrigin().getY() - move->getTarget().getY() == 2) {
      rookTarget = new Position(move->getTarget().getX(), move->getTarget().getY() + 1);
      rookOrigin = new Position(move->getTarget().getX(), 0);
    }
    if (currentChessman->getType() == Chessman::FigureType::King
        && move->getOrigin().getY() - move->getTarget().getY() == -2) {
      rookTarget = new Position(move->getTarget().getX(), move->getTarget().getY() - 1);
      rookOrigin = new Position(move->getTarget().getX(), 7);
    }
    if (rookTarget != nullptr) {
      Move *m = new Move(*rookOrigin, *rookTarget);
      Board::move(m);
      previousMoves.back()->addType(Move::Casteling);
      delete m;
      delete rookOrigin;
      delete rookTarget;
    }


    //check if after the move the player is still in check
    if (isCheck && this->isCheck()) {
      changeCurrentColour();
      this->undoLastMove();
      return false;
    }

    changeCurrentColour();

    return true;
  }
  return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x4088
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4a86
movq %rax, %r13
movq %rbx, %rdi
callq 0x4c62
testq %r13, %r13
je 0x48be
movl %eax, %r14d
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x37c2
testb %al, %al
je 0x48be
movq %r13, %rdi
callq 0x379a
cmpb %al, 0x8(%rbx)
jne 0x48be
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4d5c
movq %r13, %rdi
callq 0x3830
testl %eax, %eax
jne 0x4781
movq %r12, %rdi
callq 0x4088
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28ae
movl %eax, %r15d
movq %r12, %rdi
callq 0x4092
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28ae
subl %eax, %r15d
cmpl $0x2, %r15d
jne 0x4781
pushq $0x8
popq %rdi
callq 0x2110
movq %rax, -0x30(%rbp)
movq %r12, %rdi
callq 0x4092
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28a6
movl %eax, %r15d
movq %r12, %rdi
callq 0x4092
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28ae
incl %eax
movq -0x30(%rbp), %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x286e
pushq $0x8
popq %rdi
callq 0x2110
movq %rax, %r15
movq %r12, %rdi
callq 0x4092
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28a6
movq %r15, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x286e
jmp 0x478c
xorl %r15d, %r15d
movq $0x0, -0x30(%rbp)
movq %r13, %rdi
callq 0x3830
testl %eax, %eax
jne 0x4843
movq %r12, %rdi
callq 0x4088
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28ae
movl %eax, %r13d
movq %r12, %rdi
callq 0x4092
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28ae
subl %eax, %r13d
cmpl $-0x2, %r13d
jne 0x4843
pushq $0x8
popq %rdi
callq 0x2110
movq %rax, %r13
movq %r12, %rdi
callq 0x4092
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28a6
movl %eax, %r15d
movq %r12, %rdi
callq 0x4092
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28ae
decl %eax
movq %r13, %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x286e
pushq $0x8
popq %rdi
callq 0x2110
movq %rax, %r15
movq %r12, %rdi
callq 0x4092
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x28a6
pushq $0x7
popq %rdx
movq %r15, %rdi
movl %eax, %esi
callq 0x286e
jmp 0x484c
movq -0x30(%rbp), %r13
testq %r13, %r13
je 0x48a1
pushq $0x20
popq %rdi
callq 0x2110
movq %rax, %r12
movq (%r15), %rsi
movq (%r13), %rdx
movq %rax, %rdi
callq 0x3ffc
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4d5c
movq 0x30(%rbx), %rax
movq 0x10(%rax), %rdi
pushq $0x2
popq %rsi
callq 0x409c
movq %r12, %rdi
callq 0x4072
movq %r12, %rdi
callq 0x20f0
movq %r15, %rdi
callq 0x20f0
movq %r13, %rdi
callq 0x20f0
testb %r14b, %r14b
je 0x48cf
movq %rbx, %rdi
callq 0x4c62
testb %al, %al
je 0x48cf
xorb $0x1, 0x8(%rbx)
movq %rbx, %rdi
callq 0x4ea4
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorb $0x1, 0x8(%rbx)
movb $0x1, %al
jmp 0x48c0
jmp 0x48e6
jmp 0x48ec
jmp 0x48dd
movq %rax, %rbx
movq %r15, -0x30(%rbp)
jmp 0x48f8
movq %r13, -0x30(%rbp)
jmp 0x48ec
movq %rax, %rbx
jmp 0x48f8
movq %rax, %rbx
movq %r12, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x21d0
nop