Last active
January 3, 2016 18:27
-
-
Save kazyka/7a56617337d21706ec5e to your computer and use it in GitHub Desktop.
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
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Random; | |
/** | |
* Created by Baby- on 31-Dec-15. | |
*/ | |
public class Deck implements Iterable<Card>{ | |
private ArrayList<Card> cards; | |
public Iterator<Card> iterable() { | |
return cards.iterator(); | |
} | |
@Override | |
public Iterator<Card> iterator() { | |
return null; | |
} | |
public Deck() { | |
this.cards = new ArrayList<Card>(); | |
} | |
List<Card> getCards() { | |
return cards; | |
} | |
public void createDeck() { | |
for (Suit suit : Suit.values()) { | |
for (Rank rank : Rank.values()) { | |
this.cards.add(new Card(rank, suit)); | |
} | |
} | |
} | |
public void shuffle() { | |
ArrayList<Card> tmpDeck = new ArrayList<Card>(); | |
Random random = new Random(); | |
int randCardIndex = 0; | |
int orginalSize = this.cards.size(); | |
for (int i = 0; i < orginalSize; i++) { | |
randCardIndex = random.nextInt((this.cards.size() - 1 ) + 1); | |
tmpDeck.add(this.cards.get(randCardIndex)); | |
this.cards.remove(randCardIndex); | |
} | |
this.cards = tmpDeck; | |
} | |
public Card dealCard() { | |
return cards.remove(0); | |
} | |
public int valueOfCards() { | |
int totalValue = 0; | |
int aces = 0; | |
for (Card aCard : this.cards) { | |
switch (aCard.getRank()) { | |
case TWO: totalValue += 2; break; | |
case THREE: totalValue += 3; break; | |
case FOUR: totalValue += 4; break; | |
case FIVE: totalValue += 5; break; | |
case SIX: totalValue += 6; break; | |
case SEVEN: totalValue += 7; break; | |
case EIGHT: totalValue += 8; break; | |
case NINE: totalValue += 9; break; | |
case TEN: totalValue += 10; break; | |
case JACK: totalValue += 10; break; | |
case QUEEN: totalValue += 10; break; | |
case KING: totalValue += 10; break; | |
case ACE: aces += 1; break; | |
} | |
} | |
for (int i = 0; i < aces; i++) { | |
if (totalValue > 10) { | |
totalValue += 1; | |
} | |
else { | |
totalValue += 11; | |
} | |
} | |
return totalValue; | |
} | |
public int deckSize() { | |
return this.cards.size(); | |
} | |
@Override | |
public String toString() { | |
String output = ""; | |
for (Card aCard : this.cards) { | |
output += "\n" + " " + aCard; | |
} | |
return output; | |
} | |
public static void main(String[] args) { | |
Deck test = new Deck(); | |
test.createDeck(); | |
System.out.println(test); | |
test.shuffle(); | |
System.out.println(test); | |
System.out.println(test.deckSize()); | |
test.dealCard(); | |
System.out.println(test.deckSize()); | |
System.out.println(test.dealCard()); | |
System.out.println(test.dealCard()); | |
System.out.println(test.dealCard()); | |
System.out.println(test.dealCard()); | |
System.out.println(test.deckSize()); | |
} | |
} |
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
import junit.framework.TestCase; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
import static org.junit.Assert.assertEquals; | |
public class DeckTest extends TestCase { | |
Deck deck = new Deck(); | |
List<Card> generateCardsForSuit(final Suit suit) { | |
final List<Card> result = new ArrayList<>(); | |
for (Rank rank : Rank.values()) { | |
result.add(new Card(rank, suit)); | |
} | |
return result; | |
} | |
@Test | |
public void testCreateDeck() throws Exception { | |
assertEquals(deck, deck.createDeck()); <--- I have (Deck, void) | |
} | |
@Test | |
public void testShuffle() throws Exception { | |
} | |
@Test | |
public void testDealCard() throws Exception { | |
} | |
@Test | |
public void testValueOfCards() throws Exception { | |
} | |
@Test | |
public void testDeckSize() throws Exception { | |
assertEquals(52, deck.deckSize()); | |
} | |
@Test | |
public void testToString() throws Exception { | |
} | |
@Test | |
public void testMain() throws Exception { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment