// File: /workspace/github_cpp_makefile_star2_O3/IncredibleHannes[P]QMUL-ChessGame/src/Controller/Controler.cpp
// Function: Controller::run()
void Controller::run() {

  Board *board = new Board();

  View view = View();

  view.printGreeting();

  while (true) {
    if (board->isCheck()) {
      view.printCheck();
      if (board->isCheckmate()) {
        view.printCheckmate();
        view.printBoard(board);
        break;
      }
    }
    if (board->isDraw()) {
      view.printDraw();
      view.printBoard(board);
      break;
    }
    view.printBoard(board);

    bool noInvalidAction = true;
    while (noInvalidAction) {
      UserAction *userAction = view.getUserAction();
      Move *move;
      switch (userAction->getType()) {
        case UserAction::Save :
          view.saveToFile(board->getPreviousMoves());
          noInvalidAction = false;
          break;
        case UserAction::Load :
          delete board;
          board = new Board(view.loadFromFile());
          noInvalidAction = false;
          break;
        case UserAction::Undo :
          board->undoLastMove();
          noInvalidAction = false;
          break;
        case UserAction::MoveAction :
          move = new Move(*userAction->getMove());
          if(!board->applyMove(move)) {
            view.printInvalidMove();
            delete move;
            delete userAction;
            continue;
          }
          if (board->isPromotion(move)) {
            board->applyPromotion(move, view.getPromotionType());
          }
          noInvalidAction = false;
          //delete move;
          break;
        case UserAction::Quit :
          view.printQuitMessage();
          delete board;
          delete userAction;
          return;
        default:
          view.printInvalidUserAction();
      }
      delete userAction;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0x40, %edi
callq 0x2120
movq %rax, %r14
movq %rax, %rdi
callq 0x4184
leaq -0x29(%rbp), %r12
movb $0x0, (%r12)
movq %r12, %rdi
callq 0x615e
leaq -0x60(%rbp), %r15
movq %r14, %rdi
callq 0x4e48
testb %al, %al
je 0x26b7
movq %r12, %rdi
callq 0x62a8
movq %r14, %rdi
callq 0x54dc
testb %al, %al
jne 0x289b
movq %r14, %rdi
callq 0x5628
testb %al, %al
jne 0x288d
movq %r12, %rdi
movq %r14, %rsi
callq 0x5814
movq %r12, %rdi
callq 0x5dca
movq %rax, %r13
movq %rax, %rdi
callq 0x5802
testl %eax, %eax
jne 0x2746
movl $0x20, %edi
callq 0x2120
movq %rax, %rbx
movq %r13, %rdi
callq 0x580a
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4074
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4736
testb %al, %al
jne 0x280d
movq %r12, %rdi
callq 0x5ba0
movq %rbx, %rdi
callq 0x40c0
movq %rbx, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x57dc
movq %r13, %rdi
callq 0x2100
jmp 0x26d2
decl %eax
cmpl $0x3, %eax
ja 0x2801
leaq 0x48ac(%rip), %rcx      # 0x7004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r14, %r14
je 0x276e
movq %r14, %rdi
callq 0x4c5c
movq %r14, %rdi
callq 0x2100
movl $0x40, %edi
callq 0x2120
movq %rax, %r14
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x65c4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x465a
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2831
leaq -0x48(%rbp), %r15
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x27ae
xorl %ebx, %ebx
leaq -0x60(%rbp), %r15
jmp 0x2833
movq %r14, %rdi
callq 0x50b0
jmp 0x2831
movq %r15, %rdi
movq %r14, %rsi
callq 0x4e2c
movq %r12, %rdi
movq %r15, %rsi
callq 0x640a
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x2831
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x27ef
jmp 0x2831
movq %r12, %rdi
callq 0x5bee
movb $0x1, %bl
jmp 0x2833
movq %r14, %rdi
movq %rbx, %rsi
callq 0x567a
testb %al, %al
je 0x2831
movq %r12, %rdi
callq 0x5c8a
movq %r14, %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0x5272
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x57dc
movq %r13, %rdi
callq 0x2100
testb %bl, %bl
jne 0x26d2
jmp 0x2693
leaq -0x29(%rbp), %rdi
callq 0x5c3c
testq %r14, %r14
je 0x2866
movq %r14, %rdi
callq 0x4c5c
movq %r14, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x57dc
movq %r13, %rdi
callq 0x2100
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x29(%rbp), %rbx
movq %rbx, %rdi
callq 0x6380
jmp 0x28a7
leaq -0x29(%rbp), %rbx
movq %rbx, %rdi
callq 0x62f6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5814
jmp 0x287e
movq %rax, %r12
jmp 0x28d9
movq %rax, %r12
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
je 0x28d9
leaq -0x48(%rbp), %r15
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x28c9
movq %r14, %rbx
jmp 0x290a
movq %rax, %r12
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x2912
movq %r15, %r14
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x28ed
jmp 0x2912
movq %rax, %r12
movq %r14, %rdi
jmp 0x290d
movq %rax, %r12
movq %rbx, %rdi
callq 0x2100
movq %r12, %rdi
callq 0x21d0
nop