java - Poker game hand evaluator arrays condition structure -


i made quick poker game. generates 5 random numbers , converts numbers actual cards values , symbols based on value. however, have problems when comes making hand evaluation.

so far did flush right it's easy it's not perfect (it prints user has flush 5 times... ) , appreciate if me pair, 2 pair, 3 of kind , straight. rest afterwards need heads-up on how those.

thank in advance help, here code :

package tests;  import java.util.*;  public class tests {  public static void main(string[] args) {     boolean[] pack = new boolean[52]; // array not generate same number twice     int[] cards = new int[5]; //the 5 unique random numbers stored in here.     string[] cardsvalues = new string[5]; // assign card's value based on random number's value      char[] cardssymbols = new char[5];//this assign card's symbol based on random number's value      char symbols[] = {'♥', '♦', '♣', '♠'}; // possible symbols random number can take     string values[] = {"a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k"}; // possible values random number can take     random give = new random();      (int = 0; < cards.length; i++) { // gives 5 unique random numbers         {             cards[i] = give.nextint(52);         } while (pack[cards[i]]);         pack[cards[i]] = true;         system.out.println(cards[i]);     }      (int = 0; < cards.length; i++) { // converts number card symbol based on number's value         final int numofsymbol = cards[i] / 13;         cardssymbols[i] = symbols[numofsymbol];     }     (int = 0; < cards.length; i++) { // converts number actual card value based on number's value.         final int numofvalues = cards[i] % 13;         cardsvalues[i] = values[numofvalues];     }     (int = 0; < cardssymbols.length; i++) { // prints actual cards once converted         system.out.print(cardssymbols[i]);         system.out.println(cardsvalues[i]);     }     (int = 0; < cardsvalues.length; i++) { //here problem, have no idea on how make handevaluator ...         if (cardsvalues[i] == cardsvalues[i] + 1) {             system.out.println("pair !!!");         } else if (cardsvalues[i] == cardsvalues[i] + 1 && cardsvalues[i] == cardsvalues[i] + 2) {             system.out.println("trips !!!");         } else if (cardssymbols[0] == cardssymbols[1] && cardssymbols[1] == cardssymbols[2] && cardssymbols[2] == cardssymbols[3] && cardssymbols[3] == cardssymbols[4]) {             system.out.println("flushhh");         }     } } 

hints:

  • to simplify testing straights , sorting highest card, easier represent ranks indexes, , translate them symbols printing.
  • using card object allows clearer code.
  • the java collection framework has useful functions shuffling, slicing , sorting.

my solution:

public class test {      static final char[] suits = {'♥', '♦', '♣', '♠'};     static final string[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k", "a"};      static class card {         final int suit;         final int rank;          card(int s, int r) {             suit = s;             rank = r;         }          @override         public string tostring() {             return suits[suit] + ranks[rank]; // or want cards printed         }     }       public static void main(string[] args) {         list<card> deck = new arraylist<>();         (int s = 0; s < suits.length; s++) {             (int r = 0; r < ranks.length; r++) {                 deck.add(new card(s,r));             }         }         collections.shuffle(deck);         list<card> hand = deck.sublist(0,5);          collections.sort(hand, comparator.comparing(c -> c.rank));         system.out.println("your hand is: " + hand);          system.out.println(value(hand));      }      static string value(list<card> hand) {         boolean straight = true;         boolean flush = true;         (int = 1; < hand.size(); i++) {             straight &= hand.get(i - 1).rank + 1 == hand.get(i).rank;             flush &= hand.get(i - 1).suit == hand.get(i).suit;         }          if (straight && flush) {             return "straight flush " + hand.get(4);         }          list<run> runs = findruns(hand);         runs.sort(comparator.comparing(r -> -r.rank));         runs.sort(comparator.comparing(r -> -r.length));          if (runs.get(0).length == 4) {             return "four of kind: " + runs;         }          if (runs.get(0).length == 3 && runs.get(1).length == 2) {             return "full house: " + runs;         }          if (straight) {             return "straight " + hand.get(4);         }          if (runs.get(0).length == 3) {             return "three of kind: " + runs;         }          if (runs.get(1).length == 2) {             return "two pair: " + runs;         }          if (runs.get(0).length == 2) {             return "pair: " + runs;         }          return "high card: " + runs;     }      /** represents {@code length} cards of rank {@code rank} */     static class run {         int length;         int rank;          @override         public string tostring() {             return ranks[rank];         }     }      static list<run> findruns(list<card> hand) {         list<run> runs = new arraylist<>();         run run = null;         (card c : hand) {             if (run != null && run.rank == c.rank) {                 run.length++;             } else {                 run = new run();                 runs.add(run);                 run.rank = c.rank;                 run.length = 1;             }         }         return runs;     } } 

example output:

your hand is: [♣10, ♥j, ♦j, ♠k, ♥k] 2 pair: [k, j, 10] 

Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -