<commit_before>package com.skraylabs.poker;

import com.skraylabs.poker.model.Board;
import com.skraylabs.poker.model.Card;
import com.skraylabs.poker.model.CardUtils;
import com.skraylabs.poker.model.GameState;
import com.skraylabs.poker.model.Pocket;
import com.skraylabs.poker.model.Rank;

import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * For a given {@link com.skraylabs.poker.model.GameState}, calculates the outcome odds for each
 * Player.
 */
class OddsCalculator {

  private GameState gameState;

  public OddsCalculator(GameState gameState) {
    this.gameState = gameState;
  }

  /**
   * Report the odds of a player getting a Two Of A Kind.
   *
   * @param playerIndex index of Player in the GameState. A number in range [0, 9].
   * @return the probability of getting a Two Of A Kind.
   */
  public double twoOfAKindForPlayer(int playerIndex) {
    double result = 0.0;
    Pocket[] pockets = gameState.getPockets();
    Pocket pocket = pockets[playerIndex];
    Board board = gameState.getBoard();
    Collection<Card> cards = CardUtils.collectCards(board);
    cards.addAll(CardUtils.collectCards(pocket));
    Map<Rank, Long> countByRank =
        cards.stream().collect(Collectors.groupingBy(Card::getRank, Collectors.counting()));
    for (Long count : countByRank.values()) {
      if (count >= 2) {
        result = 1.0;
        break;
      }
    }
    if (result < 1.0) {
      int numberOfCardsToBeDealt = 5 - board.size();
      double numberOfOuts = numberOfCardsToBeDealt * cards.size() * 3;
      double numberOfRemainingCards = 52 - cards.size();
      result = numberOfOuts / numberOfRemainingCards;
    }
    return result;
  }
}
<commit_msg>Factor-out some logic that determines a Two of a Kind.<commit_after>package com.skraylabs.poker;

import com.skraylabs.poker.model.Board;
import com.skraylabs.poker.model.Card;
import com.skraylabs.poker.model.CardUtils;
import com.skraylabs.poker.model.GameState;
import com.skraylabs.poker.model.Pocket;
import com.skraylabs.poker.model.Rank;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * For a given {@link com.skraylabs.poker.model.GameState}, calculates the outcome odds for each
 * Player.
 */
class OddsCalculator {

  private GameState gameState;

  public OddsCalculator(GameState gameState) {
    this.gameState = gameState;
  }

  /**
   * Report the odds of a player getting a Two Of A Kind.
   *
   * @param playerIndex index of Player in the GameState. A number in range [0, 9].
   * @return the probability of getting a Two Of A Kind.
   */
  public double twoOfAKindForPlayer(int playerIndex) {
    double result = 0.0;
    Pocket[] pockets = gameState.getPockets();
    Pocket pocket = pockets[playerIndex];
    Board board = gameState.getBoard();
    Collection<Card> cards = CardUtils.collectCards(board);
    cards.addAll(CardUtils.collectCards(pocket));
    Map<Rank, Long> countByRank =
        cards.stream().collect(Collectors.groupingBy(Card::getRank, Collectors.counting()));
    for (Long count : countByRank.values()) {
      if (count >= 2) {
        result = 1.0;
        break;
      }
    }
    if (result < 1.0) {
      int numberOfCardsToBeDealt = 5 - board.size();
      double numberOfOuts = numberOfCardsToBeDealt * cards.size() * 3;
      double numberOfRemainingCards = 52 - cards.size();
      result = numberOfOuts / numberOfRemainingCards;
    }
    return result;
  }

  /**
   * Helper method that determines if a Two of a Kind exists on a given combination of board and
   * pocket cards.
   *
   * @param board cards collected from a {@link Board}
   * @param pocket cards collected from a {@link Pocket}
   * @return {@code true} if there is at least one Two of a Kind; {@code false} otherwise
   */
  static boolean hasTwoOfAKind(Collection<Card> board, Collection<Card> pocket) {
    boolean result = false;
    Collection<Card> cards = new ArrayList<>(board);
    cards.addAll(pocket);
    Map<Rank, Long> countByRank =
        cards.stream().collect(Collectors.groupingBy(Card::getRank, Collectors.counting()));
    for (Long count : countByRank.values()) {
      if (count >= 2) {
        result = true;
        break;
      }
    }
    return result;
  }
}

{'fraction_non_alphanumeric': 0.11183294663573086, 'fraction_numerical': 0.0062645011600928075, 'mean_word_length': 3.311, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1786566', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1372, 'n_words': 362}