/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "start.h"

void pokerStartInit(poker_t *poker) {
  uint8_t i, indexDealer, indexSmallBlind, indexBigBlind;
  bool foundDealer, foundSmallBlind;
  pokerplayer_t *player;

  poker->round = POKER_ROUND_START;

  // Prepare the initial game state
  poker->round = POKER_ROUND_DEAL;
  pokerBetReset(&poker->bet);
  pokerDealerInit(&poker->dealer);

  // Reset the players
  for(i = 0; i < POKER_PLAYER_COUNT; i++) {
    pokerPlayerReset(poker->players + i);
  }

  // Decide on the dealer
  poker->roundDealer = (poker->roundDealer+1) % POKER_PLAYER_COUNT;

  // Find the players.
  i = poker->roundDealer;
  foundDealer = false;
  foundSmallBlind = false;
  while(true) {
    player = poker->players + i;
    if(!pokerPlayerIsAlive(player)) continue;
    if(!foundDealer) {
      indexDealer = i;
      foundDealer = true;
    } else if(!foundSmallBlind) {
      indexSmallBlind = i;
      foundSmallBlind = true;
    } else {
      indexBigBlind = i;
      break;
    }
    i = (i + 1) % POKER_PLAYER_COUNT;
  }
  
  // Update players for the round.
  poker->roundDealer = indexDealer;
  poker->roundBigBlind = indexBigBlind;
  poker->roundSmallBlind = indexSmallBlind;

  printf("Round Start\n");
  pokerBlindsInit(poker);
}