我有一个名为card-set的类,它包含我的卡的数组列表
public abstract class CardSet {
public ArrayList<Card> cards;
public Card card = new Card();
private int cardSize;
public CardSet(int cardSize) {
this.cardSize = cardSize;
}
public CardSet() {};
public abstract void displayAllCards();
public abstract void displayVisibleCards();
}
Enum穿着我所有的西装
public enum Suit {
CLUBS,
DIAMONDS,
HEARTS,
SPADES
}
用52张牌填充牌组的牌组
public class Deck extends CardSet {
private final int SIZE = 52;
public Deck() {
populateDeck();
shuffle();
draw();
}
public void populateDeck() {
for (int i = 0; i < SIZE; i++) {
for (int a = 0; a <= 3; a++) {
for (int b = 0; b <= 12; b++) {
cards.add(new Card(a, b));
}
}
}
}
public void shuffle() {
Collections.shuffle(cards);
}
public void draw() {//ie taking one from the top of the deck
cards.remove(0);
}
public int size() {
return this.SIZE;
}
public void displayCards(int start) {
System.out.println(cards.get(start));
}
public void displayAllCards() {
for(Card i : cards) {
System.out.println(i + " ");
}
}
}
我的问题是,我不确定如何每次点击每次添加1张卡片,或者我如何为我的程序单独制作手牌。我对此非常陌生。
public class Hand extends CardSet {
private String userName;
public ArrayList<Card> cardsInHand;
/**
*
* @param userName
*/
public Hand(String userName) {
this.userName = userName;
}
/**
*
* @return
*/
public String getUserName() {
return this.userName;
}
/**
*
* @param userName
*/
public void setUserName(String userName) {
this.userName = userName;
}
public void hit(Card c) {
cardsInHand.add(cards.get(0));
}
public int getCardValue(Card c) {
return c.getRank();
}
}
GameOf21:
public class GameOf21 {
Deck deck;
Hand user;
Hand computer;
Scanner input = new Scanner(System.in);
public GameOf21(String name) {
deck = new Deck();
user = new Hand(name);
computer = new Hand("Computer");
}
public void playGame() {
System.out.println("The deck has " + deck.size() + " cards");
System.out.println("*********" + user + "********");
user.hit();
}
public String printTitle() {
return "This application allows a player to play the game of "
+ "\n21 against the computer.";
}
}
开始玩游戏的课程
public class GameOf21App {
public static void main(String[] args) {
String name = " ";
GameOf21 g = new GameOf21(name);
System.out.println(g.printTitle() + "\n" );
System.out.println("Enter your name: ");
name = g.input.nextLine();
System.out.println("Welcome to the Game of 21 " + name + "\n");
g.playGame();
}
}
本程序应该只针对计算机模拟21的游戏。每个玩家手中都装有2张牌,并且可以选择最后一击。如果超过21岁,那么过去的人就输了。你们都知道怎么玩。
我的问题在于hand.hit()
方法,我无法弄清楚如何从我的牌组中正确添加值。它只是没有点击,任何帮助都表示赞赏。