620 likes | 1.02k Views
Card 크레스. Card String suit String face int rank Card (String, String, int ) Card () int compareTo (Card ) String toString (). Deck 크레스. Deck ArrayList <Card> cards Deck () Card deal (). 52 Card 객체 들 : Hearts, Diamonds, Spades, Clubs
E N D
Card 크레스 Card String suit String face intrank Card(String, String, int) Card() intcompareTo(Card) StringtoString()
Deck 크레스 Deck ArrayList<Card> cards Deck() Card deal() 52 Card 객체들: Hearts, Diamonds, Spades, Clubs Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King
publicDeck() { //constructor cards = newArrayList<Card>(); String[] suit = {"Clubs", "Spades", "Hearts", "Diamonds"}; String[] face = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; for(intx = 0; x < 52; x++) { Card c = new Card(); c.setSuit(suit[x / 13]); c.setFace(face[x % 13]); c.setRank((x % 13) + 1); cards.add(c); } Collections.shuffle(cards); }
Hand 크레스 Hand ArrayList<Card> cards ArrayList<Card> evalCards inthandValue booleanisHi booleanisPair booleanis2Pair booleanis3 booleanis4 booleanisFlush booleanisFull booleanisStr8 booleanisStr8Flush addToHand(Card) Stringeval() StringcalcHandValue(int, int, int, int)
Hand Evaluation • 2 methods: • eval() • calcHandValue(int, int, int, int)
eval() • Multiple cards of same rank: • Four of a kind, full house, three of a kind, 2pair, pair • 5 cards in order of rank • Str8 • 5 cards of same suit • Flush
sameSuit[0] sameSuit[1] sameSuit[2] sameSuit[3] = 0 = 0 = 0 = 1
calcHandValue(int,int,int,int) • Instance variable handValuecalculation, representing the strength of the hand
inthandValue • Six 4bit ‘words’ : 0000 0000 0000 0000 0000 0000 Type Card1 Card2 Card3 Card4 Card5
inthandValue • First ‘word’ - Type 1000 isStr8Flush 0111 is4 0110 isFull 0101 isFlush 0100 isStr8 0011 is3 0010 is2Pair 0001 isPair
Example: isFlush Player 1: Player 2:
Example: isFlush Player 1: inthandValue: 0000 0000 0000 0000 0000
Example: isFlush Player 1: inthandValue: 1101 1010 0111 0100 0011 Bit Shift Operation: 13<<16 10<<12 7<<8 4<<4 3
inthandValue • First ‘word’ - Type 1000 isStr8Flush 0111 is4 0110 isFull 0101 isFlush 0100 isStr8 0011 is3 0010 is2Pair 0001 isPair
Example: isFlush isFlush Player 1: inthandValue: 0101 1101 1010 0111 0100 0011 5<<20 Bit Shift Operation: 13<<16 10<<12 7<<8 4<<4 3
Example: isFlush isFlush Player 1: inthandValue: 6,137,667
Example: isFlush isFlush Player 1: 6,137,667 inthandValue: isFlush Player 2: 6,137,666 inthandValue:
Example: isFlush 6,137,667 > 6,137,666 Player 1 wins!
Write calcHandValue() method Add to evalTest(): assertEquals(h.handValue, 6137667)
Example: multiple cards same rank Player 1: Player 2:
Example: multiple cards same rank Player 1:
Example: multiple cards same rank Player 1:
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 1 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 2 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 0 = 1 = 0 = 2 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 2 = 0 = 0 = 0 = 0 = 0 = 0 = 2 = 1 = 0 = 2 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 2 = 0 = 0 = 0 = 0 = 0 = 0 = 2 = 1 = 0 = 2 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 2 = 0 = 0 = 0 = 0 = 0 = 0 = 2 = 1 = 0 = 2 = 0
Example: multiple cards same rank X=12, sameCard1=2, sameCardRank1=12
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 2 = 0 = 0 = 0 = 0 = 0 = 0 = 2 = 1 = 0 = 2 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 2 = 0 = 0 = 0 = 0 = 0 = 0 = 2 = 1 = 0 = 2 = 0
sameRank [0] sameRank[1] sameRank[2] sameRank[3] sameRank[4] sameRank[5] sameRank[6] sameRank[7] sameRank[8] sameRank[9] sameRank[10] sameRank[11] sameRank[12] sameRank[13] = 0 = 0 = 2 = 0 = 0 = 0 = 0 = 0 = 0 = 2 = 1 = 0 = 2 = 0