Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Card and Deck classes that were missed in the last commit
- Loading branch information
1 parent
b0bd2af
commit b5b857e
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package pokerhands; | ||
|
||
/** | ||
* | ||
* @author jfritz | ||
*/ | ||
public class Card | ||
{ | ||
public static enum SUIT | ||
{ | ||
CLUBS, | ||
DIAMONDS, | ||
HEARTS, | ||
SPADES | ||
} | ||
|
||
/** | ||
* 1 is an ace | ||
* 14 is an ace | ||
*/ | ||
private int rank; | ||
|
||
private SUIT suit; | ||
|
||
public Card(int rank, SUIT suit) | ||
{ | ||
if (rank > 0 && rank < 15) | ||
{ | ||
this.rank = rank; | ||
} | ||
|
||
this.suit = suit; | ||
} | ||
|
||
public int getRank() | ||
{ | ||
return this.rank; | ||
} | ||
public SUIT getSuit() | ||
{ | ||
return this.suit; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
switch (rank) | ||
{ | ||
case 1: | ||
sb.append("ACE"); | ||
break; | ||
case 11: | ||
sb.append("JACK"); | ||
break; | ||
case 12: | ||
sb.append("QUEEN"); | ||
break; | ||
case 13: | ||
sb.append("KING"); | ||
break; | ||
case 14: | ||
sb.append("ACE"); | ||
break; | ||
default: | ||
sb.append(rank); | ||
break; | ||
} | ||
|
||
sb.append(" of "); | ||
|
||
switch (suit) | ||
{ | ||
case CLUBS: | ||
sb.append("CLUBS"); | ||
break; | ||
case DIAMONDS: | ||
sb.append("DIAMONDS"); | ||
break; | ||
case HEARTS: | ||
sb.append("HEARTS"); | ||
break; | ||
case SPADES: | ||
sb.append("SPADES"); | ||
break; | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package pokerhands; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import pokerhands.Card.SUIT; | ||
|
||
/** | ||
* | ||
* @author jfritz | ||
*/ | ||
public class Deck | ||
{ | ||
ArrayList<Card> cards = new ArrayList<>(); | ||
|
||
private int index = 0; | ||
|
||
public Deck() | ||
{ | ||
for (SUIT s : Card.SUIT.values()) | ||
{ | ||
for (int r = 1; r < 14; r++) | ||
{ | ||
cards.add(new Card(r, s)); | ||
} | ||
} | ||
} | ||
|
||
public void shuffle() | ||
{ | ||
Random generator = new Random(); | ||
|
||
for (int i = 0; i < 1000; i++) | ||
{ | ||
int index1 = generator.nextInt(52); | ||
cards.add(cards.remove(index1)); | ||
} | ||
|
||
this.index = 0; | ||
} | ||
|
||
public Card drawCard() | ||
{ | ||
this.index++; | ||
return cards.get(index - 1); | ||
} | ||
|
||
public List<Card> drawCards(int howMany) | ||
{ | ||
ArrayList<Card> drawnCards = new ArrayList<>(); | ||
for(int i = 0; i < howMany; i++) | ||
{ | ||
drawnCards.add(this.drawCard()); | ||
} | ||
return drawnCards; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
for (Card c : cards) | ||
{ | ||
sb.append(c.toString()); | ||
sb.append(System.getProperty("line.separator")); | ||
} | ||
return sb.toString(); | ||
} | ||
} |