// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x40
popq %rdi
callq 0x2110
movq %rax, %rbx
movq %rax, %rdi
callq 0x4138
leaq -0x29(%rbp), %r14
movb $0x0, (%r14)
movq %r14, %rdi
callq 0x5e94
leaq -0x48(%rbp), %r15
movq %rbx, %rdi
callq 0x4c62
testb %al, %al
je 0x2654
movq %r14, %rdi
callq 0x5f02
movq %rbx, %rdi
callq 0x525e
testb %al, %al
jne 0x27e1
movq %rbx, %rdi
callq 0x53a0
testb %al, %al
jne 0x27d3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x58c8
movb $0x1, %r13b
testb $0x1, %r13b
je 0x2630
movq %r14, %rdi
callq 0x5bce
movq %rax, %r12
movq %rax, %rdi
callq 0x58b6
cmpl $0x4, %eax
ja 0x2769
movl %eax, %eax
leaq 0x4967(%rip), %rcx      # 0x7004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
pushq $0x20
popq %rdi
callq 0x2110
movq %rax, %r13
movq %r12, %rdi
callq 0x58be
movq %r13, %rdi
movq %rax, %rsi
callq 0x4028
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4656
testb %al, %al
je 0x2773
movq %rbx, %rdi
movq %r13, %rsi
callq 0x53f2
testb %al, %al
je 0x2764
movq %r14, %rdi
callq 0x5af0
movq %rbx, %rdi
movq %r13, %rsi
movl %eax, %edx
callq 0x5042
jmp 0x2764
leaq -0x60(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4c46
movq %r14, %rdi
movq %r13, %rsi
callq 0x5fa6
jmp 0x275c
movq %rbx, %rdi
callq 0x4ea4
jmp 0x2764
movq %r15, %r13
testq %rbx, %rbx
je 0x2733
movq %rbx, %rdi
callq 0x4ac0
movq %rbx, %rdi
callq 0x20f0
pushq $0x40
popq %rdi
callq 0x2110
movq %rax, %rbx
movq %r13, %rdi
movq %r14, %rsi
callq 0x60f6
movq %rbx, %rdi
movq %r13, %rsi
callq 0x45a8
movq %r13, %rdi
callq 0x2840
xorl %r13d, %r13d
jmp 0x278e
movq %r14, %rdi
callq 0x5ab0
jmp 0x278b
movq %r14, %rdi
callq 0x5a90
movq %r13, %rdi
callq 0x4072
movq %r13, %rdi
callq 0x20f0
movb $0x1, %r13b
movq %r12, %rdi
callq 0x5890
movq %r12, %rdi
callq 0x20f0
jmp 0x2672
leaq -0x29(%rbp), %rdi
callq 0x5ad0
testq %rbx, %rbx
je 0x27b9
movq %rbx, %rdi
callq 0x4ac0
movq %rbx, %rdi
callq 0x20f0
movq %r12, %rdi
callq 0x5890
movq %r12, %rdi
callq 0x20f0
jmp 0x27f8
leaq -0x29(%rbp), %r14
movq %r14, %rdi
callq 0x5f64
jmp 0x27ed
leaq -0x29(%rbp), %r14
movq %r14, %rdi
callq 0x5f22
movq %r14, %rdi
movq %rbx, %rsi
callq 0x58c8
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2817
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x2840
jmp 0x2838
movq %rax, %r14
jmp 0x2828
movq %rax, %r14
leaq -0x48(%rbp), %rdi
callq 0x2840
movq %rbx, %rdi
jmp 0x2833
movq %rax, %r14
movq %r13, %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x21d0