// File: /workspace/github_cpp_makefile_star2_O1/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 0x5252
movq %rdi, %rbx
callq 0x52b0
callq 0x23b0
movq %rbx, %rdi
callq 0x5422
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x21c0
movl %eax, %edi
callq 0x2200
movq 0x6029(%rip), %r14      # 0xafd8
leaq 0x2522(%rip), %r12      # 0x74d8
leaq 0x24e3(%rip), %r13      # 0x74a0
movq (%r14), %rdi
callq 0x20f0
leal -0x66(%rax), %ecx
cmpl $0xd, %ecx
ja 0x4fe4
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x56ca
jmp 0x507f
leal -0x102(%rax), %ecx
cmpl $0x3, %ecx
ja 0x5046
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 0x507f
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 0x507f
movl 0x2c(%rbx), %eax
testl %eax, %eax
jne 0x502e
movl 0x24(%rbx), %eax
decl %eax
movl %eax, 0x2c(%rbx)
jmp 0x507f
movl 0x28(%rbx), %eax
testl %eax, %eax
jne 0x503f
movl 0x20(%rbx), %eax
decl %eax
movl %eax, 0x28(%rbx)
jmp 0x507f
cmpl $0x19a, %eax            # imm = 0x19A
jne 0x507f
movq (%rbx), %rdi
callq 0x6514
callq 0x23b0
jmp 0x507f
movq %rbx, %rdi
callq 0x5726
jmp 0x507f
movl 0x28(%rbx), %esi
movl 0x2c(%rbx), %edx
movq %rbx, %rdi
callq 0x5784
cmpl $0xff, %eax
je 0x5144
cmpb $0x0, 0x3c(%rbx)
jne 0x50c8
movl 0x30(%rbx), %edx
cmpl 0x34(%rbx), %edx
jne 0x50c8
movl $0x0, 0x38(%rbx)
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
cmpq %rcx, %rax
je 0x50c3
xorl %esi, %esi
movq %rax, %rdi
movl (%rdi), %r8d
notl %r8d
testl $0x40001, %r8d          # imm = 0x40001
jne 0x50ba
incl %esi
movl %esi, 0x38(%rbx)
addq $0x4, %rdi
cmpq %rcx, %rdi
jne 0x50a6
cmpl %edx, 0x38(%rbx)
je 0x50e4
movq %rbx, %rdi
callq 0x5422
jmp 0x4fbd
testb $0x1, (%rax)
je 0x50e0
movl $0xe00000, (%rax)       # imm = 0xE00000
addq $0x4, %rax
cmpq %rcx, %rax
jne 0x50d5
movq %rbx, %rdi
callq 0x5422
movq (%rbx), %rdi
xorps %xmm0, %xmm0
movq %rsp, %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movq %rbx, (%rdx)
leaq 0x951(%rip), %rax       # 0x5a5c
movq %rax, 0x18(%rdx)
leaq 0x962(%rip), %rax       # 0x5a78
movq %rax, 0x10(%rdx)
movl $0x3, %esi
callq 0x66a8
movq 0x10(%rsp), %rax
movb $0x1, %bpl
testq %rax, %rax
je 0x51ba
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x51ba
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
cmpq %rcx, %rax
je 0x5162
testb $0x1, (%rax)
je 0x515c
movl $0x100000, (%rax)       # imm = 0x100000
addq $0x4, %rax
jmp 0x514c
movq %rbx, %rdi
callq 0x5422
movq (%rbx), %rdi
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movq %rbx, (%rdx)
leaq 0x894(%rip), %rax       # 0x5a1a
movq %rax, 0x18(%rdx)
leaq 0x8a5(%rip), %rax       # 0x5a36
movq %rax, 0x10(%rdx)
movl $0x2, %esi
callq 0x66a8
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x51b8
leaq 0x20(%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
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x22b5(%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 0x2361(%rip), %rcx      # 0x756c
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
xorl %edx, %edx
xorl %eax, %eax
callq 0x65f2
movq (%r14), %rdi
callq 0x2250
movq (%rbx), %rdi
leaq 0x2362(%rip), %rcx      # 0x758d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
xorl %eax, %eax
callq 0x65f2
movq (%r14), %rdi
callq 0x20f0
cmpl $0x157, %eax            # imm = 0x157
je 0x524d
cmpl $0xa, %eax
jne 0x5239
callq 0x23b0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x52a7
movq %rax, %rbx
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x529f
leaq 0x20(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x529f
jmp 0x52a7
jmp 0x52a7
movq %rax, %rbx
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x529f
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0x23c0
movq %rax, %rdi
callq 0x4739
nop