×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Shamir Yona
Added: Nov 23, 2015 1:19 PM
Views: 10
Tags: no tags
Implementation of a Poker hand.
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package c2java;
  7.  
  8. /**
  9.  *
  10.  * @author shayzukerman
  11.  */
  12. public class Hand
  13. {
  14.     public static final int CARDS_IN_HAND = 5;
  15.    
  16.     Card[] cards = new Card[CARDS_IN_HAND];
  17.    
  18.     public Hand(String cardsLineStr)
  19.     {
  20.         String[] cardsStrTbl = cardsLineStr.split("\\s");
  21.         int cardCnt = 0;
  22.         for (String cardStr : cardsStrTbl)
  23.         {
  24.             this.cards[cardCnt] = new Card(cardStr);
  25.             if (++cardCnt == CARDS_IN_HAND)
  26.                 break;
  27.         }
  28.     }
  29.  
  30.     @Override
  31.     public String toString()
  32.     {
  33.         StringBuilder sb = new StringBuilder();
  34.         for (Card card : this.cards)
  35.             sb.append(card).append(' ');
  36.        
  37.         return("{ " + sb.toString() + "}");
  38.     }
  39.    
  40.     public static void main(String[] args)
  41.     {
  42.         Hand hand = new Hand("5H 5C 6S 7S KD");
  43.         System.out.println("Hand: " + hand);
  44.     }
  45. }
  46.