// File: /workspace/github_cpp_makefile_star2_O3/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 0x40d6
leaq -0x38(%rbp), %r14
movq %rax, (%r14)
movq (%rbx), %r15
movq %r14, %rdi
callq 0x2962
cltq
movq (%r15,%rax,8), %r15
movq %r14, %rdi
callq 0x296a
cltq
movq (%r15,%rax,8), %r13
movq %rbx, %rdi
callq 0x4e48
testq %r13, %r13
je 0x49c4
movl %eax, %r14d
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x37d6
testb %al, %al
je 0x49c4
movq %r13, %rdi
callq 0x37ae
cmpb %al, 0x8(%rbx)
jne 0x49c4
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f46
movq %r13, %rdi
callq 0x3844
testl %eax, %eax
jne 0x487d
movq %r12, %rdi
callq 0x40d6
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x296a
movl %eax, %r15d
movq %r12, %rdi
callq 0x40e0
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x296a
subl %eax, %r15d
cmpl $0x2, %r15d
jne 0x487d
movl $0x8, %edi
callq 0x2120
movq %rax, -0x30(%rbp)
movq %r12, %rdi
callq 0x40e0
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2962
movl %eax, %r15d
movq %r12, %rdi
callq 0x40e0
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x296a
incl %eax
movq -0x30(%rbp), %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x292a
movl $0x8, %edi
callq 0x2120
movq %rax, %r15
movq %r12, %rdi
callq 0x40e0
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2962
movq %r15, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x292a
jmp 0x4888
xorl %r15d, %r15d
movq $0x0, -0x30(%rbp)
movq %r13, %rdi
callq 0x3844
testl %eax, %eax
jne 0x4945
movq %r12, %rdi
callq 0x40d6
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x296a
movl %eax, %r13d
movq %r12, %rdi
callq 0x40e0
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x296a
subl %eax, %r13d
cmpl $-0x2, %r13d
jne 0x4945
movl $0x8, %edi
callq 0x2120
movq %rax, %r13
movq %r12, %rdi
callq 0x40e0
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2962
movl %eax, %r15d
movq %r12, %rdi
callq 0x40e0
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
callq 0x296a
decl %eax
movq %r13, %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x292a
movl $0x8, %edi
callq 0x2120
movq %rax, %r15
movq %r12, %rdi
callq 0x40e0
leaq -0x38(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2962
movq %r15, %rdi
movl %eax, %esi
movl $0x7, %edx
callq 0x292a
jmp 0x494e
movq -0x30(%rbp), %r13
testq %r13, %r13
je 0x49a7
movl $0x20, %edi
callq 0x2120
movq %rax, %r12
movq (%r15), %rsi
movq (%r13), %rdx
movq %rax, %rdi
callq 0x4040
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f46
movq 0x30(%rbx), %rax
movq 0x10(%rax), %rdi
movl $0x2, %esi
callq 0x40ea
movq %r12, %rdi
callq 0x40c0
movq %r12, %rdi
callq 0x2100
movq %r15, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x2100
testb %r14b, %r14b
je 0x49d5
movq %rbx, %rdi
callq 0x4e48
testb %al, %al
je 0x49d5
xorb $0x1, 0x8(%rbx)
movq %rbx, %rdi
callq 0x50b0
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 0x49c6
jmp 0x49ec
jmp 0x49f2
jmp 0x49e3
movq %rax, %rbx
movq %r15, -0x30(%rbp)
jmp 0x49fe
movq %r13, -0x30(%rbp)
jmp 0x49f2
movq %rax, %rbx
jmp 0x49fe
movq %rax, %rbx
movq %r12, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x21d0
nop