// File: /workspace/github_cpp_makefile_star2_O3/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 0x5220
movq %rdi, %rbx
callq 0x5280
callq 0x23b0
movq %rbx, %rdi
callq 0x53f2
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x21c0
movl %eax, %edi
callq 0x2200
movq 0x605d(%rip), %r14      # 0xafd8
leaq 0x2556(%rip), %r12      # 0x74d8
leaq 0x2517(%rip), %r13      # 0x74a0
movq (%r14), %rdi
callq 0x20f0
leal -0x66(%rax), %ecx
cmpl $0xd, %ecx
ja 0x4fb0
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x5692
jmp 0x504b
leal -0x102(%rax), %ecx
cmpl $0x3, %ecx
ja 0x5012
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 %r15d, %edx
movl %edx, 0x28(%rbx)
jmp 0x504b
movl 0x24(%rbx), %eax
movl 0x2c(%rbx), %ecx
decl %eax
leal 0x1(%rcx), %edx
cmpl %eax, %ecx
cmovel %r15d, %edx
movl %edx, 0x2c(%rbx)
jmp 0x504b
movl 0x2c(%rbx), %eax
testl %eax, %eax
jne 0x4ffa
movl 0x24(%rbx), %eax
decl %eax
movl %eax, 0x2c(%rbx)
jmp 0x504b
movl 0x28(%rbx), %eax
testl %eax, %eax
jne 0x500b
movl 0x20(%rbx), %eax
decl %eax
movl %eax, 0x28(%rbx)
jmp 0x504b
cmpl $0x19a, %eax            # imm = 0x19A
jne 0x504b
movq (%rbx), %rdi
callq 0x649a
callq 0x23b0
jmp 0x504b
movq %rbx, %rdi
callq 0x56ee
jmp 0x504b
movl 0x28(%rbx), %esi
movl 0x2c(%rbx), %edx
movq %rbx, %rdi
callq 0x574c
cmpl $0xff, %eax
je 0x5117
cmpb $0x0, 0x3c(%rbx)
jne 0x5099
movl 0x30(%rbx), %edx
cmpl 0x34(%rbx), %edx
jne 0x5099
movl $0x0, 0x38(%rbx)
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
cmpq %rcx, %rax
je 0x5095
xorl %esi, %esi
movq %rax, %rdi
movl (%rdi), %r8d
notl %r8d
testl $0x40001, %r8d          # imm = 0x40001
jne 0x5086
incl %esi
movl %esi, 0x38(%rbx)
addq $0x4, %rdi
cmpq %rcx, %rdi
jne 0x5072
cmpl %edx, %esi
jne 0x5099
jmp 0x50b5
testl %edx, %edx
je 0x50ba
movq %rbx, %rdi
callq 0x53f2
jmp 0x4f89
testb $0x1, (%rax)
je 0x50b1
movl $0xe00000, (%rax)       # imm = 0xE00000
addq $0x4, %rax
cmpq %rcx, %rax
jne 0x50a6
movq %rbx, %rdi
callq 0x53f2
movq (%rbx), %rdi
leaq 0x8(%rsp), %rdx
movq $0x0, 0x8(%rdx)
movq %rbx, (%rdx)
leaq 0x948(%rip), %rax       # 0x5a24
movq %rax, 0x18(%rdx)
leaq 0x959(%rip), %rax       # 0x5a40
movq %rax, 0x10(%rdx)
movl $0x3, %esi
callq 0x6630
movq 0x18(%rsp), %rax
movb $0x1, %bpl
testq %rax, %rax
je 0x518b
leaq 0x8(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x518b
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
cmpq %rcx, %rax
je 0x5135
testb $0x1, (%rax)
je 0x512f
movl $0x100000, (%rax)       # imm = 0x100000
addq $0x4, %rax
jmp 0x511f
movq %rbx, %rdi
callq 0x53f2
movq (%rbx), %rdi
leaq 0x28(%rsp), %rdx
movq $0x0, 0x8(%rdx)
movq %rbx, (%rdx)
leaq 0x88b(%rip), %rax       # 0x59e2
movq %rax, 0x18(%rdx)
leaq 0x89c(%rip), %rax       # 0x59fe
movq %rax, 0x10(%rdx)
movl $0x2, %esi
callq 0x6630
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x5189
leaq 0x28(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorl %ebp, %ebp
movb %bpl, 0x50(%rbx)
callq 0x22f0
movq %rax, 0x48(%rbx)
subq 0x40(%rbx), %rax
cvtsi2sd %rax, %xmm0
divsd 0x22e7(%rip), %xmm0     # 0x7490
cvttsd2si %xmm0, %eax
movl %eax, 0x54(%rbx)
movq (%rbx), %rdi
movslq %eax, %r9
imulq $0x51eb851f, %r9, %r8   # imm = 0x51EB851F
movq %r8, %rax
shrq $0x3f, %rax
sarq $0x25, %r8
addl %eax, %r8d
imull $0x64, %r8d, %eax
subl %eax, %r9d
leaq 0x2393(%rip), %rcx      # 0x756c
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
xorl %edx, %edx
xorl %eax, %eax
callq 0x657a
movq (%r14), %rdi
callq 0x2250
movq (%rbx), %rdi
leaq 0x2394(%rip), %rcx      # 0x758d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
xorl %eax, %eax
callq 0x657a
movq (%r14), %rdi
callq 0x20f0
cmpl $0x157, %eax            # imm = 0x157
je 0x521b
cmpl $0xa, %eax
jne 0x5207
callq 0x23b0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5277
movq %rax, %rbx
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x526f
leaq 0x28(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x526f
jmp 0x5277
jmp 0x5277
movq %rax, %rbx
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x526f
leaq 0x8(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0x23c0
movq %rax, %rdi
callq 0x4729
nop