public static void main(String[] args) { List<Card> cards = new LinkedList<Card>(); for (Value v : Value.values()) { for (Suit s : Suit.values()) { cards.add(new Card(v, s)); } } Random rand = new Random(); Card[] cards1 = new Card[7]; Card[] cards2 = new Card[7]; for (int i = 0; i < 7; i++) { // Card c = cards.remove(rand.nextInt(cards.size())) ; Card c1 = cards.remove(2 + i * 3); cards1[i] = c1; Card c2 = cards.remove(rand.nextInt(cards.size())); cards2[i] = c2; } System.out.println("------ Card 1 ---------"); for (Card c : cards1) { System.out.println(c.getSuit() + " " + c.getValue()); } System.out.println(Round.getRank(cards1)); System.out.println("------ Card 2 ---------"); for (Card c : cards2) { System.out.println(c.getSuit() + " " + c.getValue()); } System.out.println(Round.getRank(cards2)); }
@Override public int compareTo(Card c) { if (this.suit.compareTo(c.getSuit()) == 0) { return this.rank.compareTo(c.getRank()); } else { return this.suit.compareTo(c.getSuit()); } }
public boolean canTake(Card card) { if (isEmpty()) { return card.getFace() == 0; } else { Card top = this.peek(); return (card.getSuit() == top.getSuit()) && (card.getFace() == (top.getFace() + 1)); } }
// Check if it is legal for a card to move to a non-empty foundation public static boolean moveToFoundationIsLegal(Card card1, Card foundationcard) { if (card1.getValue() == (foundationcard.getValue() + 1)) { if (card1.getSuit().equalsIgnoreCase(foundationcard.getSuit())) return true; } return false; }
/** * @param pCard The card to compare against * @return True if and only if pCard's suit is of the same color as this card. */ public boolean sameColorAs(Card pCard) { assert pCard != null; if (getSuit() == Suit.DIAMONDS || getSuit() == Suit.HEARTS) { return pCard.getSuit() == Suit.DIAMONDS || pCard.getSuit() == Suit.HEARTS; } else { return pCard.getSuit() == Suit.CLUBS || pCard.getSuit() == Suit.SPADES; } }
@Override public int giveValue() { int SuitValue = -1; for (Card c : card) { if (SuitValue == -1) { SuitValue = c.getSuit(); } else { if (SuitValue != c.getSuit()) return 0; // it not flush } } return 2; // flush has 2 points }
double estimateOddsOnPreFlop(Card first, Card second, int opponentCount) { if (first.getValue().ordinal() > second.getValue().ordinal()) { Card tmp = first; first = second; second = tmp; } double baseOdds = preFlopOdds[first.getValue().ordinal()][second.getValue().ordinal()]; // This is a crude approximation if (first.getSuit() == second.getSuit()) { baseOdds += 4.; } return baseOdds / 100; }
private boolean isFlush() { Card referencia = null; boolean retorno = true; for (Card carta : this.cards) { if (referencia == null) { referencia = carta; } else { retorno = (retorno && carta.getSuit() == referencia.getSuit()); } } return retorno; }
@Test public void testCard() { // if card was created (is not null) assertNotNull(card); // if card was created with proper rank assertEquals(rank3, card.getRank()); // if cards suit is different than another suit assertNotEquals(suitD, card.getSuit()); // if after change suits match card.setSuit(suitD); assertEquals(suitD, card.getSuit()); // if card's String representation is as expected assertEquals("3♦", card.toString()); }
private void hit(Player p) { Card drawn = drawCard(); int value = drawn.getValue(); String sval; if (value > 2 && value < 11) { sval = value + ""; } else if (value == 11) { sval = "Jack"; value = 10; } else if (value == 12) { sval = "Queen"; value = 10; } else if (value == 13) { sval = "King"; value = 10; } else { sval = "Ace"; if (p.getScore() <= 10) { value = 11; } } p.add(value); String suit = drawn.getSuit(); io.print(p.getName() + " drew the " + sval + " of " + suit + "."); io.print(p.getName() + "'s score is currently " + p.getScore()); }
@Test public void testNumCards() { List<Card> deck = makeDeck(); Card tmpCard; Hand aHand = new Hand( 1 + " " + deck.remove(0) + " " + deck.remove(0) + " " + deck.remove(0) + " " + deck.remove(0) + " " + deck.remove(0)); assertTrue(aHand.isComplete()); try { tmpCard = deck.remove(0); aHand.addCard(tmpCard.getRank(), tmpCard.getSuit()); } catch (Exception e) { fail("hand exception"); } }
/** * Notify user which suit must be played * * @param cardsPlayed cards played for current turn * @param veryFirstTurn indicator whether this is a very first turn * @param myHand */ private void notifyPlayableSuit( List<Card> cardsPlayed, boolean veryFirstTurn, List<Card> myHand) { Iterator<Card> searchHand = myHand.iterator(); boolean found = false; while (searchHand.hasNext() && found == false) { Card card = searchHand.next(); if (veryFirstTurn) // player 1 in first trick of a round { found = true; System.out.println("(You must lead with the Duece of Clubs)"); } else { if (cardsPlayed.size() > 0) // players 2-4 { Suit suitLed = cardsPlayed.get(0).getSuit(); if (card.getSuit().equals(suitLed)) // user holds a suit matching first suit played { found = true; System.out.println("(You must play " + suitLed + ")"); } } else { found = true; System.out.println("(You can lead with any card)"); } } } }
private int numDifferentSuits() { Set<Character> suits = new HashSet<Character>(); for (Card c : this.cardList) { suits.add(c.getSuit().charAt(0)); } return suits.size(); }
public Player(Vector<Card> cards) { for (int i = 0; i < 4; i++) { Vector<Rank> ranks = new Vector<Rank>(); this.cards.add(ranks); } for (Card card : cards) { suits.add(card.getSuit()); distinctRank.add(card.getRank()); } List<Rank> myList = new ArrayList(distinctRank); myList.sort(new RankComparator()); for (Rank rank : myList) { int count = 0; for (Card card : cards) { if (card.getRank().ordinal() == rank.ordinal()) count++; } if (count < 5 && count > 0) { if (this.cards.size() > 0) this.cards.get(count - 1).add(rank); } } mode = findPlayerMode(); }
/** * Given a Card array (possibly with null values) classifies the current Poker hand and returns * the classification. * * @param hand - a Poker hand represented as an array of Card objects which may contain null * values * @return classification of the given Poker hand */ public static PokerHand getPokerHand(Card[] hand) { // Compute counts int[] rankCounts = new int[Card.NUM_RANKS]; int[] suitCounts = new int[Card.NUM_SUITS]; for (Card card : hand) if (card != null) { rankCounts[card.getRank()]++; suitCounts[card.getSuit()]++; } // Compute count of rank counts int maxOfAKind = 0; int[] rankCountCounts = new int[hand.length + 1]; for (int count : rankCounts) { rankCountCounts[count]++; if (count > maxOfAKind) maxOfAKind = count; } // Flush check boolean hasFlush = false; for (int i = 0; i < Card.NUM_SUITS; i++) if (suitCounts[i] != 0) { if (suitCounts[i] == hand.length) hasFlush = true; break; } // Straight check boolean hasStraight = false; boolean hasRoyal = false; int rank = 0; while (rank <= Card.NUM_RANKS - 5 && rankCounts[rank] == 0) rank++; hasStraight = (rank <= Card.NUM_RANKS - 5 && rankCounts[rank] == 1 && rankCounts[rank + 1] == 1 && rankCounts[rank + 2] == 1 && rankCounts[rank + 3] == 1 && rankCounts[rank + 4] == 1); if (rankCounts[0] == 1 && rankCounts[12] == 1 && rankCounts[11] == 1 && rankCounts[10] == 1 && rankCounts[9] == 1) hasStraight = hasRoyal = true; // Return score if (hasFlush) { if (hasRoyal) return PokerHand.ROYAL_FLUSH; // Royal Flush if (hasStraight) return PokerHand.STRAIGHT_FLUSH; // Straight Flush } if (maxOfAKind == 4) return PokerHand.FOUR_OF_A_KIND; // Four of a Kind if (rankCountCounts[3] == 1 && rankCountCounts[2] == 1) return PokerHand.FULL_HOUSE; // Full House if (hasFlush) return PokerHand.FLUSH; // Flush if (hasStraight) return PokerHand.STRAIGHT; // Straight if (maxOfAKind == 3) return PokerHand.THREE_OF_A_KIND; // Three of a Kind if (rankCountCounts[2] == 2) return PokerHand.TWO_PAIR; // Two Pair if (rankCountCounts[2] == 1) return PokerHand.ONE_PAIR; // One Pair return PokerHand.HIGH_CARD; // Otherwise, High Card. This applies to empty Card arrays as well. }
/** * Returns a bit representation of the card. This representation follow this schema: * * <p>|C|D|H|S|2|3|4|5|6|7|8|9|10|J|Q|K|A| * * @return integer with bitmask representation. */ private int asBits(Card card) { int ret = 0; switch (card.getSuit()) { case CLUBS: ret += 65536; break; case DIAMONDS: ret += 32768; break; case HEARTS: ret += 16384; break; case SPADES: ret += 8192; break; } switch (card.getFace()) { case TWO: ret += 4096; break; case THREE: ret += 2048; break; case FOUR: ret += 1024; break; case FIVE: ret += 512; break; case SIX: ret += 256; break; case SEVEN: ret += 128; break; case EIGHT: ret += 64; break; case NINE: ret += 32; break; case TEN: ret += 16; break; case JACK: ret += 8; break; case QUEEN: ret += 4; break; case KING: ret += 2; break; case ACE: ret += 1; break; } return ret; }
/** * Checks to see if a certain card is in the memory. * * @param c The card to check. * @return True if it is in the memory, else false. */ public boolean contains(Card c) { for (int i = 0; i < memory.size(); i++) { if (memory.get(i).getNumber() == c.getNumber() && memory.get(i).getSuit() == c.getSuit()) { return true; } } return false; }
@Test public void testContructorGetSuit() { System.out.println("card() and getSuit()"); Card.Suit expSuit = Card.Suit.CLUB; Card instance = new Card(expSuit, Card.Rank.TWO); Card.Suit suitResult = instance.getSuit(); assertEquals(expSuit, suitResult); }
public boolean equals(Object o) { Card c; if (o.getClass() == this.getClass()) { c = (Card) o; if (suit.equals(c.getSuit()) && face == c.getFace()) return true; } return false; }
/** * Compare this card with the argument. * * @author Cameron Sullivan * @version 2016.3.11 * @param otherCard the other card to compare to this * @return true if the rank and suit of this card are equal to those of the argument, false * otherwise. */ public boolean equals(Card otherCard) { // If the cards are of the same suit and same rank, they are identical. if (getSuit().equals(otherCard.getSuit()) && getRank().equals(otherCard.getRank())) { return true; } // If not, they are not the same card. else { return false; } }
/* * Compares two cards * Returns 1 if card 1 is highest (ordered by suit) * Returns 0 if card 2 is highest (ordered by suit) * Returns -1 if they are both the same */ private int compareCards(Card card1, Card card2) { // Check for card1 being biggest if (card1.getSuit() > card2.getSuit()) { return 1; } else if (card1.getSuit() < card2.getSuit()) { return 0; } else { // suits must be equal by this stage - check for highest card num if (card1.getCardNum() > card2.getCardNum()) { return 1; } else if (card1.getCardNum() < card2.getCardNum()) { return 0; } else { // suits & card nums must be equal by this stage i.e. cards are the same return -1; } } }
public void render() { int stairNumber = new LimitedIntDialog("De qué escalera ? [1-7]: ", 1, 7).read(); Card cardSelected = moveStairToStickController.getBoard().getStair()[stairNumber - 1].getTopCardStair(); Card cardTopstick = moveStairToStickController .getBoard() .getStick()[cardSelected.getSuit().getNumber()] .getTopCardStick(); if (cardSelected.getRank().getNumber() == (cardTopstick.getRank().getNumber() - 1)) { moveStairToStickController .getBoard() .getStick()[cardSelected.getSuit().getValue()] .addCardToStick(cardSelected); moveStairToStickController .getBoard() .getStair()[stairNumber - 1] .removeCardFromStair(cardSelected); } }
@Test public void testInstantion() { Card card1 = new Card(Suit.HEART, "5"); Card card2 = new Card(Suit.SPADE, "K"); assertNotNull(card1); assertNotNull(card2); assertEquals(10, card2.getValue()); assertEquals("HEART", card1.getSuit().toString()); assertEquals("5", card1.getDescription()); }
public void remove(int columnNumber) { if (colHasCards(columnNumber)) { Card c = getTopCard(columnNumber); boolean removeCard = false; for (int i = 0; i < 4; i++) { if (i != columnNumber) { if (colHasCards(i)) { Card compare = getTopCard(i); if (compare.getSuit() == c.getSuit()) { if (compare.getValue() > c.getValue()) { removeCard = true; } } } } } if (removeCard) { this.cols.get(columnNumber).remove(this.cols.get(columnNumber).size() - 1); } else this.error = true; } }
@Test public final void testGetSuit() { Assert.assertEquals(CardSuit.CLUBS, AC.getSuit()); Assert.assertEquals(CardSuit.HEARTS, KH.getSuit()); Assert.assertEquals(AC.getSuit(), AC2.getSuit()); Assert.assertFalse(AC.getSuit().equals(KH.getSuit())); }
/** * Try to play card selected by user * * @param card card selected to play * @param cardsPlayed cards played for the current turn * @param heartsBroken indicator whether hearts has been broken * @param veryFirstTurn indicator whether this is a very first turn * @throws Exception if card is not valid to play */ public void TryPlayCard( Card card, List<Card> cardsPlayed, boolean heartsBroken, boolean veryFirstTurn) throws Exception { if (veryFirstTurn) { if (card.getSuit() != Suit.Clubs || card.getFace() != Face.Deuce) throw new Exception("Must play deuce of clubs"); } else { List<Card> playable = getLegalCards(cardsPlayed, heartsBroken); if (!playable.contains(card)) throw new Exception("Invalid card to play. Try other card!"); getHand().remove(card); } }
/** * Returns the graphical image of the card. * * @return the image */ public BufferedImage getImage() { // Generate the filename String filename; if (card.isVisible()) { filename = IMG_DIR + card.getRank().name() + IMG_SEPERATOR + card.getSuit().name() + IMG_FILETYPE; } else filename = IMG_DIR + BACK_FILENAME + IMG_FILETYPE; // Create the image BufferedImage img = null; try { img = ImageIO.read(new File(filename)); } catch (IOException e) { e.printStackTrace(); System.exit(1); } return img; }
public int takeTrick() { if (leadSuit == null) { System.out.println("Lead Suit not set!!"); } int highestValue = -1; int playerNum = -1; for (int i = 0; i < Game.numPlayers; i++) { Card current = playedCards[i]; if (current.getSuit().equals(leadSuit)) { if (current.getValue().getNum() > highestValue) { highestValue = current.getValue().getNum(); playerNum = i; } } } return playerNum; }
/** * User input is converted from string into tokens. Requires input in the form of "[number] space * [first letter of suit]" * * <p>Both tokens are then compared against card suits and faces in hand to determine if they * match. * * @param cardStr * @param myHand * @param potentialPlay * @return potentialPlay, which returns either a valid card or null */ private Card tokenizeString(String cardStr, List<Card> myHand, Card potentialPlay) { StringTokenizer st = new StringTokenizer(cardStr); // turn individual characters/words into tokens String numStr = st.nextToken(); int myFace = Integer.parseInt(numStr); String suitStr = st.nextToken(); Suit mySuit = null; if (suitStr.equalsIgnoreCase("c")) // clubs mySuit = Suit.Clubs; else if (suitStr.equalsIgnoreCase("d")) // diamonds mySuit = Suit.Diamonds; else if (suitStr.equalsIgnoreCase("h")) // hearts mySuit = Suit.Hearts; else if (suitStr.equalsIgnoreCase("s")) // spades mySuit = Suit.Spades; else { System.out.println("Invalid character typed. Must be the first letter of a suit"); return potentialPlay; } Iterator<Card> iterator = myHand.iterator(); Card card = null; boolean match = false; while (iterator.hasNext() && match == false) { card = iterator.next(); if (card.getFace().ordinal() == myFace - 2) // num - 2 is offset in ordinal. 4 is actually 2. { if (card.getSuit() == mySuit) { match = true; potentialPlay = card; myHand.remove(card); return potentialPlay; } } } if (!match) { System.out.println("Seriously? Choose a VALID card"); } return potentialPlay; }
public void playCard(int playerNum, Card card) { playedCards[playerNum] = card; if (leadSuit == null) { leadSuit = card.getSuit(); } }