// File: fredericmarchand[P]TexasHoldem/Game.cpp
// Function: Game::playRound(Player*, bool)
bool Game::playRound(Player *me, bool firstRound)
{
    Player *last = NULL;
    Player *turn = NULL;

    //If its the first round, the players turn is the player after the big blind
    if (firstRound)
    {
        turn = getNextPlayer(getBigBlindPlayer());
        last = getBigBlindPlayer();
    }
    else //Otherwise its the small blind player who goes first
    {
        turn = getSmallBlindPlayer();
        last = getDealer();
    }

    int raiseCount = 0;
    while (1)
    {

        if (checkLastPlayerRemaining(turn) == true)
        {
            break;
        }

        if (turn->getState().move == FOLD)
        {
            turn = getNextPlayer(turn);
            continue;
        }

        Move move = CHECK;
        if (turn == me && !me->isAI())
        {
            move = getMove();
            me->doMove(move, getPlayerIndex(me), getPreviousPlayer(turn), false);
        }
        else
        {
            NodeState *state = new NodeState(this, turn);
            move = UCTSearch(state, UCT_DEPTH);
            if (move == BET)
            {
                raiseCount++;
            }
            if (raiseCount > 5)
            {
                raiseCount = 0;
                move = CHECK;
            }
            move = turn->doMove(move, getPlayerIndex(turn), getPreviousPlayer(turn), false);
            delete state;
        }

        // Change last player to make a move
        if (move == BET)
        {
            last = getPreviousPlayer(turn);
        }

        // Exit condition
        if (turn == last)
        {
            break;
        }

        turn = getNextPlayer(turn);
    }

    addBetsToPot();
    setPlayerStates();

    for (int i = 0; i < numPlayers; ++i)
    {
        if (checkLastPlayerRemaining(players[i]) == true)
        {
            return true;
        }
    }

    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
testb $0x1, -0x19(%rbp)
je 0x5b70
movq -0x60(%rbp), %rdi
callq 0x4d50
movq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x4eb0
movq -0x60(%rbp), %rdi
movq %rax, -0x30(%rbp)
callq 0x4d50
movq %rax, -0x28(%rbp)
jmp 0x5b8a
movq -0x60(%rbp), %rdi
callq 0x4e10
movq -0x60(%rbp), %rdi
movq %rax, -0x30(%rbp)
callq 0x4cd0
movq %rax, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x5770
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x5bad
jmp 0x5d77
movq -0x30(%rbp), %rdi
callq 0x3690
movq %rax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x5bd3
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x4eb0
movq %rax, -0x30(%rbp)
jmp 0x5b91
movl $0x1, -0x40(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x5c42
movq -0x18(%rbp), %rdi
callq 0x32e0
testb $0x1, %al
jne 0x5c42
movq -0x60(%rbp), %rdi
callq 0x5df0
movq -0x60(%rbp), %rdi
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x18(%rbp), %rsi
callq 0x5000
movq -0x60(%rbp), %rdi
movl %eax, -0x64(%rbp)
movq -0x30(%rbp), %rsi
callq 0x4f50
movq -0x70(%rbp), %rdi
movl -0x68(%rbp), %esi
movl -0x64(%rbp), %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x37a0
jmp 0x5d3e
movl $0x30, %edi
callq 0x2190
movq -0x60(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
movq -0x30(%rbp), %rdx
callq 0x8e70
jmp 0x5c65
movq -0x78(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movl $0x3, %esi
callq 0x5f70
movl %eax, -0x40(%rbp)
cmpl $0x2, -0x40(%rbp)
jne 0x5cae
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5cae
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x30, %esi
callq 0x21a0
jmp 0x5ddf
cmpl $0x5, -0x34(%rbp)
jle 0x5cc2
movl $0x0, -0x34(%rbp)
movl $0x1, -0x40(%rbp)
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0x30(%rbp), %rsi
callq 0x5000
movq -0x60(%rbp), %rdi
movl %eax, -0x84(%rbp)
movq -0x30(%rbp), %rsi
callq 0x4f50
movq -0x90(%rbp), %rdi
movl -0x88(%rbp), %esi
movl -0x84(%rbp), %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x37a0
movl %eax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x5d3c
movq -0x80(%rbp), %rdi
callq 0x94e0
movq -0x80(%rbp), %rdi
movl $0x30, %esi
callq 0x21a0
jmp 0x5d3e
cmpl $0x2, -0x40(%rbp)
jne 0x5d55
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x4f50
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x5d61
jmp 0x5d77
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x4eb0
movq %rax, -0x30(%rbp)
jmp 0x5b91
movq -0x60(%rbp), %rdi
callq 0x62b0
movq -0x60(%rbp), %rdi
callq 0x6310
movl $0x0, -0x58(%rbp)
movq -0x60(%rbp), %rcx
movl -0x58(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x5dcd
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x5770
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x5dc0
movb $0x1, -0x1(%rbp)
jmp 0x5dd1
jmp 0x5dc2
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5d90
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x22a0
nopl (%rax,%rax)