package com.bassoli.poker; public class WinAnalyzer { public static boolean firstHandWins(Hand first, Hand second, Hand board) { if ((first.getHandSize() != 2)||(second.getHandSize() != 2)) { return false; } if ((board == null)||(board.getHandSize() == 0)) { return firstHandWins(first,second); } Card[] cards = board.getCards(); Hand firstTemp = (Hand)first.clone(); Hand secondTemp = (Hand)second.clone(); for (int i = 0 ; i < cards.length ; i++) { firstTemp.addCard(cards[i]); secondTemp.addCard(cards[i]); } return firstHandWins(firstTemp,secondTemp); } public static boolean secondHandWins(Hand first, Hand second, Hand board) { if ((first.getHandSize() != 2)||(second.getHandSize() != 2)) { return false; } if ((board == null)||(board.getHandSize() == 0)) { return secondHandWins(first,second); } Card[] cards = board.getCards(); Hand firstTemp = (Hand)first.clone(); Hand secondTemp = (Hand)second.clone(); for (int i = 0 ; i < cards.length ; i++) { firstTemp.addCard(cards[i]); secondTemp.addCard(cards[i]); } return secondHandWins(firstTemp,secondTemp); } public static boolean handsTied(Hand first, Hand second, Hand board) { if ((first.getHandSize() != 2)||(second.getHandSize() != 2)) { return false; } if ((board == null)||(board.getHandSize() == 0)) { return handsTied(first,second); } Card[] cards = board.getCards(); Hand firstTemp = (Hand)first.clone(); Hand secondTemp = (Hand)second.clone(); for (int i = 0 ; i < cards.length ; i++) { firstTemp.addCard(cards[i]); secondTemp.addCard(cards[i]); } return handsTied(firstTemp,secondTemp); } public static boolean firstHandWins(Hand first, Hand second) { if (first.getHandSize() != second.getHandSize()) { return false; } String winCodeFirst = HandAnalyzer.getWinCode(first); String winCodeSecond = HandAnalyzer.getWinCode(second); if (winCodeFirst.charAt(0) > winCodeSecond.charAt(0)) { return true; } else if (winCodeFirst.charAt(0) < winCodeSecond.charAt(0)) { return false; } else { for (int i = 2 ; i < winCodeFirst.length() ; i++) { if (Card.getValueInt(winCodeFirst.charAt(i)) > Card.getValueInt(winCodeSecond.charAt(i))) return true; else if (Card.getValueInt(winCodeFirst.charAt(i)) == Card.getValueInt(winCodeSecond.charAt(i))) continue; else return false; } return false; } } public static boolean secondHandWins(Hand first, Hand second) { if (first.getHandSize() != second.getHandSize()) { return false; } String winCodeFirst = HandAnalyzer.getWinCode(first); String winCodeSecond = HandAnalyzer.getWinCode(second); if (winCodeFirst.charAt(0) < winCodeSecond.charAt(0)) { return true; } else if (winCodeFirst.charAt(0) > winCodeSecond.charAt(0)) { return false; } else { for (int i = 2 ; i < winCodeFirst.length() ; i++) { if (Card.getValueInt(winCodeFirst.charAt(i)) < Card.getValueInt(winCodeSecond.charAt(i))) return true; else if (Card.getValueInt(winCodeFirst.charAt(i)) == Card.getValueInt(winCodeSecond.charAt(i))) continue; else return false; } return false; } } public static boolean handsTied(Hand first, Hand second) { if (first.getHandSize() != second.getHandSize()) { return false; } String winCodeFirst = HandAnalyzer.getWinCode(first); String winCodeSecond = HandAnalyzer.getWinCode(second); if (winCodeFirst.charAt(0) > winCodeSecond.charAt(0)) { return false; } else if (winCodeFirst.charAt(0) < winCodeSecond.charAt(0)) { return false; } else { for (int i = 2 ; i < winCodeFirst.length() ; i++) { if (Card.getValueInt(winCodeFirst.charAt(i)) != Card.getValueInt(winCodeSecond.charAt(i))) return false; } return true; } } public static void main(String[] args) { Hand first = new Hand(); Hand second = new Hand(); Hand board = new Hand(); first.addCard("KA"); first.addCard("S4"); second.addCard("HA"); second.addCard("H4"); board.addCard("H2"); board.addCard("HT"); board.addCard("H4"); System.out.println(WinAnalyzer.firstHandWins(first,second,board)); System.out.println(WinAnalyzer.secondHandWins(first,second,board)); System.out.println(WinAnalyzer.handsTied(first,second,board)); } }