// File: 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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6a10
movq -0xc0(%rbp), %rdi
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
callq 0x7de0
movq -0xc0(%rbp), %rdi
movq %rax, -0x20(%rbp)
callq 0x81e0
andb $0x1, %al
movb %al, -0x29(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x7a82
movq -0xc0(%rbp), %rsi
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rdx
callq 0x5810
testb $0x1, %al
jne 0x7588
jmp 0x7a82
movq -0x20(%rbp), %rdi
callq 0x5790
movq -0xc0(%rbp), %rcx
andb $0x1, %al
movzbl %al, %eax
movb 0x8(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x7a82
movq -0xc0(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x8410
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rdi
callq 0x58f0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xc1(%rbp)
jne 0x762e
movq -0x18(%rbp), %rdi
callq 0x6a10
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2ef0
movl %eax, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0x50(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2ef0
movl %eax, %ecx
movl -0xc8(%rbp), %eax
subl %ecx, %eax
cmpl $0x2, %eax
sete %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x763d
jmp 0x776d
movl $0x8, %edi
callq 0x2160
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0xd0(%rbp)
jmp 0x7660
movq -0xd0(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2ee0
movl %eax, -0xdc(%rbp)
jmp 0x767c
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0xe8(%rbp)
jmp 0x768e
movq -0xe8(%rbp), %rax
movq %rax, -0x6c(%rbp)
leaq -0x6c(%rbp), %rdi
callq 0x2ef0
movl %eax, -0xec(%rbp)
jmp 0x76aa
movl -0xdc(%rbp), %esi
movq -0xd8(%rbp), %rdi
movl -0xec(%rbp), %edx
incl %edx
callq 0x2e50
jmp 0x76c6
movq -0xd8(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x8, %edi
callq 0x2160
movq %rax, -0x100(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0xf8(%rbp)
jmp 0x76f4
movq -0xf8(%rbp), %rax
movq %rax, -0x74(%rbp)
leaq -0x74(%rbp), %rdi
callq 0x2ee0
movl %eax, -0x104(%rbp)
jmp 0x7710
movl -0x104(%rbp), %esi
movq -0x100(%rbp), %rdi
xorl %edx, %edx
callq 0x2e50
jmp 0x7726
movq -0x100(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x776d
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x2140
jmp 0x7a94
movq -0x100(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x2140
jmp 0x7a94
movq -0x20(%rbp), %rdi
callq 0x58f0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x105(%rbp)
jne 0x77d3
movq -0x18(%rbp), %rdi
callq 0x6a10
movq %rax, -0x7c(%rbp)
leaq -0x7c(%rbp), %rdi
callq 0x2ef0
movl %eax, -0x10c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0x84(%rbp)
leaq -0x84(%rbp), %rdi
callq 0x2ef0
movl %eax, %ecx
movl -0x10c(%rbp), %eax
subl %ecx, %eax
cmpl $-0x2, %eax
sete %al
movb %al, -0x105(%rbp)
movb -0x105(%rbp), %al
testb $0x1, %al
jne 0x77e2
jmp 0x7927
movl $0x8, %edi
callq 0x2160
movq %rax, -0x120(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0x118(%rbp)
jmp 0x7805
movq -0x118(%rbp), %rax
movq %rax, -0x8c(%rbp)
leaq -0x8c(%rbp), %rdi
callq 0x2ee0
movl %eax, -0x124(%rbp)
jmp 0x7827
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0x130(%rbp)
jmp 0x7839
movq -0x130(%rbp), %rax
movq %rax, -0x94(%rbp)
leaq -0x94(%rbp), %rdi
callq 0x2ef0
movl %eax, -0x134(%rbp)
jmp 0x785b
movl -0x124(%rbp), %esi
movq -0x120(%rbp), %rdi
movl -0x134(%rbp), %edx
decl %edx
callq 0x2e50
jmp 0x7877
movq -0x120(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x8, %edi
callq 0x2160
movq %rax, -0x148(%rbp)
movq -0x18(%rbp), %rdi
callq 0x6a30
movq %rax, -0x140(%rbp)
jmp 0x78a5
movq -0x140(%rbp), %rax
movq %rax, -0x9c(%rbp)
leaq -0x9c(%rbp), %rdi
callq 0x2ee0
movl %eax, -0x14c(%rbp)
jmp 0x78c7
movl -0x14c(%rbp), %esi
movq -0x148(%rbp), %rdi
movl $0x7, %edx
callq 0x2e50
jmp 0x78e0
movq -0x148(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x7927
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x2140
jmp 0x7a94
movq -0x148(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x2140
jmp 0x7a94
cmpq $0x0, -0x38(%rbp)
je 0x7a3a
movl $0x20, %edi
callq 0x2160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x158(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
callq 0x68b0
jmp 0x797a
movq -0xc0(%rbp), %rdi
movq -0x158(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rsi
callq 0x8410
movq -0xc0(%rbp), %rdi
addq $0x28, %rdi
callq 0x97a0
movq (%rax), %rdi
movl $0x2, %esi
callq 0x6a50
movq -0xa8(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpq $0x0, %rax
je 0x79e4
movq -0x160(%rbp), %rdi
callq 0x69d0
movq -0x160(%rbp), %rdi
callq 0x2140
movq -0x40(%rbp), %rax
movq %rax, -0x168(%rbp)
cmpq $0x0, %rax
je 0x7a01
movq -0x168(%rbp), %rdi
callq 0x2140
movq -0x38(%rbp), %rax
movq %rax, -0x170(%rbp)
cmpq $0x0, %rax
je 0x7a1e
movq -0x170(%rbp), %rdi
callq 0x2140
jmp 0x7a3a
movq -0x158(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x2140
jmp 0x7a94
testb $0x1, -0x29(%rbp)
je 0x7a70
movq -0xc0(%rbp), %rdi
callq 0x81e0
testb $0x1, %al
jne 0x7a52
jmp 0x7a70
movq -0xc0(%rbp), %rdi
callq 0x8630
movq -0xc0(%rbp), %rdi
callq 0x8670
movb $0x0, -0x1(%rbp)
jmp 0x7a86
movq -0xc0(%rbp), %rdi
callq 0x8630
movb $0x1, -0x1(%rbp)
jmp 0x7a86
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2260
nopl (%rax)