interview-preparation / what-we-do

0 stars 8 forks source link

[Object-Oriented Design] interview questions #1 #108

Closed btakeya closed 5 years ago

btakeya commented 5 years ago

Deck of Cards: Design the data structures for a generic deck of cards. Explain how you would subclass the data structures to implement blackjack.

GyuminLee commented 5 years ago
public enum Suit {
    Club (0), Diamond (1), Heart (2), Spade (3);
    private int value;

    private Suit(int v) {
        value = v;
    }

    public int getValue() {
        return value;
    }

    public static Suit getSuitFromValue(int value) {
        //...
    }

}

public class Deck <T extends Card> {
    private ArrayList<T> cards; // 모든 카드
    private int dealtIndex = 0; // 돌리지 않은 첫번째 카드

    public void setDeckOfCards(ArrayList<T> deckOfCards) {
        //...
    }

    public void shuffle() {
        //...
    }

    public int remainingCards() {
        return cards.size() - dealtIndex;
    }

    public T[] dealHand(int number) {
        //...
    }

    public T dealCard() {
        //...
    }
}

public abstract class Card {
    private boolean available = true;

    protected int faceValue;    //왜 protected?
    protected Suit suit;        //왜 protected?

    public Card(int c, Suit s) {
        faceValue = c;
        suit = s;
    }

    public abstract int value();

    public Suit suit() {
        return suit;
    }

    // 카드를 돌릴 수 있는 상태인 지 확인

    public boolean isAvailable() {
        return available;
    }

    public void markUnavailable() {
        available = false;
    }

    public void markAvailable() {
        available = true;
    }
}
public class Hand <T extends Card> {
    protected ArrayList<T> cards = new ArrayList<T>(); //왜 protected?

    public int score() {
        int score = 0;
        for(T cards : cards) {
            score += card.value();
        }
        return score;
    }

        public void addCard(T card){
            cards.add(card);
        }
}
public class BlackJackHand extends Hand<BlackJackCard> {
    public int score() {
        ArrayList<Integer> scores = possibleScores();
        int maxUnder = Integer.MIN_VALUE;
        int minOver = Integer.MAX_VALUE;

        for(int score : scores) {
            if(score > 21 && score < minOver) {
                minOver = score;
            } else if(score <= 21 && score > maxUnder) {
                maxUnder = score;
            }
        }
        return maxUnder == Integer.MIN_VALUE ? minOver : maxUnder;
    }

    private ArrayList<Integer> possibleScores() {
        //...
    }
    public boolean busted() {
        return score() > 21;
    }
    public boolean is21() {
        return score() == 21;
    }
    public boolean isBlackJack () {
        //...
    }
}

public class BlackJackCard extends Card {
    public BlackJackCard(int c, Suit s) {
        super(c, s);
    }

    public int value() {
        if(isAce()) {
                return 1;
            } else if(faceValue >= 11 && faceValue <= 13) {
                return 10;
            } else {
                return faceValue;
            }
        }

        public int minValue() {
            if(isAce()) {
                return 1;
            } else {
                return value();
            }
        }

        public int maxValue() {
            if(isAce()) {
                return 11;
            } else {
                return value();
            }
        }

        public boolean isAce() {
            return faceValue == 1;
        }

        public boolean isFaceCard() {
            return faceValue >= 11 && faceValue <= 13;
        }
}