Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /*
- * To execute Java, please define "static void main" on a class
- * named Solution.
- *
- * If you need more classes, simply define them inline.
- */
- class Solution {
- public static void main(String[] args) {
- // Card testCard = new Card("Heart", "Jack");
- // Card testCard2 = new Card("Spades", "8");
- // ArrayList<Card> testCards = new ArrayList<Card>();
- // testCards.add(testCard);
- // testCards.add(testCard2);
- // Deck deck = new Deck(testCards);
- // Card drawnCard = deck.draw();
- // System.out.println("Drawn card: " + drawnCard.suit + " " + drawnCard.value);
- // System.out.println("Deck length: " + deck.cards.size());
- Deck deck = new Deck();
- deck.shuffle();
- for (int i = 0; i < deck.cards.size(); i++) {
- System.out.println(deck.cards.get(i).suit + " " + deck.cards.get(i).value);
- }
- }
- }
- class Card {
- public String suit;
- public String value;
- public Card(String suit, String value) {
- this.suit = suit;
- this.value = value;
- }
- }
- class Deck {
- public ArrayList<Card> cards;
- private final String[] cardSuits =
- {"Hearts", "Spades", "Clubs", "Diamonds"};
- private final String[] cardValues =
- {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
- public Deck() {
- cards = new ArrayList<Card>();
- generateCards();
- }
- public void generateCards() {
- for (String suit : cardSuits) {
- for(String value : cardValues) {
- cards.add(new Card(suit, value));
- }
- }
- }
- public Card draw() {
- Card draw = cards.get(cards.size()-1);
- cards.remove(cards.size()-1);
- return draw;
- }
- public void shuffle() {
- Collections.shuffle(cards);
- }
- }
- /*
- Your previous Plain Text content is preserved below:
- For this coding exercise, I ask that you do the following:
- Implement a deck of cards.
- - The deck must contain 52 cards
- - Each card can be one of four suits (Hearts, Spades, Diamonds, Clubs)
- - A card can also be one of 13 values (1, 2, 3, ...Jack, Queen, King)
- - I should be able to shuffle the deck of cards
- - I should be able to draw one card from the top of the deck
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement