// File: /workspace/github_cpp_makefile_star2_O2/Dalgona[P]mine/game.cpp
// Function: game::start()
void game::start()
{
  // Initialize ncurses mode
  if (!scr->enabled) return;

  initColors();

  clear();
  updateDisplay();

  std::srand(std::time(nullptr));

  // Main game loop
  while (true)
  {
    int key = getch();
    int result = 0;

    switch (key)
    {
    case KEY_LEFT:  case 'h': cX = cX == 0 ? cols - 1 : cX - 1; break;
    case KEY_DOWN:  case 'j': cY = cY == rows - 1 ? 0 : cY + 1; break;
    case KEY_UP:    case 'k': cY = cY == 0 ? rows - 1 : cY - 1; break;
    case KEY_RIGHT: case 'l': cX = cX == cols - 1 ? 0 : cX + 1; break;

    case 'f': setFlag();             break;
    case 'q': setQuestionMark();     break;
    case 's': result = step(cY, cX); break;

    case KEY_RESIZE:
      scr->updateMaxYX();
      clear();
      break;
    }

    // Game Over
    if (result == 0xFF)
    {
      for (auto &i : field) if (i & MF_MINE) i = MF_DISP_MINE;
      updateDisplay();
      scr->with_color(2, [&]() {
        scr->mvprintw(-3, 0, "GAME OVER! You stepped on a mine!");
      });
      this->result.win = false;
      break;
    }
    // Game Cleared
    if (!newGame && nMines == nFlags)
    {
      clearValidate = 0;
      for (const auto &i : field)
        if (i & MF_MINE && i & MF_DISP_FLAG) clearValidate++;
      if (clearValidate == nMines)
      {
        for (auto &i : field) if (i & MF_MINE) i = MF_DISP_MINE_CLR;
        updateDisplay();
        scr->with_color(3, [&]() {
          scr->mvprintw(-3, 0, "CONGRATULATIONS! You've found all mine!");
        });
        this->result.win = true;
        break;
      }
    }

    updateDisplay();
  }

  tEnd = steady_clock::now();
  int elapsed = this->result.time =  (int)duration<double, std::centi>(tEnd - tBegin).count();
  scr->mvprintw(-2, 0, "Elapsed time: %04d.%02d seconds\n", elapsed / 100, elapsed % 100);
  clrtoeol();
  {
    scr->mvprintw(-1, 0, "Press ENTER to exit...");
    int key;
    while (true)
    {
      key = getch();
      if (key == KEY_ENTER || key == '\n') break;
    }
  }

  clear();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq (%rdi), %rax
cmpb $0x1, (%rax)
jne 0x4bc5
movq %rdi, %rbx
callq 0x4bf4
callq 0x23c0
movq %rbx, %rdi
callq 0x4d3a
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x21b0
movl %eax, %edi
callq 0x21f0
movq 0x5663(%rip), %r15      # 0x9fd8
leaq 0x1ae4(%rip), %r12      # 0x6460
leaq 0x1aa5(%rip), %r13      # 0x6428
movq (%r15), %rdi
callq 0x20f0
leal -0x66(%rax), %ecx
cmpl $0xd, %ecx
ja 0x49aa
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x4f4a
jmp 0x4a45
leal -0x102(%rax), %ecx
cmpl $0x3, %ecx
ja 0x4a0c
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movl 0x20(%rbx), %eax
movl 0x28(%rbx), %ecx
decl %eax
leal 0x1(%rcx), %edx
cmpl %eax, %ecx
cmovel %r14d, %edx
movl %edx, 0x28(%rbx)
jmp 0x4a45
movl 0x24(%rbx), %eax
movl 0x2c(%rbx), %ecx
decl %eax
leal 0x1(%rcx), %edx
cmpl %eax, %ecx
cmovel %r14d, %edx
movl %edx, 0x2c(%rbx)
jmp 0x4a45
movl 0x2c(%rbx), %eax
testl %eax, %eax
jne 0x49f4
movl 0x24(%rbx), %eax
decl %eax
movl %eax, 0x2c(%rbx)
jmp 0x4a45
movl 0x28(%rbx), %eax
testl %eax, %eax
jne 0x4a05
movl 0x20(%rbx), %eax
decl %eax
movl %eax, 0x28(%rbx)
jmp 0x4a45
cmpl $0x19a, %eax            # imm = 0x19A
jne 0x4a45
movq (%rbx), %rdi
callq 0x5c6c
callq 0x23c0
jmp 0x4a45
movq %rbx, %rdi
callq 0x4fa2
jmp 0x4a45
movl 0x28(%rbx), %esi
movl 0x2c(%rbx), %edx
movq %rbx, %rdi
callq 0x5000
cmpl $0xff, %eax
je 0x4ae6
cmpb $0x0, 0x3c(%rbx)
jne 0x4a87
movl 0x30(%rbx), %edx
cmpl 0x34(%rbx), %edx
jne 0x4a87
andl $0x0, 0x38(%rbx)
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
xorl %esi, %esi
movq %rax, %rdi
cmpq %rcx, %rdi
je 0x4a83
movl (%rdi), %r8d
notl %r8d
testl $0x40001, %r8d          # imm = 0x40001
jne 0x4a7d
incl %esi
movl %esi, 0x38(%rbx)
addq $0x4, %rdi
jmp 0x4a64
cmpl %edx, %esi
je 0x4aa3
movq %rbx, %rdi
callq 0x4d3a
jmp 0x4983
testb $0x1, (%rax)
je 0x4a9f
movl $0xe00000, (%rax)       # imm = 0xE00000
addq $0x4, %rax
cmpq %rcx, %rax
jne 0x4a94
movq %rbx, %rdi
callq 0x4d3a
movq (%rbx), %rdi
leaq 0x8(%rsp), %r14
andq $0x0, 0x8(%r14)
movq %rbx, (%r14)
leaq 0x7fd(%rip), %rax       # 0x52c4
movq %rax, 0x18(%r14)
leaq 0x80c(%rip), %rax       # 0x52de
movq %rax, 0x10(%r14)
movb $0x1, %bpl
pushq $0x3
popq %rsi
movq %r14, %rdx
callq 0x5e02
jmp 0x4b3f
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
cmpq %rcx, %rax
je 0x4b04
testb $0x1, (%rax)
je 0x4afe
movl $0x100000, (%rax)       # imm = 0x100000
addq $0x4, %rax
jmp 0x4aee
movq %rbx, %rdi
callq 0x4d3a
movq (%rbx), %rdi
leaq 0x28(%rsp), %r14
andq $0x0, 0x8(%r14)
movq %rbx, (%r14)
leaq 0x761(%rip), %rax       # 0x5284
movq %rax, 0x18(%r14)
leaq 0x770(%rip), %rax       # 0x529e
movq %rax, 0x10(%r14)
pushq $0x2
popq %rsi
movq %r14, %rdx
callq 0x5e02
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x4200
movb %bpl, 0x50(%rbx)
callq 0x22f0
movq %rax, 0x48(%rbx)
subq 0x40(%rbx), %rax
cvtsi2sd %rax, %xmm0
divsd 0x18b3(%rip), %xmm0     # 0x6418
cvttsd2si %xmm0, %eax
movl %eax, 0x54(%rbx)
movq (%rbx), %rdi
pushq $0x64
popq %rcx
cltd
idivl %ecx
movl %edx, %r9d
leaq 0x1991(%rip), %rcx      # 0x6510
pushq $-0x2
popq %rsi
xorl %edx, %edx
movl %eax, %r8d
xorl %eax, %eax
callq 0x5d4c
movq (%r15), %rdi
callq 0x2240
movq (%rbx), %rdi
leaq 0x1991(%rip), %rcx      # 0x6531
pushq $-0x1
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x5d4c
movq (%r15), %rdi
callq 0x20f0
cmpl $0x157, %eax            # imm = 0x157
je 0x4bc0
cmpl $0xa, %eax
jne 0x4bac
callq 0x23c0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x4be6
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4200
movq %rbx, %rdi
callq 0x23d0
nop