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

void pokerDealInit(poker_t *poker) {
  uint8_t x, y;
  card_t temporary;

  // Prepare the initial game state
  poker->round = POKER_ROUND_DEAL;
  poker->pot = 0;
  poker->roundBet = 0;
  poker->graveSize = 0;
  poker->cardsFacing = 0;
  poker->deckSize = CARD_DECK_SIZE;
  for(x = 0; x < CARD_DECK_SIZE; x++) poker->deck[x] = x;

  // Reset the players
  for(x = 0; x < POKER_PLAYER_COUNT; x++) {
    poker->players[x].cardCount = 0;
    poker->players[x].currentBet = 0;

    // Invert then bitwise AND to turn off.
    poker->players[x].state &= ~(
      POKER_PLAYER_STATE_FOLDED | 
      POKER_PLAYER_STATE_SHOWING
    );
  }

  // Hard look at the dealer
  pokerLookAtPlayer(&poker->cameraWorld, POKER_SEAT_DEALER);

  // Shuffle the deck
  for(x = 0; x < CARD_DECK_SIZE - 1; x++) {
    // Select random element from remaining elements.
    y = u8randRange(x, CARD_DECK_SIZE);
    temporary = poker->deck[y];// Take out other card
    poker->deck[y] = poker->deck[x];// Move my card there
    poker->deck[x] = temporary;// Put other card here.
  }
}