// File: /workspace/github_cpp_makefile_star2_O1/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 0x40ee
leaq -0x38(%rbp), %r14
movq %rax, (%r14)
movq (%rbx), %r15
movq %r14, %rdi
callq 0x2956
cltq
movq (%r15,%rax,8), %r15
movq %r14, %rdi
callq 0x295e
cltq
movq (%r15,%rax,8), %r13
movq %rbx, %rdi
callq 0x4e5e
testq %r13, %r13
je 0x49df
movl %eax, %r14d
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x37da
testb %al, %al
je 0x49df
movq %r13, %rdi
callq 0x37b2
cmpb %al, 0x8(%rbx)
jne 0x49df
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f6e
movq %r13, %rdi
callq 0x3848
testl %eax, %eax
jne 0x4895
movq %r12, %rdi
callq 0x40ee
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x295e
movl %eax, %r15d
movq %r12, %rdi
callq 0x40f8
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x295e
subl %eax, %r15d
cmpl $0x2, %r15d
jne 0x4895
movl $0x8, %edi
callq 0x2120
movq %rax, -0x30(%rbp)
movq %r12, %rdi
callq 0x40f8
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2956
movl %eax, %r15d
movq %r12, %rdi
callq 0x40f8
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x295e
incl %eax
movq -0x30(%rbp), %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x291e
movl $0x8, %edi
callq 0x2120
movq %rax, %r15
movq %r12, %rdi
callq 0x40f8
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2956
movq %r15, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x291e
jmp 0x48a0
xorl %r15d, %r15d
movq $0x0, -0x30(%rbp)
movq %r13, %rdi
callq 0x3848
testl %eax, %eax
je 0x48b5
movq -0x30(%rbp), %r13
jmp 0x4964
movq %r12, %rdi
callq 0x40ee
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x295e
movl %eax, %r13d
movq %r12, %rdi
callq 0x40f8
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x295e
subl %eax, %r13d
cmpl $-0x2, %r13d
movq -0x30(%rbp), %r13
jne 0x4964
movl $0x8, %edi
callq 0x2120
movq %rax, %r13
movq %r12, %rdi
callq 0x40f8
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2956
movl %eax, %r15d
movq %r12, %rdi
callq 0x40f8
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x295e
decl %eax
movq %r13, %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x291e
movl $0x8, %edi
callq 0x2120
movq %rax, %r15
movq %r12, %rdi
callq 0x40f8
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2956
movq %r15, %rdi
movl %eax, %esi
movl $0x7, %edx
callq 0x291e
testq %r13, %r13
je 0x49c2
movl $0x20, %edi
callq 0x2120
movq %rax, %r12
movq (%r15), %rsi
movq (%r13), %rdx
movq %rax, %rdi
callq 0x4058
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f6e
movq 0x30(%rbx), %rax
movq 0x10(%rax), %rdi
movl $0x2, %esi
callq 0x4102
movq %r12, %rdi
callq 0x40d8
movq %r12, %rdi
callq 0x2100
movq %r15, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x2100
testb %r14b, %r14b
je 0x49f0
movq %rbx, %rdi
callq 0x4e5e
testb %al, %al
je 0x49f0
xorb $0x1, 0x8(%rbx)
movq %rbx, %rdi
callq 0x50d8
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 0x49e1
jmp 0x4a07
jmp 0x4a0d
jmp 0x49fe
movq %rax, %rbx
movq %r15, -0x30(%rbp)
jmp 0x4a19
movq %r13, -0x30(%rbp)
jmp 0x4a0d
movq %rax, %rbx
jmp 0x4a19
movq %rax, %rbx
movq %r12, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x21d0