8.1

Topic 8.1 Design the data structures for a generic deck of cards. Explain how you would subclass the data structures to implement blackjack.

public enum Suit {
	Club (0),
	Diamond (1),
	Heart (2),
	Spade (3);

	private int value;
	private Suit(int value) {
		this.value = value;
	}

	public int getValue() {
		return value;
	}

	public static Suit getSuitFromValue(int value) {
		switch (value) {
		case 0:
			return Suit.Club;
		case 1:
			return Suit.Diamond;
		case 2:
			return Suit.Heart;
		case 3: 
			return Suit.Spade;
		default:
				return null;
		}
	}
}
public abstract class Card {
	private boolean available = true;
	
	protected int value;
	protected Suit suit;
	
	public Card(int value, Suit suit) {
		this.value = value;
		this.suit = suit;
	}
	
	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 void print() {
		String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
		System.out.print(values[value - 1]);
		switch (suit) {
		case Club:
			System.out.print("Club");
			break;
		case Heart:
			System.out.print("Heart");
			break;
		case Diamond:
			System.out.print("Diamond");
			break;
		case Spade:
			System.out.print("Spade");
			break;			
		}
		System.out.print(" ");
	}
}




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值