Package com.poker.analyst.analyser.combanalyzer

Source Code of com.poker.analyst.analyser.combanalyzer.StraightAnalyzer

package com.poker.analyst.analyser.combanalyzer;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.poker.analyst.analyser.CardComparator;
import com.poker.analyst.combination.Combination;
import com.poker.analyst.combination.Flush;
import com.poker.analyst.combination.FourOfKind;
import com.poker.analyst.combination.Pair;
import com.poker.analyst.combination.ThreeOfKind;
import com.poker.analyst.combination.Straight;
import com.poker.analyst.element.Card;
import com.poker.analyst.element.CardFace;
import com.poker.analyst.element.CardPositionType;
import com.poker.analyst.element.PlayingCards;
import com.poker.analyst.element.Suit;


public class StraightAnalyzer {
  public static List<Straight> analyseStraight(PlayingCards plCards,
      List<Pair> pairs,
      List<ThreeOfKind> threeOfKinds,
      List<FourOfKind> fourOfKinds){
    Set<Card> allCards = new TreeSet<Card>(new CardComparator());
    for (Card card: plCards.getPlayerCards())
      if (card != null) allCards.add(card);
    for (Card card: plCards.getTableCards())
      if (card != null) allCards.add(card);

    return analyseStraight(allCards, plCards, pairs, threeOfKinds,fourOfKinds,5);
  }
  public static List<Straight> analyseStraightOESD(PlayingCards plCards,
      List<Pair> pairs,
      List<ThreeOfKind> threeOfKinds,
      List<FourOfKind> fourOfKinds){
    Set<Card> allCards = new TreeSet<Card>(new CardComparator());
    for (Card card: plCards.getPlayerCards())
      if (card != null) allCards.add(card);
    for (Card card: plCards.getTableCards())
      if (card != null) allCards.add(card);

    return analyseStraight(allCards, plCards, pairs, threeOfKinds,fourOfKinds,4);
  }
 
  public static List<Straight> analyseStraightGH(PlayingCards plCards,
      List<Pair> pairs,
      List<ThreeOfKind> threeOfKinds,
      List<FourOfKind> fourOfKinds){
   
   
    // TODO need testing
    return null;
    /*
   
    Set<Card> allCards = new TreeSet<Card>(new CardComparator());
    for (Card card: plCards.getPlayerCards())
      if (card != null) allCards.add(card);
    for (Card card: plCards.getTableCards())
      if (card != null) allCards.add(card);

   
    PlayingCards myPlCards = new PlayingCards(plCards.getPlayerCards(), plCards.getTableCards(), true);
    List<Straight> straightGh = null;
    List<Straight> straightGhAll = null;
    List<CardFace> outFaces = null;
   
   
    boolean isInList;
    for (int cface1 = CardFace.KING.getSerialNumber(); cface1 >=CardFace.TWO.getSerialNumber(); cface1--){
      isInList = false;
      for(Card card: allCards){
        if (card.getFace().getSerialNumber().equals(cface1))
          isInList = true;
      }
      if (isInList) continue;
      myPlCards.getTableCards()[5] = new Card(Suit.forAnalyse,CardFace.getGetCardFaceFromNumber(cface1));
      straightGh = analyseStraight(myPlCards,pairs,threeOfKinds,fourOfKinds);
      if (straightGh != null){       
        if (straightGhAll == null)
          straightGhAll = new ArrayList<Straight>();
       
        for (Straight tempStright: straightGh){
          tempStright.getCombinationList().remove(myPlCards.getTableCards()[5]);
          outFaces = new ArrayList<CardFace>();
          outFaces.add(myPlCards.getTableCards()[5].getFace());
          tempStright.setOutFaces(outFaces);
        }
        straightGhAll.addAll(straightGh);
      }                 
    }
    CardFace cface1 = CardFace.ACE;
      isInList = false;
      for(Card card: allCards){
        if (card.getFace().getSerialNumber().equals(cface1))
          isInList = true;
      }
      if (!isInList) {
        myPlCards.getTableCards()[5] = new Card(Suit.forAnalyse,cface1);
        straightGh = analyseStraight(myPlCards,pairs,threeOfKinds,fourOfKinds);
        if (straightGh != null){       
          if (straightGhAll == null)
            straightGhAll = new ArrayList<Straight>();
         
         
         
          for (Straight tempStright: straightGh){
            outFaces = new ArrayList<CardFace>();
            outFaces.add(myPlCards.getTableCards()[5].getFace());
            tempStright.setOutFaces(outFaces);
           
            if (tempStright.getMinValue().equals(CardFace.ACE)){
              tempStright.getCombinationList().remove(myPlCards.getTableCards()[5]);
              straightGhAll.add(tempStright);
            }else{
              tempStright.getCombinationList().remove(myPlCards.getTableCards()[5]);
              List<Straight> straightGhAll2 = new ArrayList<Straight>();
              straightGhAll2.add(tempStright);
              straightGhAll2.addAll(straightGhAll);
              straightGhAll = straightGhAll2;
            }
          }
          straightGhAll.addAll(straightGh);
        }   
      }
    return straightGhAll;
    */
  }
 
 
 
 
 

  public static List<Straight> analyseStraight(Set<Card> allCards,
                                  PlayingCards plCards,                        
                                  List<Pair> pairs,
                                  List<ThreeOfKind> threeOfKinds,
                                  List<FourOfKind> fourOfKinds, int numCards){

    Straight      comb       = null;
    List<Straight>     combinations = new ArrayList<Straight>();          
    List<Card>       combinationList;     
    List<Set<Card>> allStraightCardLists = null;
    Set<Card>     straightCardList    = null;
    List<CardFace> outFaces = null;
   

    List<CardFace> repeatedCards         = new ArrayList<CardFace>();
    if (pairs != null)
      for (Combination repeatedComb: pairs)
        repeatedCards.add(repeatedComb.getMaxValue());       
    if (threeOfKinds != null)
      for (Combination repeatedComb: threeOfKinds)
        repeatedCards.add(repeatedComb.getMaxValue());
    if (fourOfKinds != null)
      for (Combination repeatedComb: fourOfKinds)
        repeatedCards.add(repeatedComb.getMaxValue())

    allStraightCardLists = new ArrayList<Set<Card>>();   

    Set<Card> allNonRepCards = new TreeSet<Card>(new CardComparator());
    CardFace previousCard = null;

    for (Card card: allCards){
      if (card.getFace().equals(previousCard))
        continue;
      else
        previousCard = card.getFace();
     
      allNonRepCards.add(card);
    }
   
    int i = 0;
    int count = 0;
    int previous = 100;
    int position = -1;
   
    List<Integer> positions = new ArrayList<Integer>();
    do{
      i = 0;
      for (Card card: allNonRepCards){
        i++;
        if (position >= i ) continue;
        if (card.getFace().getSerialNumber() == previous - 1){
          previous--;
          count++;
        }
        else{
          previous = card.getFace().getSerialNumber();
          position = i;
          count = 1;   
        }
 
        if (count == numCards){
          positions.add(position);
          break;
        }       
      }
    }while(count == numCards);
   
    for (Integer pos: positions){
      i = 0;
      straightCardList = new TreeSet<Card>(new CardComparator());
      for (Card card: allNonRepCards){
        if (++i < pos) continue;
        if (card.getFace().equals(CardFace.ACE) && numCards == 4){
          straightCardList = null;
          break;
        }
        straightCardList.add(card);       
        if (i == pos + numCards - 1) break;
       
      }
      if (straightCardList != null)
      allStraightCardLists.add(straightCardList);
    }
   
    if (allStraightCardLists != null && allStraightCardLists.size() != 0){     
      for (Set<Card> straightsList : allStraightCardLists){
        comb = new Straight();
       
        combinationList = new ArrayList<Card>();
        for (Card card: straightsList)
          combinationList.add(card);
        comb.setCombinationList(combinationList);
        if(numCards == 4){
          outFaces = new ArrayList<CardFace>();
          outFaces.add(CardFace.getGetCardFaceFromNumber(comb.getMaxValue().getSerialNumber() + 1));
          if (comb.getMinValue().equals(CardFace.TWO))
            outFaces.add(CardFace.ACE);
          else
            outFaces.add(CardFace.getGetCardFaceFromNumber(comb.getMinValue().getSerialNumber() - 1));
         
          comb.setOutFaces(outFaces);
        }
        combinations.add(comb);
      }     
    }
    // Checking for (A- 2 3 4 5)
    if (numCards == 5){
      combinationList = new ArrayList<Card>();
      i = 0;
      int max = allNonRepCards.size() - 1;
      for (Card card: allNonRepCards){     
        if (i == 0 && card.getFace().equals(CardFace.ACE))
          combinationList.add(card);     
        if (i == max - 3 && card.getFace().equals(CardFace.FIVE) && numCards == 5)
          combinationList.add(card);
        if (i == max - 2 && card.getFace().equals(CardFace.FOUR))
          combinationList.add(card);
        if (i == max - 1 && card.getFace().equals(CardFace.THREE))
          combinationList.add(card);
        if (i == max && card.getFace().equals(CardFace.TWO))
          combinationList.add(card);       
        i++; 
      }
      if (combinationList.size() == numCards){
        comb = new Straight();     
        comb.setCombinationList(combinationList);
        combinations.add(comb);
      }   
    }
   
   
   
    if (combinations.size() != 0)
      return combinations;
    else
      return null;
  }
 
}
TOP

Related Classes of com.poker.analyst.analyser.combanalyzer.StraightAnalyzer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.