// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rax, %rdi
callq 0x419c
leaq -0x29(%rbp), %rdi
movb $0x0, (%rdi)
callq 0x61a2
leaq -0x60(%rbp), %r15
movq %r12, %rdi
callq 0x4e5e
testb %al, %al
je 0x26b3
leaq -0x29(%rbp), %rdi
callq 0x62ec
movq %r12, %rdi
callq 0x5504
testb %al, %al
jne 0x2887
movq %r12, %rdi
callq 0x5648
leaq -0x29(%rbp), %rdi
testb %al, %al
jne 0x2880
movq %r12, %rsi
callq 0x584c
movb $0x1, %r14b
leaq -0x29(%rbp), %rdi
callq 0x5e04
movq %rax, %r13
movq %rax, %rdi
callq 0x5838
cmpl $0x4, %eax
ja 0x2828
movl %eax, %eax
leaq 0x490c(%rip), %rcx      # 0x7004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x20, %edi
callq 0x2120
movq %rax, %rbx
movq %r13, %rdi
callq 0x5840
movq %rbx, %rdi
movq %rax, %rsi
callq 0x408c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x474e
testb %al, %al
je 0x2854
movq %r12, %rdi
movq %rbx, %rsi
callq 0x56a8
xorl %r14d, %r14d
testb %al, %al
je 0x2831
leaq -0x29(%rbp), %rdi
callq 0x5cc4
movq %r12, %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0x529a
jmp 0x2823
movq %r12, %rdi
callq 0x50d8
jmp 0x2823
movq %r15, %rdi
movq %r12, %rsi
callq 0x4e42
leaq -0x29(%rbp), %rdi
movq %r15, %rsi
callq 0x644e
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x2823
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x2796
jmp 0x2823
leaq -0x29(%rbp), %rdi
callq 0x5c76
movl $0x1, %ebx
testq %r12, %r12
je 0x2833
movq %r12, %rdi
callq 0x4c76
movq %r12, %rdi
callq 0x2100
jmp 0x2833
testq %r12, %r12
je 0x27e2
movq %r12, %rdi
callq 0x4c76
movq %r12, %rdi
callq 0x2100
movl $0x40, %edi
callq 0x2120
movq %rax, %r12
leaq -0x48(%rbp), %r14
movq %r14, %rdi
leaq -0x29(%rbp), %rsi
callq 0x6608
movq %r12, %rdi
movq %r14, %rsi
callq 0x4672
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x2823
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x2813
xorl %r14d, %r14d
jmp 0x2831
leaq -0x29(%rbp), %rdi
callq 0x5c28
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x580c
movq %r13, %rdi
callq 0x2100
testb $0x3, %bl
jne 0x2876
testb $0x1, %r14b
jne 0x26d2
jmp 0x2874
leaq -0x29(%rbp), %rdi
callq 0x5bda
movq %rbx, %rdi
callq 0x40d8
movq %rbx, %rdi
callq 0x2100
movl $0x4, %ebx
jmp 0x2833
xorl %ebx, %ebx
testl %ebx, %ebx
je 0x268e
jmp 0x289c
callq 0x63c4
jmp 0x2890
leaq -0x29(%rbp), %rdi
callq 0x633a
leaq -0x29(%rbp), %rdi
movq %r12, %rsi
callq 0x584c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r13
movq %r12, %rdi
jmp 0x2901
movq %rax, %r13
jmp 0x28d8
movq %rax, %r13
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x28d8
leaq -0x48(%rbp), %r14
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x28c8
movq %r12, %rbx
jmp 0x28fe
movq %rax, %r13
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x2906
movq (%rdi), %rbx
callq 0x2100
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x28e9
jmp 0x2906
movq %rax, %r13
movq %rbx, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x21d0
nop