Skip to content

Instantly share code, notes, and snippets.

@segun
Created May 7, 2020 18:10
Show Gist options
  • Save segun/fd39fbb221c25b33d21c4aaa391115ec to your computer and use it in GitHub Desktop.
Save segun/fd39fbb221c25b33d21c4aaa391115ec to your computer and use it in GitHub Desktop.
Poker Game Rules.
//Compiler version 1.8.0_111
import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
//TODO: Check logic for isPair.
//TODO: Implement is2Pairs
class Solution {
/**
* @param {String[]} hand1
* @param {String[]} hand2
* @return {int} winner
*/
public static List<String> royalFlush = Arrays.asList("T", "J", "Q", "K", "A");
public static String highCard(String h[]) {
List<String> handList = Arrays.asList(h);
Collections.sort(handList);
return handList.get(h.length - 1);
}
public static boolean isPair(String h[]) {
return isXOfAKind(h, 2);
}
public static boolean isStraight(String h[]) {
List<String> handList = Arrays.asList(h);
Collections.sort(handList);
String previousCard = null;
String theSuit = "XX";
for (String hand : handList) {
String suit = "XX";
String card = hand.charAt(0) + "";
if (theSuit.equals(suit)) {
// still same suit and card is previousCard + 1;
if (previousCard != null) {
int cc = Integer.parseInt(card);
int pcc = Integer.parseInt(previousCard);
if (cc != (pcc + 1)) {
return false;
}
}
} else {
return false;
}
}
return true;
}
public static boolean isFlush(String h[]) {
char theSuit = h[0].charAt(1);
for (int i = 0; i < h.length; i++) {
char suit = hand.charAt(1);
if (suit != theSuit) {
return false;
}
}
return false;
}
public static boolean isFourOfAKind(String h[]) {
return isXOfAKind(h, 4);
}
public static boolean isThreeOfAKind(String h[]) {
return isXOfAKind(h, 3);
}
public static boolean isXOfAKind(String h[], int x) {
int kind = 0;
char value = h[0].charAt(0);
for (int i = 0; i < h.length; i++) {
if (h[i].charAt(0) == value) {
kind++;
} else {
kind--;
}
}
System.out.println(kind);
return kind >= (x - 1);
}
public static boolean isStraightFlush(String h[]) {
List<String> handList = Arrays.asList(h);
Collections.sort(handList);
String previousCard = null;
String theSuit = h[0].charAt(1) + "";
for (String hand : handList) {
String suit = hand.charAt(1) + "";
String card = hand.charAt(0) + "";
if (theSuit.equals(suit)) {
// still same suit and card is previousCard + 1;
if (previousCard != null) {
int cc = Integer.parseInt(card);
int pcc = Integer.parseInt(previousCard);
if (cc != (pcc + 1)) {
return false;
}
}
} else {
return false;
}
}
return true;
}
public static boolean isRoyalFlush(String hand[]) {
String theSuit = hand[0].charAt(1) + "";
// copy it.
List<String> remainingFlushCards = royalFlush.stream().collect(Collectors.toList());
for (int i = 0; i < hand.length; i++) {
String suit = hand[i].charAt(1) + "";
String card = hand[i].charAt(0) + "";
if (theSuit.equals(suit) && remainingFlushCards.contains(card)) {
// still same suit and card is in royalFlush
// remove this card from remainingFlush
List<String> temp = remainingFlushCards.stream().filter(x -> {
return !(x.equals(card));
}).collect(Collectors.toList());
remainingFlushCards = temp;
} else {
return false;
}
}
return true;
}
public static int determine_winner(String[] hand1, String[] hand2) {
int winner = 0;
// Put your code here to calculate the winner
boolean h1Wins = isRoyalFlush(hand1);
boolean h2Wins = isRoyalFlush(hand2);
if (h1Wins == true && h2Wins == false) {
return 1;
} else if (h1Wins == false && h2Wins == true) {
return 2;
}
h1Wins = isStraightFlush(hand1);
h2Wins = isStraightFlush(hand2);
if (h1Wins == true && h2Wins == false) {
return 1;
} else if (h1Wins == false && h2Wins == true) {
return 2;
}
h1Wins = isFourOfAKind(hand1);
h2Wins = isFourOfAKind(hand2);
if (h1Wins == true && h2Wins == false) {
return 1;
} else if (h1Wins == false && h2Wins == true) {
return 2;
}
h1Wins = isFlush(hand1);
h2Wins = isFlush(hand2);
if (h1Wins == true && h2Wins == false) {
return 1;
} else if (h1Wins == false && h2Wins == true) {
return 2;
}
h1Wins = isStraight(hand1);
h2Wins = isStraight(hand2);
if (h1Wins == true && h2Wins == false) {
return 1;
} else if (h1Wins == false && h2Wins == true) {
return 2;
}
h1Wins = isThreeOfAKind(hand1);
h2Wins = isThreeOfAKind(hand2);
if (h1Wins == true && h2Wins == false) {
return 1;
} else if (h1Wins == false && h2Wins == true) {
return 2;
}
h1Wins = isPair(hand1);
h2Wins = isPair(hand2);
if (h1Wins == true && h2Wins == false) {
return 1;
} else if (h1Wins == false && h2Wins == true) {
return 2;
}
String h1HighCard = highCard(hand1);
String h2HighCard = highCard(hand2);
int h1HC = Integer.parseInt(h1HighCard.charAt(0) + "");
int h2HC = Integer.parseInt(h2HighCard.charAt(0) + "");
if(h1HC > h2HC) {
return 1;
} else if(h2HC > h2HC) {
return 2;
}
// Return the result, do not change the structure. winner = 1 -> Player 1 wins,
// winner = 2 -> Player 2 wins
return winner;
}
}
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String[] components = sc.nextLine().split(" ");
String[] hand1 = new String[5];
String[] hand2 = new String[5];
for (int i = 0; i < 10; i++) {
if (i < 5)
hand1[i] = components[i];
else
hand2[i - 5] = components[i];
}
int winner = Solution.determine_winner(hand1, hand2);
System.out.printf("Player %d wins", winner);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment