// File: Thomas1995[P]Rentz/src/table.cpp
// Function: Table::Start()
void Table::Start() {

  std::cout << "\n\nA new game has started!\n\n";

  for(size_t i = 0; i < players.size(); ++i)
    players[i].sendIndex(i);

  // choose players in play

  Card::lowestCard = 15 - players.size() * 2;

  score.resize(TABLE_SIZE, 0);

  // true if a game was already played but a certain connection, false otherwise
  std::vector<std::vector<bool>> gamesPlayed(players.size(), std::vector<bool>(gamesNumber + 1, false));

  for(size_t i=0;i<players.size();++i)
    for(int j=0;j<gamesNumber+1;++j)
      gamesPlayed[i][j] = false;

  // assign the first connection
  firstPlayer = players.begin();

  for(int gameChoice = 1; gameChoice <= gamesNumber; ++gameChoice) {

    for(size_t i=0;i<players.size();++i) {
      // set the first player
      firstPlayer = players.begin() + i;

      std::cout << "Pre-round:\n";
      std::cout << "First player is " << firstPlayer->getName() << '\n';

      // check if connection wants to play NV mode and enforce it for the last choice
      if(gameChoice < gamesNumber) {
        modeNV = players[i].getNVChoice();
      } else {
        modeNV = true;
      }

      // check if modeNV was chosed
      if(modeNV) {
        std::cout << "NV Mode chosen!\n";
      } else {
        std::cout << "NV Mode not chosen!\n";
      }

      // if NV mode not chosen, give cards before game choice
      if(modeNV == false) {
        GiveHands();
        gameType = players[i].getGameChoice();
      } else {
        gameType = players[i].getGameChoice();
        GiveHands();
      }

      // check if connection can play that game
      require(gameType >= 1 && gameType <= gamesNumber,
          players[i].getName() + " has chosen a game index out of bounds\n");

      std::cout << players[i].getName() << " chose to play: " << GameName[gameType] << '\n';

      require(gamesPlayed[i][gameType] == false,
          players[i].getName() + " had already chosen that game\n");

      gamesPlayed[i][gameType] = true;

      // let the other players know the game type
      for(auto &connection : players)
        connection.sendGameChoice(static_cast<uint8_t>(gameType));

      // start the round
      PlayRound();
    }
  }
  std::cout << "Game is over!\n";
  printScores();

  for(auto &connection: players)
    connection.gameEnd();

  GAME_END = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq 0x12157(%rip), %rdi     # 0x20fc8
leaq 0x7711(%rip), %rsi      # 0x16589
callq 0x32d0
movq $0x0, -0x10(%rbp)
movq -0x1e0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0x1e8(%rbp)
addq $0x8, %rdi
callq 0x7d10
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
cmpq %rcx, %rax
jae 0xeedd
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rsi
callq 0x101c0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0xbbc0
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0xee85
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
movq %rdi, -0x200(%rbp)
callq 0x7d10
movq -0x1e0(%rbp), %rdi
addl %eax, %eax
movl $0xf, %ecx
subl %eax, %ecx
leaq 0x123c5(%rip), %rax     # 0x212d0
movl %ecx, (%rax)
addq $0x40, %rdi
movl 0x12489(%rip), %eax     # 0x213a0
movl %eax, %esi
movl $0x0, -0x14(%rbp)
leaq -0x14(%rbp), %rdx
callq 0x101e0
movq -0x200(%rbp), %rdi
callq 0x7d10
movq %rax, -0x1f8(%rbp)
movb $0x0, -0x59(%rbp)
leaq -0x5a(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x10290
movq -0x1f0(%rbp), %rcx
leaq -0x58(%rbp), %rdi
movl $0x8, %esi
leaq -0x59(%rbp), %rdx
callq 0x102b0
jmp 0xef6b
leaq -0x6d(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x10330
movq -0x1f8(%rbp), %rsi
movq -0x208(%rbp), %rcx
leaq -0x30(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x10350
jmp 0xef98
leaq -0x6d(%rbp), %rdi
callq 0x103d0
leaq -0x58(%rbp), %rdi
callq 0x103f0
leaq -0x5a(%rbp), %rdi
callq 0x10410
movq $0x0, -0x78(%rbp)
movq -0x1e0(%rbp), %rdi
movq -0x78(%rbp), %rax
movq %rax, -0x210(%rbp)
addq $0x8, %rdi
callq 0x7d10
movq %rax, %rcx
movq -0x210(%rbp), %rax
cmpq %rcx, %rax
jae 0xf0b6
movl $0x0, -0x7c(%rbp)
cmpl $0x8, -0x7c(%rbp)
jge 0xf0a3
movq -0x78(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x10430
movq %rax, %rdi
movslq -0x7c(%rbp), %rsi
callq 0x10450
movq %rdx, -0x220(%rbp)
movq %rax, -0x218(%rbp)
jmp 0xf023
movq -0x220(%rbp), %rax
movq -0x218(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movq %rax, -0x88(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x104b0
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xeff0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xf084
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x6d(%rbp), %rdi
callq 0x103d0
leaq -0x58(%rbp), %rdi
callq 0x103f0
leaq -0x5a(%rbp), %rdi
callq 0x10410
jmp 0xf808
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xf7ff
jmp 0xf0a5
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0xefbb
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
callq 0x5860
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, -0x98(%rbp)
movq -0x98(%rbp), %rcx
movq %rcx, 0x20(%rax)
movl $0x1, -0x9c(%rbp)
cmpl $0x7, -0x9c(%rbp)
jg 0xf73e
movq $0x0, -0xa8(%rbp)
movq -0x1e0(%rbp), %rdi
movq -0xa8(%rbp), %rax
movq %rax, -0x228(%rbp)
addq $0x8, %rdi
callq 0x7d10
movq %rax, %rcx
movq -0x228(%rbp), %rax
cmpq %rcx, %rax
jae 0xf728
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
callq 0x5860
movq %rax, -0xb8(%rbp)
movq -0xa8(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x9a10
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, -0xb0(%rbp)
movq -0xb0(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq 0x11e46(%rip), %rdi     # 0x20fc8
leaq 0x741c(%rip), %rsi      # 0x165a5
callq 0x32d0
jmp 0xf190
movq 0x11e31(%rip), %rdi     # 0x20fc8
leaq 0x7413(%rip), %rsi      # 0x165b1
callq 0x32d0
movq %rax, -0x230(%rbp)
jmp 0xf1ac
movq -0x1e0(%rbp), %rdi
addq $0x20, %rdi
callq 0x10500
movq %rax, %rsi
leaq -0xd8(%rbp), %rdi
callq 0xb840
jmp 0xf1cd
movq -0x230(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x32b0
movq %rax, -0x238(%rbp)
jmp 0xf1e9
movq -0x238(%rbp), %rdi
movl $0xa, %esi
callq 0x3380
jmp 0xf1fc
leaq -0xd8(%rbp), %rdi
callq 0x31c0
cmpl $0x7, -0x9c(%rbp)
jge 0xf26a
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
movq -0xa8(%rbp), %rsi
callq 0x101c0
movq %rax, %rdi
callq 0xba10
movb %al, -0x239(%rbp)
jmp 0xf238
movq -0x1e0(%rbp), %rax
movb -0x239(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x5c(%rax)
jmp 0xf275
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x31c0
jmp 0xf7ff
movq -0x1e0(%rbp), %rax
movb $0x1, 0x5c(%rax)
movq -0x1e0(%rbp), %rax
testb $0x1, 0x5c(%rax)
je 0xf299
movq 0x11d3f(%rip), %rdi     # 0x20fc8
leaq 0x7332(%rip), %rsi      # 0x165c2
callq 0x32d0
jmp 0xf297
jmp 0xf2b0
movq 0x11d28(%rip), %rdi     # 0x20fc8
leaq 0x732c(%rip), %rsi      # 0x165d3
callq 0x32d0
jmp 0xf2ae
jmp 0xf2b0
movq -0x1e0(%rbp), %rax
movb 0x5c(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xf30e
movq -0x1e0(%rbp), %rdi
callq 0xf820
jmp 0xf2d2
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
movq -0xa8(%rbp), %rsi
callq 0x101c0
movq %rax, %rdi
callq 0xb8c0
movb %al, -0x23a(%rbp)
jmp 0xf2f9
movq -0x1e0(%rbp), %rax
movb -0x23a(%rbp), %cl
movzbl %cl, %ecx
movl %ecx, 0x58(%rax)
jmp 0xf351
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
movq -0xa8(%rbp), %rsi
callq 0x101c0
movq %rax, %rdi
callq 0xb8c0
movb %al, -0x23b(%rbp)
jmp 0xf335
movq -0x1e0(%rbp), %rdi
movb -0x23b(%rbp), %al
movzbl %al, %eax
movl %eax, 0x58(%rdi)
callq 0xf820
jmp 0xf34f
jmp 0xf351
movq -0x1e0(%rbp), %rcx
xorl %eax, %eax
cmpl $0x1, 0x58(%rcx)
movb %al, -0x23c(%rbp)
jl 0xf37a
movq -0x1e0(%rbp), %rax
cmpl $0x7, 0x58(%rax)
setle %al
movb %al, -0x23c(%rbp)
movq -0x1e0(%rbp), %rdi
movb -0x23c(%rbp), %al
movb %al, -0x23d(%rbp)
addq $0x8, %rdi
movq -0xa8(%rbp), %rsi
callq 0x101c0
movq %rax, %rsi
leaq -0x118(%rbp), %rdi
callq 0xb840
jmp 0xf3ae
leaq 0x7233(%rip), %rdx      # 0x165e8
leaq -0xf8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x10520
jmp 0xf3ca
movb -0x23d(%rbp), %al
movzbl %al, %edi
andl $0x1, %edi
leaq -0xf8(%rbp), %rsi
callq 0x14a60
jmp 0xf3e4
leaq -0xf8(%rbp), %rdi
callq 0x31c0
leaq -0x118(%rbp), %rdi
callq 0x31c0
movq -0x1e0(%rbp), %rdi
addq $0x8, %rdi
movq -0xa8(%rbp), %rsi
callq 0x101c0
movq %rax, %rsi
leaq -0x138(%rbp), %rdi
callq 0xb840
jmp 0xf424
movq 0x11b9d(%rip), %rdi     # 0x20fc8
leaq -0x138(%rbp), %rsi
callq 0x32b0
movq %rax, -0x248(%rbp)
jmp 0xf440
movq -0x248(%rbp), %rdi
leaq 0x71c2(%rip), %rsi      # 0x16610
callq 0x32d0
movq %rax, -0x250(%rbp)
jmp 0xf45c
movq -0x1e0(%rbp), %rax
movslq 0x58(%rax), %rsi
leaq 0x11f1a(%rip), %rdi     # 0x21388
callq 0x10570
movq -0x250(%rbp), %rdi
movq %rax, %rsi
callq 0x32b0
movq %rax, -0x258(%rbp)
jmp 0xf48b
movq -0x258(%rbp), %rdi
movl $0xa, %esi
callq 0x3380
jmp 0xf49e
leaq -0x138(%rbp), %rdi
callq 0x31c0
movq -0xa8(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x10430
movq %rax, %rdi
movq -0x1e0(%rbp), %rax
movslq 0x58(%rax), %rsi
callq 0x10450
movq %rdx, -0x268(%rbp)
movq %rax, -0x260(%rbp)
jmp 0xf4dd
movq -0x268(%rbp), %rax
movq -0x260(%rbp), %rcx
movq %rcx, -0x148(%rbp)
movq %rax, -0x140(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x10590
movq -0x1e0(%rbp), %rdi
xorb $0x1, %al
movb %al, -0x269(%rbp)
addq $0x8, %rdi
movq -0xa8(%rbp), %rsi
callq 0x101c0
movq %rax, %rsi
leaq -0x188(%rbp), %rdi
callq 0xb840
jmp 0xf535
leaq 0x70e5(%rip), %rdx      # 0x16621
leaq -0x168(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x10520
jmp 0xf551
movb -0x269(%rbp), %al
movzbl %al, %edi
andl $0x1, %edi
leaq -0x168(%rbp), %rsi
callq 0x14a60
jmp 0xf56b
leaq -0x168(%rbp), %rdi
callq 0x31c0
leaq -0x188(%rbp), %rdi
callq 0x31c0
movq -0xa8(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x10430
movq %rax, %rdi
movq -0x1e0(%rbp), %rax
movslq 0x58(%rax), %rsi
callq 0x10450
movq %rdx, -0x280(%rbp)
movq %rax, -0x278(%rbp)
jmp 0xf5b6
movq -0x280(%rbp), %rax
movq -0x278(%rbp), %rcx
movq %rcx, -0x198(%rbp)
movq %rax, -0x190(%rbp)
leaq -0x198(%rbp), %rdi
movl $0x1, %esi
callq 0x104b0
movq -0x1e0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rdi
callq 0x5860
movq %rax, -0x1a8(%rbp)
movq -0x1a0(%rbp), %rdi
callq 0x7fd0
movq %rax, -0x1b0(%rbp)
leaq -0x1a8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x9810
testb $0x1, %al
jne 0xf637
jmp 0xf701
leaq -0x1a8(%rbp), %rdi
callq 0x9850
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rdi
movzbl 0x58(%rax), %esi
callq 0xb980
jmp 0xf666
jmp 0xf668
leaq -0x1a8(%rbp), %rdi
callq 0x9870
jmp 0xf61b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xf69c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x31c0
leaq -0x118(%rbp), %rdi
callq 0x31c0
jmp 0xf7ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x31c0
jmp 0xf7ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xf6f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x31c0
leaq -0x188(%rbp), %rdi
callq 0x31c0
jmp 0xf7ff
movq -0x1e0(%rbp), %rdi
callq 0xf9f0
jmp 0xf70f
jmp 0xf711
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0xf104
jmp 0xf72a
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0xf0ec
movq 0x11883(%rip), %rdi     # 0x20fc8
leaq 0x6ef4(%rip), %rsi      # 0x16640
callq 0x32d0
jmp 0xf753
movq -0x1e0(%rbp), %rdi
callq 0xfc30
jmp 0xf761
movq -0x1e0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rdi
callq 0x5860
movq %rax, -0x1c8(%rbp)
movq -0x1c0(%rbp), %rdi
callq 0x7fd0
movq %rax, -0x1d0(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x9810
testb $0x1, %al
jne 0xf7b2
jmp 0xf7e3
leaq -0x1c8(%rbp), %rdi
callq 0x9850
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rdi
callq 0xb7c0
jmp 0xf7d3
jmp 0xf7d5
leaq -0x1c8(%rbp), %rdi
callq 0x9870
jmp 0xf799
movq -0x1e0(%rbp), %rax
movb $0x1, (%rax)
leaq -0x30(%rbp), %rdi
callq 0x105c0
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x105c0
movq -0x68(%rbp), %rdi
callq 0x34d0
nopw %cs:(%rax,%rax)